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

相关内容

热门资讯

透视揭露!wepoker开辅助... 透视揭露!wepoker开辅助能查到吗,wepoker有插件吗-一贯一直总是有辅助攻略(哔哩哔哩)1...
透视分享!wpk辅助插件,wp... 透视分享!wpk辅助插件,wpk有那种辅助吗-原来一直总是有辅助工具(哔哩哔哩)1、超多福利:超高返...
透视科普!wepoker免费脚... 透视科普!wepoker免费脚本咨询,wepoker辅助透视-其实存在有辅助插件(哔哩哔哩)1、用户...
透视解迷!大菠萝789辅助器下... 透视解迷!大菠萝789辅助器下载,cloudpoker作弊-真是存在有辅助攻略(哔哩哔哩)1、大菠萝...
透视科普!wepoker辅助器... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视必备!wepoker插件辅... 透视必备!wepoker插件辅助,wepoker有透视底牌吗-真是有辅助教程(哔哩哔哩)1、让任何用...
透视必备!pokerworld... 透视必备!pokerworld修改器,悦扑克脚本-本来真的有辅助工具(哔哩哔哩)1、pokerwor...
透视教你!wejoker免费脚... 透视教你!wejoker免费脚本,wepoker插件程序激活码-原来一直总是有辅助插件(哔哩哔哩)1...
透视关于!德普之星有辅助软件吗... 透视关于!德普之星有辅助软件吗,德普之星辅助工具如何设置-一直一直都是有辅助插件(哔哩哔哩)所有人都...
透视解密!wepoker怎么设... 透视解密!wepoker怎么设置透视,wepoker破解游戏盒子-其实是真的有辅助教程(哔哩哔哩);...