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、进入到游戏大厅浙...
8分钟外挂!九九山城万州版辅助... 8分钟外挂!九九山城万州版辅助,指尖四川辅助脚本,解说技巧(存在有挂)-哔哩哔哩1、下载好九九山城万...
6分钟外挂!威信茶馆app插件... 6分钟外挂!威信茶馆app插件,微信中至有辅助吗,教你攻略(有挂功能)-哔哩哔哩1)微信中至有辅助吗...
7分钟外挂!闲逸辅助插件,闲聚... 7分钟外挂!闲逸辅助插件,闲聚辅助器,AI教程(果真有挂)-哔哩哔哩1、该软件可以轻松地帮助玩家将闲...
第2分钟外挂!哈狗智能辅助,衢... 第2分钟外挂!哈狗智能辅助,衢州都莱辅助器是真是假,扑克教程(有挂神器)-哔哩哔哩1、打开软件启动之...
第2分钟外挂!丫丫打锅子辅助,... 第2分钟外挂!丫丫打锅子辅助,贪玩互娱辅助,技巧教程(有挂方式)-哔哩哔哩1、实时丫丫打锅子辅助开挂...
六分钟外挂!温州茶苑手机辅助器... 六分钟外挂!温州茶苑手机辅助器,边锋老友内蒙古辅助,细节揭秘(真的有挂)-哔哩哔哩1、操作简单,无需...
第九分钟外挂!余干510k副5... 第九分钟外挂!余干510k副510k,黑桃a3辅助,必胜教程(有挂方法)-哔哩哔哩1、余干510k副...
第9分钟外挂!闲逸免费app辅... 第9分钟外挂!闲逸免费app辅助,金华佛手在线辅助,专业教程(真是有挂)-哔哩哔哩1、不需要AI权限...