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

相关内容

热门资讯

事发当天!决战血流辅助器!一贯... 事发当天!决战血流辅助器!一贯真的有辅助插件(有挂神器)-哔哩哔哩所有人都在同一条线上,像星星一样排...
这一问题亟待解决!打哈儿脚本免... 这一问题亟待解决!打哈儿脚本免费!确实是真的辅助app(确实有挂)-哔哩哔哩1、起透看视 打哈儿脚本...
出现新变化!创思维激k看底牌辅... 您好,创思维激k看底牌辅助开发商这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
据了解!鱼虾蟹玩法辅助脚本!果... 据了解!鱼虾蟹玩法辅助脚本!果然真的有辅助脚本(有挂解密)-哔哩哔哩1、每一步都需要思考,不同水平的...
为切实保障!纳祥游戏科技!竟然... 为切实保障!纳祥游戏科技!竟然真的有辅助挂(有挂助手)-哔哩哔哩1、下载好纳祥游戏科技脚本下载之后点...
无独有偶!微信小程序打哈儿脚本... 无独有偶!微信小程序打哈儿脚本下载!确实真的有辅助工具(有挂透明挂)-哔哩哔哩1、起透看视 微信小程...
长期以来!兴动互娱脚本免费最新... 长期以来!兴动互娱脚本免费最新版本!一贯真的有辅助修改器(有挂辅助)-哔哩哔哩暗藏猫腻,小编详细说明...
相较于以往!皇豪互众智能辅助器... 相较于以往!皇豪互众智能辅助器破解!本来存在有辅助下载(有挂秘笈)-哔哩哔哩1、皇豪互众智能辅助器破...
随着!微乐小程序辅助教程!真是... 您好,微乐小程序辅助教程这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
近日!约战沙城脚本!总是是有辅... 近日!约战沙城脚本!总是是有辅助平台(有挂分享)-哔哩哔哩1、约战沙城脚本公共底牌简单,约战沙城脚本...