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辅助挂多功能透视工具,好像有挂(有挂解密)1、进入到wpk是否有挂之后,能看到左侧胜...
重大消息!wepoker辅助挂... 重大消息!wepoker辅助挂多功能透视工具,都是真的是有挂(竟然有挂)1、wepoker免费辅助多...
一分钟了解!德扑之星辅助挂多功... 一分钟了解!德扑之星辅助挂多功能透视工具,切实有挂(有挂分享)1、德扑之星透视辅助软件激活码多个强度...
必备科技!wepoker辅助挂... 必备科技!wepoker辅助挂多功能透视工具,总是是有挂(有挂解惑);1、金币登录送、破产送、升级送...
一分钟了解!aapoker辅助... 一分钟了解!aapoker辅助挂多功能透视工具,好像真的是有挂(有挂透视)1、下载好aapoker脚...
每日必看推荐!aapoker辅... 每日必看推荐!aapoker辅助挂多功能透视工具,原来真的有挂(确实有挂)1、打开软件启动之后找到中...
推荐一款!智星德州扑克辅助挂多... 推荐一款!智星德州扑克辅助挂多功能透视工具,原来真的有挂(有挂秘诀)1、全新机制【智星德州扑克ai辅...
玩家必看教程!红龙poker辅... 玩家必看教程!红龙poker辅助挂多功能透视工具,一贯有挂(存在有挂)暗藏猫腻,小编详细说明红龙po...
教程攻略!智星德州扑克辅助挂多... 教程攻略!智星德州扑克辅助挂多功能透视工具,果然有挂(有挂头条);1、完成智星德州扑克有辅助插件,帮...
一分钟快速了解!AAPoKer... 您好,AAPoKer这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...