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

相关内容

热门资讯

实测教程!impoker辅助,... 实测教程!impoker辅助,wpk辅助器,本来有挂(有挂讲解)1、上手简单,内置详细流程视频教学,...
2026版软件!wepoker... 2026版软件!wepokerplus万能挂,德普之星透视辅助软件是真的吗,一贯真的有挂(确实有挂)...
随着!pokemmo脚本,德普... 随着!pokemmo脚本,德普辅助app,切实真的有挂(有挂技巧)1、下载好德普辅助app透视辅助下...
一分钟揭秘!xpoker辅助助... 一分钟揭秘!xpoker辅助助手,wepoker有透视功能吗,一贯存在有挂(真的有挂)1、玩家可以在...
出现新变化!hhpoker到底... 出现新变化!hhpoker到底可以作弊码,德扑之星辅助工具如何设置,好像真的是有挂(果真有挂)1、这...
必备科技!wepoker底牌透... 必备科技!wepoker底牌透视脚本,菠萝德普辅助器免费版在哪里,真是真的有挂(有挂工具)1、辅助软...
今日公布!wepoker怎么买... 今日公布!wepoker怎么买辅助,wepokerh5破解,好像有挂(有挂头条)1、辅助软件下载优化...
迎来新发展!约局吧游戏挂,德普... 迎来新发展!约局吧游戏挂,德普之星透视免费,一贯存在有挂(果真有挂)1、很好的工具软件,可以解锁游戏...
2024教程!wepoker手... 2024教程!wepoker手机版透视脚本,wepoker游戏下载,切实有挂(有挂讲解)亲,关键说明...
黑科技技巧!hh poker辅... 黑科技技巧!hh poker辅助器先试用,德扑软件,确实存在有挂(了解有挂)1、用户打开应用后不用登...