AndroidStudio在应用程序未运行时无法接收通知负载
创始人
2024-08-20 12:30:04
0

当应用程序未运行时,Android Studio 无法接受推送通知的负载。要解决此问题,您需要在应用程序中实现 FirebaseMessagingService 类以处理通知负载。在 FirebaseMessagingService 类中,您可以使用 onMessageReceived 方法来接收通知负载并执行所需的操作。以下是一个示例:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMessagingService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    // Get notification payload
    String notificationTitle = remoteMessage.getNotification().getTitle();
    String notificationBody = remoteMessage.getNotification().getBody();

    // Get data payload
    String dataValue1 = remoteMessage.getData().get("key1");
    String dataValue2 = remoteMessage.getData().get("key2");

    // Handle notification
    sendNotification(notificationTitle, notificationBody, dataValue1, dataValue2);
}

private void sendNotification(String notificationTitle, String notificationBody, String dataValue1, String dataValue2) {
    // Build notification
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
            .setContentTitle(notificationTitle)
            .setContentText(notificationBody)
            .setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true);

    // Handle data payload
    if (dataValue1 != null && dataValue2 != null) {
        // Do something with data values
    }

    // Show notification
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(0, notificationBuilder.build());
}

}

您还需要在 AndroidManifest.xml 文件中声明您的 FirebaseMessagingService 类,如下所示:

现在您可以在应用程序未运行时接受推送通知并处理通知负载。

相关内容

热门资讯

避坑细节!微信小程序辅助app... 避坑细节!微信小程序辅助app下载,新上游拼十辅助,解密教程(有挂分析);无需打开直接搜索加薇136...
实测教程!小闲川南辅助,填的那... 实测教程!小闲川南辅助,填的那款辅助视频,专业教程(讲解有挂);无需打开直接搜索加(薇:136704...
重大科普!微信小程序开发,兴动... 重大科普!微信小程序开发,兴动互娱脚本,攻略教程(存在有挂);无需打开直接搜索加(薇:1367043...
盘点一款!!山西扣点子辅助器,... 盘点一款!!山西扣点子辅助器,决战卡五星游戏辅助器,透牌教程(有挂助手);无需打开直接搜索加(薇:1...
必备辅助推荐!边锋干瞪眼小程序... 必备辅助推荐!边锋干瞪眼小程序辅助,对战互娱辅助系统,新2024教程(有挂详细)1、下载安装好边锋干...
玩家必备教程!西部透视辅助,雀... 玩家必备教程!西部透视辅助,雀友会广东潮汕辅助软件,细节揭秘(真的有挂)1、下载安装好雀友会广东潮汕...
每日必看推荐!西兵辅助器,闲来... 每日必看推荐!西兵辅助器,闲来山水广西辅助,专业教程(真实有挂);无需打开直接搜索加(薇:13670...
一分钟揭秘!白金岛辅助怎么设置... 一分钟揭秘!白金岛辅助怎么设置,四川游戏家园通用辅助,透牌教程(有挂教学)1、下载安装好四川游戏家园...
玩家实测!来几局辅助器,爱来大... 玩家实测!来几局辅助器,爱来大菠萝挂,AI教程(有人有挂);无需打开直接搜索加(薇:13670430...
六分钟了解!暗宝破解器,爱玩联... 六分钟了解!暗宝破解器,爱玩联盟辅助下载,解密教程(有挂方法)1、下载安装好爱玩联盟辅助下载,进入游...