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

相关内容

热门资讯

透视安卓版"hhpk... 透视安卓版"hhpkoer辅助器"详细辅助安装教程(本然有挂);原来确实真的有挂(需添加指定薇136...
推荐一款!pokemmo手机脚... 推荐一款!pokemmo手机脚本,wepoker怎么拿到好牌,分享教程(有挂软件)1、不需要AI权限...
透视工具"wepok... 透视工具"wepoker私人局可以透视"详细辅助必赢教程(果然真的是有挂),支持语音通讯、好友开房及...
必备辅助推荐!pokemmo脚... 必备辅助推荐!pokemmo脚本辅助器下载,aapoker万能辅助器,力荐教程(有挂透视);实战中需...
透视透视"wepok... 透视透视"wepoker辅助器有哪些功能"详细辅助辅助教程(往昔真的是有挂)1、玩家可以在wepok...
每日必看推荐!hhpoker透... 每日必看推荐!hhpoker透视方法,德扑圈透视挂,曝光教程(有挂软件);实战中需综合运用上述技巧,...
透视总结"wpk显示... 透视总结"wpk显示有作弊"详细辅助2025版教程(最初有挂)科技教程也叫必备教程,这是一款功能强大...
最新技巧!wpk透视插件,德普... 最新技巧!wpk透视插件,德普之星透视辅助软件是真的吗,透明挂教程(有挂神器)这是由厦门游乐互动科技...
透视科技"wepok... 透视科技"wepoker透视苹果系统"详细辅助辅助教程(本然是有挂)1、玩家可以在wepoker透视...
最新技巧!wepoker底牌透... 最新技巧!wepoker底牌透视脚本,wepoker透视破解版,可靠教程(有挂辅助);大神普及一款德...