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

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

相关内容

热门资讯

热点讨论“荔枝竞技辅助”原生有... 荔枝竞技辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,荔枝竞技辅助作为一种经典的娱乐方式,吸引...
分享一款“小闲54辅助”原本有... 您好:这款小闲54辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款小闲54辅助游戏中打牌都会发现很...
免费测试版“网易亲友圈辅助”原... >>您好:网易亲友圈辅助确实是有挂的,很多玩家在这款网易亲友圈辅助游戏中打牌都会发现很多用户的牌特别...
实测交流“wejoker透视方... 实测交流“wejoker透视方法”先前有辅助工具(有人有挂);打开点击测试直接进入微信(136704...
发现玩家“闲玩暗宝辅助软件”一... 发现玩家“闲玩暗宝辅助软件”一向有辅助开挂平台(揭秘有挂);无需打开直接搜索加薇136704302(...
透视好友房“吉祥填大坑游戏攻略... 透视好友房“吉祥填大坑游戏攻略”原先有辅助开挂工具(有挂辅助) 了解更多开挂安装加(13670430...
一秒答解“微信大厅辅助挂”从来... 一秒答解“微信大厅辅助挂”从来有开挂辅助安装(有挂分享);无需打开直接搜索薇:136704302 咨...
一分钟揭秘“衢州都莱辅助器是真... 一分钟揭秘“衢州都莱辅助器是真是假”先前有辅助开挂脚本(有挂辅助)1、下载安装好衢州都莱辅助器是真是...
科技新动态“新佛手在线辅助软件... 科技新动态“新佛手在线辅助软件”一向有开挂辅助器(有挂教学) 了解更多开挂安装加(136704302...
推荐攻略“上饶中至脚本修改”一... 您好:上饶中至脚本修改这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...