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

相关内容

热门资讯

一分钟揭秘!四川熊猫辅助器,闲... 一分钟揭秘!四川熊猫辅助器,闲逸辅助插件,详细方法(有挂教程)1、首先打开四川熊猫辅助器辅助器下载最...
一分钟教你!大菠萝免费辅助,手... 一分钟教你!大菠萝免费辅助,手游蛮王辅助,详细技巧(有挂秘笈)1、实时大菠萝免费辅助透视辅助更新:用...
玩家爆料!天天贵阳辅助工具,对... 玩家爆料!天天贵阳辅助工具,对战互娱辅助系统,详细方法(有挂辅助)1、任何天天贵阳辅助工具透视是真的...
玩家必看秘籍!决战卡五星辅助器... 玩家必看秘籍!决战卡五星辅助器下载,山西扣点点辅助,详细方法(有挂方略)决战卡五星辅助器下载透视方法...
玩家必看科普!奇迹免费脚本辅助... 玩家必看科普!奇迹免费脚本辅助工具,腾讯欢乐卡五星辅助器,详细神器(有挂总结)1、腾讯欢乐卡五星辅助...
一秒答解!皮皮游戏辅助平台,火... 一秒答解!皮皮游戏辅助平台,火神大厅辅助器,详细工具(有挂细节)1、皮皮游戏辅助平台公共底牌简单,皮...
我来分享!摸一把麻将辅助,赣南... 我来分享!摸一把麻将辅助,赣南好友辅助,详细挂(有挂功能)我来分享!摸一把麻将辅助,赣南好友辅助,详...
玩家实测!微信小程序蜀山四川辅... 您好,微信小程序蜀山四川辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
一分钟教你!微友助手破解版,广... 一分钟教你!微友助手破解版,广东雀神辅助器透视,详细挂(有挂攻略);1、广东雀神辅助器透视辅助器安装...
一分钟揭秘!茶馆游戏辅助,湖北... 一分钟揭秘!茶馆游戏辅助,湖北卡五星插件,详细神器(有挂秘籍)1、这是跨平台的湖北卡五星插件轻量版有...