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 类,如下所示:

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

相关内容

热门资讯

四分钟了解!哈局十三张,大赢家... 四分钟了解!哈局十三张,大赢家跑得快辅助,分享教程(有挂脚本)1、大赢家跑得快辅助系统规律教程、大赢...
8分钟了解!星星武汉麻将胡牌技... 8分钟了解!星星武汉麻将胡牌技巧,赣牌圈开挂是真的吗,科技教程(有挂揭秘)1、进入游戏-大厅左侧-新...
4分钟了解!菠萝德州app有挂... 4分钟了解!菠萝德州app有挂吗,新玉海楼茶苑吗,必胜教程(有挂神器)1、在菠萝德州app有挂吗ai...
6分钟了解!微友麻将,浙江游戏... 6分钟了解!微友麻将,浙江游戏大厅有猫腻吗,透视教程(有挂解说)亲,关键说明,浙江游戏大厅有猫腻吗赛...
一分钟了解!斗棋红中胡牌有没有... 一分钟了解!斗棋红中胡牌有没有什么规律,开心十三张有没有挂,2025版教程(有挂技巧);暗藏猫腻,小...
2分钟了解!琼崖海南麻将怎么提... 2分钟了解!琼崖海南麻将怎么提高胜率,福建天天开心王国辅助器,揭秘教程(有挂工具)一、琼崖海南麻将怎...
八分钟了解!乐乐游戏辅助器,众... 八分钟了解!乐乐游戏辅助器,众乐联盟有挂吗,可靠教程(有挂透视)1、全新机制【众乐联盟有挂吗软件透明...
一分钟了解!雀神麻将辅牌器购买... 一分钟了解!雀神麻将辅牌器购买,微信随意玩9人拼三张辅助器,实用技巧(有挂秘籍)1、用户打开应用后不...
二分钟了解!新华棋牌有没有挂,... 二分钟了解!新华棋牌有没有挂,打小闲川南棋牌为什么总是输,揭秘教程(有挂软件)暗藏猫腻,小编详细说明...
三分钟了解!汇友手游外 挂,边... 三分钟了解!汇友手游外 挂,边锋干瞪眼外挂效果,必备教程(有挂软件)进入游戏-大厅左侧-新手福利-激...