Android推送通知数据
创始人
2024-08-21 21:00:34
0

Android中实现推送通知需要使用第三方库或者服务,比如Firebase Cloud Messaging(FCM),JPush等。以下是使用FCM实现推送通知的代码示例:

  1. 在项目根目录下的build.gradle文件中添加以下依赖:
dependencies {
    // ...
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
}
  1. 在AndroidManifest.xml文件中添加以下代码:





    
        
    




    
        
        
    

  1. 在MyFirebaseMessagingService类中添加以下代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMessagingService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        
        // 获取通知数据
        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();
        Map data = remoteMessage.getData();
        
        // 处理通知
        sendNotification(title, body, data);
    }

    private void sendNotification(String title, String body, Map data) {
        // 构建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default");
        builder.setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentIntent(getPendingIntent(data));

        // 发送通知
        NotificationManagerCompat notificationManager =
                NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());
    }

    private PendingIntent getPendingIntent(Map data) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setAction("OPEN_NOTIFICATION_ACTION");
        for (String key : data.keySet()) {
            intent.putExtra(key, data.get(key));
        }
        return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
}

上述代码中,使用MyFirebaseMessagingService类继

相关内容

热门资讯

黑科技好友!手机云扑克辅助是真... 黑科技好友!手机云扑克辅助是真的假的(微扑克ai机器人)原先是真的有挂(讲解有挂)-哔哩哔哩是一款可...
黑科技线上!云扑克辅助挂(we... 一、wepoke有没有挂简介了解软件请加微:136704302wepoke有没有挂是一款在线扑克游戏...
黑科技代打!cloudpoke... 黑科技代打!cloudpoker云扑克(wepoke智能ai)固有有挂(有挂方式)-哔哩哔哩1、每一...
黑科技神器!aapoker俱乐... 1、黑科技神器!aapoker俱乐部(德扑ai机器人软件开发)果然是有挂(有挂讲解)-哔哩哔哩。2、...
黑科技辅助挂!微扑克辅助软件(... 黑科技辅助挂!微扑克辅助软件(wepoke智能ai)其实是有挂(讲解有挂)-哔哩哔哩1、wepoke...
黑科技软件!德扑数据软件(德州... 一、德州ai辅助软件简介了解软件请加微:136704302德州ai辅助软件是一款在线扑克游戏平台,玩...
黑科技了解!pokerrrr开... 黑科技了解!pokerrrr开挂(aapoker辅助工具ai)原先是真的有挂(有挂讲解)-哔哩哔哩1...
黑科技辅助挂!gg扑克有问题(... 黑科技辅助挂!gg扑克有问题(wepoke黑科技)原来真的有挂(有挂教程)-哔哩哔哩;黑科技辅助挂!...
黑科技代打!红龙扑克好友桌(a... 黑科技代打!红龙扑克好友桌(aapoker辅助工具ai)原生是真的有挂(讲解有挂)-哔哩哔哩1、许多...
黑科技肯定!wepoke黑科技... 黑科技肯定!wepoke黑科技功能(德扑AI策略软件)原本有挂(真是有挂)-哔哩哔哩1)德扑AI策略...