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

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

相关内容

热门资讯

开挂透视!aa poker辅助... 开挂透视!aa poker辅助包,微乐家乡小程序脚本,方案教程(有挂秘籍)-哔哩哔哩1、下载好aa ...
教你透视!wepoker有没有... 教你透视!wepoker有没有透视方法,微信小程序怎么开挂,大纲教程(有挂透视)-哔哩哔哩1、许多玩...
普及透视!wpk透视是真的假的... 普及透视!wpk透视是真的假的,微信小程序游戏破解器,操作教程(果真有挂)-哔哩哔哩1、完成wpk透...
必备透视!德州私人局脚本,微乐... 必备透视!德州私人局脚本,微乐家乡小程序自建房辅助app,练习教程(有挂秘籍)-哔哩哔哩一、德州私人...
辅助透视!wepoker私人定... 辅助透视!wepoker私人定制透视,微乐麻辣自建房辅助视频,手筋教程(有挂攻略)-哔哩哔哩1、起透...
辅助透视!wepoker私人局... 辅助透视!wepoker私人局外卦,微乐小程序黑科技免费知乎,窍门教程(有挂存在)-哔哩哔哩wepo...
开挂透视!pokemmo脚本辅... 开挂透视!pokemmo脚本辅助,如何下载微乐自建房免费黑科技,讲义教程(有挂辅助)-哔哩哔哩1、让...
曝光透视!大菠萝免费辅助,微乐... 曝光透视!大菠萝免费辅助,微乐自建房脚本下载,要领教程(真的有挂)-哔哩哔哩1、任何大菠萝免费辅助透...
辅助透视!德普之星透视辅助软件... 辅助透视!德普之星透视辅助软件下载,微乐小游戏万能修改器,手段教程(有挂分享)-哔哩哔哩1)德普之星...
曝光透视!wepoker有没有... 曝光透视!wepoker有没有透视方法,微信小程序多乐辅助器,总结教程(有挂方针)-哔哩哔哩1、在w...