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、任何智星菠萝透视透视是真...
解密透视!wpk插件辅助,微乐... 解密透视!wpk插件辅助,微乐自建房免费辅助入口在哪里,策略教程(有挂教学)-哔哩哔哩1、在wpk插...
详细透视!智星德州插件最新版本... 详细透视!智星德州插件最新版本更新内容详解,微乐春天小程序辅助,总结教程(的确有挂)-哔哩哔哩1、实...
辅助透视!红龙poker辅助工... 辅助透视!红龙poker辅助工具,微信小程序微乐破解器2025,教程书教程(有挂分析)-哔哩哔哩辅助...
曝光透视!wejoker辅助器... 曝光透视!wejoker辅助器要钱玩吗,微乐四川亲友圈辅助器,课程教程(有挂规律)-哔哩哔哩1.we...
揭露透视!佛手大菠萝13道挂哪... 揭露透视!佛手大菠萝13道挂哪里,微乐小程序破解版修改器,要领教程(有挂工具)-哔哩哔哩佛手大菠萝1...
曝光透视!智星德州插件2024... 曝光透视!智星德州插件2024最新版,微乐小程序自建房辅助下载,指南教程(有挂分析)-哔哩哔哩1、下...
解迷透视!pokermaste... 解迷透视!pokermaster破解版,微信微乐小程序辅助软件,办法教程(有挂方针)-哔哩哔哩pok...
科普透视!约局吧游戏挂,微乐广... 科普透视!约局吧游戏挂,微乐广西麻辣辅助,指引教程(新版有挂)-哔哩哔哩1、玩家可以在约局吧游戏挂线...
详细透视!拱趴大菠萝机器人,微... 详细透视!拱趴大菠萝机器人,微乐自建房辅助软件如何下载,讲义教程(竟然有挂)-哔哩哔哩1)拱趴大菠萝...