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

相关内容

热门资讯

今年以来!黑侠破解wepoke... 今年以来!黑侠破解wepoker(透视)微乐麻将脚本透视(一直是真的辅助方法)-哔哩哔哩1、黑侠破解...
长期以来!wepoker有辅助... 长期以来!wepoker有辅助插件吗(透视)微乐广西小程序开发(一贯是有辅助插件)-哔哩哔哩亲,关键...
透视软件!wepoker有辅助... 透视软件!wepoker有辅助工具吗(透视)微信小程序微乐房间透视(都是存在有辅助挂)-哔哩哔哩1、...
透视实锤!cloudpoker... 透视实锤!cloudpoker怎么开挂(透视)手机微乐小程序破解器(一贯真的是有辅助攻略)-哔哩哔哩...
有了最新消息!拱趴大菠萝开挂方... 有了最新消息!拱趴大菠萝开挂方法(透视)微乐云南小程序修改器ios(本来是真的辅助软件)-哔哩哔哩1...
据文件显示!wepoker有什... 据文件显示!wepoker有什么规律(透视)微乐小程序辅助工具免费版(一贯是真的辅助神器)-哔哩哔哩...
透视脚本!wepoker怎么破... 透视脚本!wepoker怎么破解游戏(透视)提高微乐运气的方法(总是是真的辅助攻略)-哔哩哔哩1、许...
透视辅助!如何判断wpk辅助软... 透视辅助!如何判断wpk辅助软件的真假(透视)微乐广西小程序开挂(一贯真的有辅助器)-哔哩哔哩如何判...
更值得关注的是!wepoker... 更值得关注的是!wepoker破解游戏盒子(透视)微乐智能插件激活码(切实是有辅助攻略)-哔哩哔哩w...
透视了解!德普之星透视辅助软件... 透视了解!德普之星透视辅助软件激活码(透视)小程序微乐贵阳捉鸡麻将挂(其实是有辅助器)-哔哩哔哩1、...