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

相关内容

热门资讯

黑科技脚本!wpk ai辅助,... 一、微扑克如何让系统发好牌简介了解软件请加微:136704302微扑克如何让系统发好牌是一款在线扑克...
黑科技科技!wpk有猫腻吗(黑... 黑科技科技!wpk有猫腻吗(黑科技)起初真的有挂(有挂透明挂)-哔哩哔哩;1分钟了解详细教程(微信 ...
黑科技挂!红龙扑克透牌辅助器,... 黑科技挂!红龙扑克透牌辅助器,wepoke有挂吗蓝鸟,2025新版技巧(有挂总结)-哔哩哔哩;人气非...
黑科技实锤!德扑自定义房间(透... 黑科技实锤!德扑自定义房间(透明挂)好像有挂(有挂AI)-哔哩哔哩关于德扑自定义房间机制的,其中提到...
黑科技神器!德州之星有没有假,... 黑科技神器!德州之星有没有假,wpk透视辅助测试,力荐教程(有挂规律)-哔哩哔哩(1)黑科技神器!德...
黑科技讲解!wpk外挂是真的还... 1、黑科技讲解!wpk外挂是真的还是(黑科技)原先真的是有挂(有挂安卓版本)-哔哩哔哩;详细教程。2...
黑科技辅助挂!wepoke智能... 黑科技辅助挂!wepoke智能ai,德扑ai软件购买,必胜教程(有挂技巧)-哔哩哔哩是一款可以让一直...
黑科技游戏!来玩德州app辅助... 黑科技游戏!来玩德州app辅助工具(ai辅助)起初是有挂(有挂存在)-哔哩哔哩;支持多人共享记分板与...
黑科技插件!德州之星有辅助挂,... 黑科技插件!德州之星有辅助挂,wpk俱乐部,规律教程(有挂总结)-哔哩哔哩是一款可以让一直输的玩家,...
黑科技免费!wepoke软件透... 黑科技免费!wepoke软件透明挂下载(透明挂)先前是真的有挂(有挂后台)-哔哩哔哩;最新版2024...