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透视外挂,(... 透视透视挂!wpk透视外挂,(wpK)一直真的是有挂,黑科技实锤(有挂透视)-哔哩哔哩;运wpk透视...
黑科技透视!(WePoKe)原... 黑科技透视!(WePoKe)原先是真的有挂,(wePokE)原来存在有挂,黑科技针对(真是有挂)-哔...
十分钟黑科技!德扑之星记牌器,... 十分钟黑科技!德扑之星记牌器,(德扑之星)往昔是有挂,科技教程(有挂教程)-哔哩哔哩;该软件可以轻松...
透视美元局(AAPOKer)透... 透视美元局(AAPOKer)透视辅助脚本(透视)好像存在有挂(有挂详情)-哔哩哔哩1、ai辅助优化,...
第四分钟辅助挂!微扑克ai辅助... 第四分钟辅助挂!微扑克ai辅助透明挂,(微扑克)都是有挂,是真的有挂(有挂猫腻)-哔哩哔哩1、不需要...
黑科技代打!(WePoKe)从... 黑科技代打!(WePoKe)从来真的有挂,(WEPOke)原来存在有挂,黑科技大厅房(揭秘有挂)-哔...
黑科技辅助!(WePoKe)一... 黑科技辅助!(WePoKe)一贯是有挂,(wepoKe)原来真的有挂,黑科技辅助(有挂秘诀)-哔哩哔...
黑科技有挂!(WePoKe)真... 黑科技有挂!(WePoKe)真是有挂,(WEpoke)原来存在有挂,黑科技苹果版(有挂工具)-哔哩哔...
透视科技(AaPOKER)透视... 透视科技(AaPOKER)透视辅助插件(透视)本来是真的有挂(讲解有挂)-哔哩哔哩1、这是跨平台的黑...
透视安装(aapoKer)透视... 透视安装(aapoKer)透视辅助脚本(透视)最初是有挂(有挂分析)-哔哩哔哩1、很好的工具软件,可...