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

相关内容

热门资讯

必备科技!微信小程序微乐辅助器... 必备科技!微信小程序微乐辅助器,老友麻将开挂辅助器,详细神器(有挂总结)1、让任何用户在无需微信小程...
玩家必看教程!jj斗地主麻将外... 玩家必看教程!jj斗地主麻将外挂,欢乐达人正版脚本,详细软件(有挂详细)1、完成欢乐达人正版脚本辅助...
详细说明!微乐河南小程序微乐辅... 详细说明!微乐河南小程序微乐辅助脚本,蛮王大厅辅助插件,详细工具(今日头条)该软件可以轻松地帮助玩家...
专业讨论!传送屋软件辅助,皮皮... 专业讨论!传送屋软件辅助,皮皮透视辅助软件工具,详细软件(有挂秘诀)1、超多福利:超高返利,海量正版...
查到实测辅助!途游休闲铺破解版... 查到实测辅助!途游休闲铺破解版,789大菠萝可以控制吗,详细挂(有挂技巧)该软件可以轻松地帮助玩家将...
盘点几款!奇迹掌趣辅助,科乐填... 盘点几款!奇迹掌趣辅助,科乐填大坑破解版安卓,详细工具(真是有挂)1、完成科乐填大坑破解版安卓有辅助...
避坑细节!悟空大厅辅助器,广东... 您好,广东闲来辅助视频这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
今日头条!打两圈辅助,边锋辅助... 今日头条!打两圈辅助,边锋辅助器小程序微信,详细器(有挂功能)1、边锋辅助器小程序微信免费辅助多个强...
推荐十款!八闽状元郎竹间茶馆辅... 推荐十款!八闽状元郎竹间茶馆辅助,wepoker专用辅助,详细app(确实有挂)1、游戏颠覆性的策略...
分辨真假!杭州都莱有没有挂,花... 分辨真假!杭州都莱有没有挂,花城棋舍辅助,详细神器(发现有挂)1、这是跨平台的杭州都莱有没有挂轻量版...