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

相关内容

热门资讯

一起来探讨透视挂!v湘西辅助透... 一起来探讨透视挂!v湘西辅助透视挂,天天石城麻将存在有外挂,切实有挂总结v湘西辅助器是一种具有地方特...
科技新动态开挂!微乐陕西三带辅... 科技新动态开挂!微乐陕西三带辅助透视挂,怡情互娱是真的有外挂,原来有挂工具微乐陕西三带透视方法中分为...
分辨真假开挂!微乐陕西挖坑辅助... 分辨真假开挂!微乐陕西挖坑辅助透视挂,天天麻将川南真的是有外挂,本来有挂助手微乐陕西挖坑辅助器是一种...
据公告内容开挂!湖南骨牌辅助透... 据公告内容开挂!湖南骨牌辅助透视挂,原味茶馆存在有外挂,其实有挂方法湖南骨牌辅助器是一种具有地方特色...
技术分享辅助!九都乐享辅助透视... 技术分享辅助!九都乐享辅助透视挂,天赐是真的有外挂,一直揭秘有挂小薇(辅助器软件下载)致您一封信;亲...
发现玩家透视挂!渝都麻将辅助透... 发现玩家透视挂!渝都麻将辅助透视挂,情怀南通麻将真的是有外挂,一直有挂攻略1)渝都麻将辅助插件:进一...
推荐攻略透视挂!毳毳辅助透视挂... 推荐攻略透视挂!毳毳辅助透视挂,悦趣竞技真的有外挂,一贯证实有挂1、用户打开应用后不用登录就可以直接...
总算清楚辅助!天庭娱乐辅助透视... 总算清楚辅助!天庭娱乐辅助透视挂,和和阿城麻将真的是有外挂,原来确实有挂1、金币登录送、破产送、升级...
分享实测辅助!新科乐棋牌辅助透... 分享实测辅助!新科乐棋牌辅助透视挂,和和抚远麻将是有外挂,真是有挂细节该软件可以轻松地帮助玩家将新科...
反观辅助!南天门辅助透视挂,闲... 反观辅助!南天门辅助透视挂,闲娱江西棋牌是真的有外挂,好像有挂方法1、闲娱江西棋牌辅助器安装包、闲娱...