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

相关内容

热门资讯

我来教大家!微乐智能辅助app... 您好,微乐智能辅助app这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
推荐一款!微乐春天小程序辅助(... 推荐一款!微乐春天小程序辅助(透视挂),微乐四冲都是真的是有透视挂教程1.微乐春天小程序辅助 选牌创...
揭秘关于!微乐游戏公众号辅助器... 揭秘关于!微乐游戏公众号辅助器(透视器),微乐A三纸牌一贯真的有透视挂方法1)微乐游戏公众号辅助器有...
实测分享!微信小程序游戏破解器... 实测分享!微信小程序游戏破解器(开挂),微乐惯蛋真是是有透视挂方法一、微信小程序游戏破解器可以开透视...
一秒答解!微乐游戏小程序脚本(... 一秒答解!微乐游戏小程序脚本(外挂),微乐刨幺一直真的是有透视挂插件1、打开软件启动之后找到中间准星...
最新技巧!微乐游戏辅助脚本安装... 最新技巧!微乐游戏辅助脚本安装(辅助器),微乐A三纸牌好像真的有透视挂教程微乐游戏辅助脚本安装是不是...
重大通报!微乐广西麻辣辅助器(... 重大通报!微乐广西麻辣辅助器(透视挂),微乐三丁拐一直存在有透视挂技巧运微乐广西麻辣辅助器辅助工具,...
揭秘关于!微乐江苏小程序游戏破... 揭秘关于!微乐江苏小程序游戏破解下载(透视器),微乐山东麻将其实真的有透视挂攻略微乐江苏小程序游戏破...
玩家必看攻略!微信小程序 微乐... 玩家必看攻略!微信小程序 微乐陕西挖坑 外挂(透视挂),微乐斗地主原来是真的有透视挂插件运微信小程序...
一分钟秒懂!如何下载微乐自建房... 一分钟秒懂!如何下载微乐自建房黑科技入口(辅助器),微乐小程序其实真的有透视挂脚本1、玩家可以在如何...