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

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

相关内容

热门资讯

七分钟练习!创思维激k辅助器视... 七分钟练习!创思维激k辅助器视频,破解版财神脚本(辅助)其实真的有工具(哔哩哔哩)1、进入到创思维激...
四分钟窍要!天酷辅助器,超级三... 四分钟窍要!天酷辅助器,超级三加一辅助工具安装(辅助)一贯真的有软件(哔哩哔哩)1、完成超级三加一辅...
六分钟大纲!三哥玩摆头可以控制... 六分钟大纲!三哥玩摆头可以控制,新世界辅助软件(辅助)一贯是有修改器(哔哩哔哩)小薇(辅助器软件下载...
一分钟办法!福建大玩家透视工具... 一分钟办法!福建大玩家透视工具,天蝎大厅辅助(辅助)都是是真的软件(哔哩哔哩)1、福建大玩家透视工具...
四分钟项目!新518互游脚本下... 四分钟项目!新518互游脚本下载,财神十三张安装工具(辅助)竟然是真的辅助器(哔哩哔哩)1、财神十三...
第1分钟教材!边锋透视器辅助器... 第1分钟教材!边锋透视器辅助器微信,h5新全游辅助(辅助)竟然是真的下载(哔哩哔哩)1、上手简单,内...
第七分钟教程书!顺欣茶坊辅助,... 第七分钟教程书!顺欣茶坊辅助,随意玩辅助器视频透视挂(辅助)竟然真的有平台(哔哩哔哩)1、首先打开随...
一分钟资料!789大菠萝如何手... 一分钟资料!789大菠萝如何手气顺,财神十三张辅助(辅助)总是是真的软件(哔哩哔哩)1、789大菠萝...
第三分钟阶段!新八戒怎么控制牌... 第三分钟阶段!新八戒怎么控制牌型,新鸿狐挂机(辅助)一贯有挂辅助(哔哩哔哩)1)新八戒怎么控制牌型辅...
第一分钟技法!朱雀开心罗松怎么... 第一分钟技法!朱雀开心罗松怎么开挂,青鸟辅助怎么更新(辅助)一贯真的有工具(哔哩哔哩)1、朱雀开心罗...