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

相关内容

热门资讯

wepoke辅助插件!wepo... wepoke辅助插件!wepoke存在外挂(辅助挂)原来是真的有挂(详细辅助软件教程);大神普及一款...
德州ai人工智能!微扑克系统发... 德州ai人工智能!微扑克系统发牌(透视辅助)确实是真的有挂(详细辅助黑科技教程);原来确实真的有挂(...
智星德州菠萝外挂!微扑克wpk... 智星德州菠萝外挂!微扑克wpk辅助软件(透视辅助)的确真的有挂(详细辅助透视脚本教程);大神普及一款...
aapoker猫腻!aapok... aapoker猫腻!aapoker俱乐部后天可以操作(黑科技)竟然真的有挂(详细辅助软件教程);《W...
微扑克辅助软件!wpk数据抓包... 微扑克辅助软件!wpk数据抓包(黑科技辅助挂)的确是真的有挂(详细辅助脚本教程);亲,其实确实真的有...
wpk有辅助挂!wepoke是... wpk有辅助挂!wepoke是有软件(黑科技)原来真的有挂(详细透视脚本教程);1.wepoke a...
wpk ai辅助!德扑之星有规... wpk ai辅助!德扑之星有规律(透视辅助)竟然真的有挂(详细辅助透视脚本教程)准备好在 ia的高塔...
wpk提高胜率!众合推扑克辅助... wpk提高胜率!众合推扑克辅助工具(透视)的确真的有挂(详细辅助挂教程);最新版本免费下载安装哦,具...
微扑克ai辅助!wepoke软... 相信很多朋友都在电脑上玩过wepoke吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大家带来...
德州免费辅助神器app!pok... 1、德州免费辅助神器app!pokerx机器人(软件透明该)竟然真的有挂(详细代打辅助教程);详细教...