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

相关内容

热门资讯

第8分钟辅助!朱雀开心罗松辅助... 第8分钟辅助!朱雀开心罗松辅助器,本来是有辅助app(有挂攻略)1、朱雀开心罗松辅助器脚本辅助下载、...
3分钟辅助!爱来510k辅助器... 3分钟辅助!爱来510k辅助器,本来真的有辅助挂(有挂教程)1、3分钟辅助!爱来510k辅助器,本来...
3分钟辅助!白金岛辅助有用吗,... 3分钟辅助!白金岛辅助有用吗,切实真的有辅助攻略(确实有挂)该软件可以轻松地帮助玩家将白金岛辅助有用...
第1分钟辅助!兴动互娱辅助脚本... 第1分钟辅助!兴动互娱辅助脚本下载,都是存在有辅助攻略(有挂神器)1、游戏颠覆性的策略玩法,独创攻略...
6分钟辅助!边锋小程序辅助脚本... 6分钟辅助!边锋小程序辅助脚本免费,切实真的有辅助技巧(揭秘有挂)1、边锋小程序辅助脚本免费免费脚本...
九分钟辅助!广东雀神智能插件安... 九分钟辅助!广东雀神智能插件安装包免费,确实真的有辅助技巧(有挂规律)广东雀神智能插件安装包免费是不...
三分钟辅助!奇迹手机辅助脚本怎... 三分钟辅助!奇迹手机辅助脚本怎么下载,原来真的有辅助挂(有挂秘诀)1、奇迹手机辅助脚本怎么下载透视辅...
第5分钟辅助!新九游辅助,本来... 第5分钟辅助!新九游辅助,本来有辅助攻略(有挂分享)1、进入游戏-大厅左侧-新手福利-激活码辅助透视...
四分钟辅助!多乐手游辅助软件,... 四分钟辅助!多乐手游辅助软件,真是是有辅助技巧(有挂方式)1、首先打开多乐手游辅助软件辅助器下载最新...
第3分钟辅助!随意玩免费辅助器... 第3分钟辅助!随意玩免费辅助器,切实有辅助挂(有挂攻略)1、该软件可以轻松地帮助玩家将随意玩免费辅助...