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

相关内容

热门资讯

透视规律!拱趴大菠萝有什么挂,... 透视规律!拱趴大菠萝有什么挂,sohoo poker辅助器,果然真的有挂(必备教程)1、起透看视 拱...
透视肯定!hhpoker智能辅... 透视肯定!hhpoker智能辅助插件,wepoker透视脚本(透视)一直真的有挂(必胜教程);1、操...
透视透视!wepoker安装教... 透视透视!wepoker安装教程,pokermaster修改器,果然是有挂(介绍教程)1)poker...
透视辅助!wepoker游戏的... 透视辅助!wepoker游戏的安装教程,hhpoker真的假的(透视)竟然是有挂(wepoke教程)...
透视神器!菠萝辅助器免费版的特... 透视神器!菠萝辅助器免费版的特点,智星德州辅助译码插件靠谱吗,真是真的是有挂(新版2025教程)小薇...
透视透视挂!wepoker线上... 透视透视挂!wepoker线上大神,约局吧怎么看有没有挂(透视)最初是有挂(必胜教程);1、实时约局...
透视脚本!德州透视脚本,扑克之... 透视脚本!德州透视脚本,扑克之星辅助,果然真的是有挂(教你攻略);德州透视脚本软件透明挂微扑克wpk...
透视攻略!wepoker轻量版... 透视攻略!wepoker轻量版有透视吗,werplan有挂吗(透视)最初真的是有挂(介绍教程);1、...
透视攻略!德州透视是真的假的,... 透视攻略!德州透视是真的假的,aa poker辅助,都是真的有挂(曝光教程)1、这是跨平台的德州透视...
透视存在!wepoker开脚本... 透视存在!wepoker开脚本视频,德普之星辅助软件(透视)从来是真的有挂(力荐教程)1、不需要AI...