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

相关内容

热门资讯

重大科普外挂!新纪元娱乐外挂多... 重大科普外挂!新纪元娱乐外挂多功能AI插件,新纪元娱乐确实真的有外挂插件运新纪元娱乐辅助工具,进入游...
我来教教大家作弊器!新余五十k... 我来教教大家作弊器!新余五十k作弊器多功能AI攻略,新余五十k总是有作弊器攻略所有人都在同一条线上,...
我来教大家透视挂!kk互娱透视... 我来教大家透视挂!kk互娱透视挂多功能AI脚本,kk互娱本来是真的有透视挂脚本进入游戏-大厅左侧-新...
重磅来袭开挂!新上游开挂多功能... 重磅来袭开挂!新上游开挂多功能AI教程,新上游好像是有开挂教程新上游能透视中分为三种模型:新上游模拟...
科技通报辅助!海象互娱辅助多功... 科技通报辅助!海象互娱辅助多功能AI工具,海象互娱一直真的是有辅助工具1、玩家可以在海象互娱透视最简...
玩家爆料作弊器!千秋互娱作弊器... 玩家爆料作弊器!千秋互娱作弊器多功能AI工具,千秋互娱都是有作弊器工具1、让任何用户在无需千秋互娱安...
最新研发开挂!火鸟大厅开挂多功... 最新研发开挂!火鸟大厅开挂多功能AI方法,火鸟大厅本来存在有开挂方法1、玩家可以在火鸟大厅线上大神俱...
分享认知开挂!圣游大厅开挂多功... 分享认知开挂!圣游大厅开挂多功能AI教程,圣游大厅确实真的是有开挂教程1、在圣游大厅插件功能辅助器技...
大家学习交流外挂!新玄龙外挂多... 大家学习交流外挂!新玄龙外挂多功能AI插件,新玄龙都是真的有外挂插件1、完成新玄龙有辅助插件,帮助玩...
推荐一款作弊器!长城互动娱乐作... 您好,长城互动娱乐这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...