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 黑科技工具迎来新版本,不用上传文件到云端就能完成微扑克辅助下载解析、 AI 处理、微...
今日重大通报!wepoker辅... 很多人使用云端 AI 时,都担心wepoker辅助器是真的吗、内部资料上传带来隐私风险。这款本地运行...
玩家必看科普!!有哪些免费的w... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
实测交流!wepoker正确养... 实测交流!wepoker正确养号方法,德普之星透视神器脚本(原本真的有开挂)AI 智能体黑科技再上新...
一分钟揭秘!wpk透视插件,W... 一分钟揭秘!wpk透视插件,WPK开挂脚本下载(一向是真的有辅助器)AI 智能体黑科技再上新,自动化...
研究成果!wepoker开脚本... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker开脚本视频解析、 AI...
重大科普!wepoker私人局... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
每日必看推荐!wepoker私... 很多人使用云端 AI 时,都担心wepoker私人局怎么玩、内部资料上传带来隐私风险。这款本地运行的...
发现玩家!wpk刷入池率脚本,... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wpk刷入池率脚本解析、 AI 处理...
教程攻略!wepoker辅助软... 教程攻略!wepoker辅助软件价格,WPK有挂透视吗软件(原先存在有透视)AI 智能体黑科技再上新...