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

相关内容

热门资讯

2026版教程!pokerma... 2026版教程!pokermaster修改器"曝光辅助攻略"确实真的是有挂(哔哩哔哩)小薇(辅助器软...
2026版技巧!牵手跑得软件&... 2026版技巧!牵手跑得软件"解谜辅助软件"真是是有挂(哔哩哔哩)1、牵手跑得软件透视辅助软件激活码...
透视辅助!填大坑辅助器视频&q... 透视辅助!填大坑辅助器视频"开挂辅助神器"其实是有挂(哔哩哔哩)1、打开软件启动之后找到中间准星的标...
透视存在!微友辅助器免费版&q... 透视存在!微友辅助器免费版"解谜辅助技巧"竟然是有挂(哔哩哔哩)1、微友辅助器免费版破解器简单,微友...
规律辅助挂!上饶中至软件下载链... 规律辅助挂!上饶中至软件下载链接在哪里"详情辅助插件"竟然是有挂(哔哩哔哩)1、操作简单,无需上饶中...
方法辅助挂!微乐卡五星有挂吗&... 方法辅助挂!微乐卡五星有挂吗"开挂辅助app"果然真的是有挂(哔哩哔哩)1、微乐卡五星有挂吗脚本辅助...
透视苹果版!约局辅助透视开挂有... 透视苹果版!约局辅助透视开挂有吗"科普辅助器"一直是真的有挂(哔哩哔哩)1、游戏颠覆性的策略玩法,独...
透视透视!同乡游有辅助软件吗&... 透视透视!同乡游有辅助软件吗"解密辅助攻略"其实是有挂(哔哩哔哩)同乡游有辅助软件吗透视方法中分为三...
2026版规律!兴动互娱绥化辅... 2026版规律!兴动互娱绥化辅助器挂"关于辅助方法"果然存在有挂(哔哩哔哩)1、不需要AI权限,帮助...
2026版规律!小程序牵手跑得... 2026版规律!小程序牵手跑得辅助器下载"关于辅助方法"总是是真的有挂(哔哩哔哩)1、下载好小程序牵...