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、超多福利:超高...
外挂经验!德州局怎么透视,拱趴... 外挂经验!德州局怎么透视,拱趴大菠萝怎么开挂-一贯是真的有辅助插件(哔哩哔哩)外挂经验!德州局怎么透...
外挂要领!大菠萝辅助器,哈糖大... 外挂要领!大菠萝辅助器,哈糖大菠萝有挂吗5个常用方法-切实有辅助app(哔哩哔哩)一、哈糖大菠萝有挂...
外挂法门!如何下载德普之星辅助... 外挂法门!如何下载德普之星辅助软件,竞技联盟破解版最新版-切实一直都是有辅助技巧(哔哩哔哩)1、上手...
外挂攻略!拱趴大菠萝辅助神器,... 外挂攻略!拱趴大菠萝辅助神器,德普之星辅助工具如何打开-果然是有辅助插件(哔哩哔哩)1、用户打开应用...
外挂法子!werplan怎么作... 外挂法子!werplan怎么作必弊,impoker辅助-一直有辅助攻略(哔哩哔哩)所有人都在同一条线...
外挂要领!德州透视脚本,佛手在... 外挂要领!德州透视脚本,佛手在线大菠萝技巧-原来真的有辅助方法(哔哩哔哩)暗藏猫腻,小编详细说明佛手...
外挂策略!steampoker... 外挂策略!steampokermaster辅助,德扑圈透视-一直是有辅助攻略(哔哩哔哩)1、超多福利...
外挂模板!红龙poker辅助工... 外挂模板!红龙poker辅助工具,约局吧app有挂吗-一直一直都是有辅助工具(哔哩哔哩)1、在红龙p...
外挂秘籍!聚星ai辅助工具收费... 外挂秘籍!聚星ai辅助工具收费多少,德普之星透视软件免费入口官网-一直有辅助方法(哔哩哔哩)1、完成...