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类继

相关内容

热门资讯

我来教大家“陕西三代实战技巧”... 【亲,陕西三代实战技巧 这款游戏可以开挂的,确实是有挂的,很多玩家在这款陕西三代实战技巧中打牌都会发...
最新研发“鱼虾蟹辅助视频教程”... 鱼虾蟹辅助视频教程开挂教程视频分享装挂详细步骤在当今的网络游戏中,鱼虾蟹辅助视频教程作为一种经典的娱...
透视软件“上饶中至辅助插件透视... 透视软件“上饶中至辅助插件透视”原先有辅助工具(存在有挂);无需打开直接搜索加薇136704302(...
玩家必看“余干五十k装挂”起初... 余干五十k装挂是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微信...
重磅来袭“玄龙大厅怎么开辅助”... 玄龙大厅怎么开辅助是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我...
查到实测“破解大菠萝的辅助器”... 破解大菠萝的辅助器是一款专注玩家量身打造的游戏记牌类型软件,在破解大菠萝的辅助器这款游戏中我们可以记...
玩家爆料“指尖辅助”本来有开挂... 指尖辅助是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微信(13...
揭秘攻略“心悦怎么开挂”一向有... 心悦怎么开挂是一款专注玩家量身打造的游戏记牌类型软件,在心悦怎么开挂这款游戏中我们可以记录下每张牌的...
新手必备“九游辅助辅助在哪里”... 您好:这款九游辅助辅助在哪里游戏是可以开挂的,确实是有挂的,很多玩家在这款九游辅助辅助在哪里游戏中打...
透视代打!吉祥填大坑插件,德州... 您好:这款德州辅助工具到底怎么样游戏是可以开挂的,确实是有挂的,很多玩家在这款德州辅助工具到底怎么样...