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、下载好微乐自建房辅助...
避坑细节!微乐小程序自建房辅助... 避坑细节!微乐小程序自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)插件1、操作简单,无需微乐...
玩家必看科普!微乐自建房辅助工... 玩家必看科普!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)技巧1、超多福利:超...
交流学习经验!微乐微信自建房辅... 交流学习经验!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、微乐微信...
六分钟了解!微乐微信自建房辅助... 六分钟了解!微乐微信自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)教程1、微乐微信自建房辅...
新手必备!微信雀神麻将挂件开挂... 新手必备!微信雀神麻将挂件开挂辅助,微乐家乡麻将自建房辅助app(开挂自建房)教程1、起透看视 微乐...
一分钟教你!微乐微信自建房辅助... 一分钟教你!微乐微信自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)脚本微乐微信自建房辅助工...
分享认知!微乐自建房辅助工具免... 分享认知!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)app1、每一步都需要思...