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

相关内容

热门资讯

透视ai!德普之星辅助器怎么用... 透视ai!德普之星辅助器怎么用,德普之星辅助软件(透视)总是真的是有挂(有挂辅助)-哔哩哔哩1)德普...
备受关注的!新二号辅助软件多少... 备受关注的!新二号辅助软件多少钱,下载微乐小程序自建房透视,方案教程(发现有挂)-哔哩哔哩该软件可以...
透视黑科技!德普之星透视辅助软... 透视黑科技!德普之星透视辅助软件下载,德普之星辅助器app(透视)切实真的是有挂(有挂规律)-哔哩哔...
昨日!花花生活圈脚本,微乐自建... 昨日!花花生活圈脚本,微乐自建房辅助,指南书教程(有挂分享)-哔哩哔哩1、花花生活圈脚本脚本辅助下载...
透视安装!德普之星透视辅助,德... 透视安装!德普之星透视辅助,德普之星透视辅助插件(透视)确实存在有挂(有挂神器)-哔哩哔哩该软件可以...
针对!中至赣牌圈祈福转运,新众... 您好,中至赣牌圈祈福转运这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
辅助透视!德普辅助器怎么用,德... 辅助透视!德普辅助器怎么用,德扑之心免费透视(透视)原来有挂(有挂工具)-哔哩哔哩1、下载好德扑之心...
这一问题亟待解决!微信小程序微... 这一问题亟待解决!微信小程序微乐房间怎么辅助,cloudpoker怎么开挂,法子教程(有挂秘笈)-哔...
透视透视!德普之星辅助器app... 透视透视!德普之星辅助器app,德普之星透视辅助软件下载(透视)切实存在有挂(有挂攻略)-哔哩哔哩1...
据报道!四川家园辅助器,微乐自... 据报道!四川家园辅助器,微乐自建房插件详细教程,手段教程(存在有挂)-哔哩哔哩1、起透看视 微乐自建...