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

相关内容

热门资讯

记者揭秘!新版边锋掼蛋辅助器(... 记者揭秘!新版边锋掼蛋辅助器(wEpOke),太坑了原来是真的有挂(有挂攻略)-哔哩哔哩;致您一封信...
6分钟了解(微扑克机器人)外挂... 6分钟了解(微扑克机器人)外挂透明挂辅助神器(ai代打)透视辅助(2024已更新)(哔哩哔哩);微扑...
科技分享(德扑之星开桌)外挂透... 科技分享(德扑之星开桌)外挂透明挂辅助工具(软件)德州ai机器人(2024已更新)(哔哩哔哩)是一款...
推荐攻略!边锋杭麻圈有挂(We... 推荐攻略!边锋杭麻圈有挂(WepokE),太坑了的确真的有挂(有挂教程)-哔哩哔哩;亲真的是有正版授...
四分钟了解!边锋老友棋牌一直输... 四分钟了解!边锋老友棋牌一直输,来玩德州 辅助,详细有挂(有挂攻略)-哔哩哔哩;边锋老友棋牌一直输软...
最新技巧!雀神小程序游戏规则(... 最新技巧!雀神小程序游戏规则(辅助挂)太坑了竟然是真的有挂(有挂教程)-哔哩哔哩;雀神小程序游戏规则...
七分钟了解(wePOke)外挂... 七分钟了解(wePOke)外挂透明挂辅助机制(系统)软件透明挂(2023已更新)(哔哩哔哩);(需添...
重大通报!鄱阳翻精系统发牌机制... 重大通报!鄱阳翻精系统发牌机制(wepoKE),太坑了其实真的有挂(有挂了解)-哔哩哔哩科技教程也叫...
我来教教你!边锋杭麻圈辅助,a... 我来教教你!边锋杭麻圈辅助,appoker透视辅助下载,详细有挂(有挂总结)-哔哩哔哩;边锋杭麻圈辅...
新手必备!广东雀神辅助插件哪里... 新手必备!广东雀神辅助插件哪里买(辅助挂)太坑了确实是真的有挂(有挂教学)-哔哩哔哩;科技详细教程小...