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、皮皮跑子胡子修改...
现场直击!越乡游义乌辅助器微信... 现场直击!越乡游义乌辅助器微信,新玄龙辅助-竟然是有辅助挂(哔哩哔哩);所有人都在同一条线上,像星星...
今年以来!欢乐达人暗堡链接脚本... 今年以来!欢乐达人暗堡链接脚本,哈糖大菠萝辅助器-好像真的是有辅助app(哔哩哔哩)1、不需要AI权...
今天上午!潮友汇俱乐部辅助器下... 今天上午!潮友汇俱乐部辅助器下载,情怀宜春辅助-切实真的是有辅助神器(哔哩哔哩)1、完成潮友汇俱乐部...
最新消息!南通长牌辅助,心悦填... 最新消息!南通长牌辅助,心悦填大坑辅助-确实存在有辅助挂(哔哩哔哩)心悦填大坑辅助辅助器是一种具有地...
透视辅助!天天互娱辅助器免费下... 透视辅助!天天互娱辅助器免费下载,禅游指尖四川修改器-本来是真的有辅助工具(哔哩哔哩)在进入天天互娱...
今天上午!微信雀神挂件辅助器,... 今天上午!微信雀神挂件辅助器,wepoker私人局辅助器-原来是真的有辅助方法(哔哩哔哩)1、完成微...
黑科技技巧!长了四人上品十三攻... 黑科技技巧!长了四人上品十三攻略,飞驰娱乐软件科技-一直真的有辅助教程(哔哩哔哩)运长了四人上品十三...
透视ai!竹间智能辅助助手,哈... 透视ai!竹间智能辅助助手,哈灵永久辅助-都是是真的有辅助器(哔哩哔哩)1、竹间智能辅助助手辅助器安...
据报道!兴动互娱修改器免费,胡... 据报道!兴动互娱修改器免费,胡乐辅助脚本-切实真的是有辅助软件(哔哩哔哩)1、完成兴动互娱修改器免费...