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、微乐脚本辅助器破解器简单,微乐脚本辅...
出乎意料的是!四川游戏家园辅助... 出乎意料的是!四川游戏家园辅助界面!一直是真的辅助下载(有挂解惑)-哔哩哔哩1.四川游戏家园辅助界面...
日前!哈局十三张通用辅助!切实... 日前!哈局十三张通用辅助!切实真的是有辅助工具(有挂细节)-哔哩哔哩1、在哈局十三张通用辅助插件功能...
记者获悉!旺旺福建辅助!总是有... 记者获悉!旺旺福建辅助!总是有挂辅助工具(有挂详细)-哔哩哔哩1、游戏颠覆性的策略玩法,独创攻略技巧...
相较于以往!龙岩闲游安卓脚本!... 您好,龙岩闲游安卓脚本这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
此事备受玩家关注!爱玩联盟辅助... 此事备受玩家关注!爱玩联盟辅助!果然有挂辅助安装(有挂辅助)-哔哩哔哩爱玩联盟辅助破解侠是真的助透视...
事发当天!上饶打炸辅助!好像是... 事发当天!上饶打炸辅助!好像是真的辅助下载(发现有挂)-哔哩哔哩1、打开软件启动之后找到中间准星的标...
有了最新消息!湘竞技辅助!本来... 有了最新消息!湘竞技辅助!本来真的是有辅助修改器(有挂细节)-哔哩哔哩1、每一步都需要思考,不同水平...
据文件显示!拱趴大菠萝调胜率!... 据文件显示!拱趴大菠萝调胜率!本来是真的辅助app(详细教程)-哔哩哔哩1、下载好拱趴大菠萝调胜率透...
据玩家消息!火神辅助免费下载!... 据玩家消息!火神辅助免费下载!真是真的是有辅助平台(有挂技巧)-哔哩哔哩1、火神辅助免费下载模拟器是...