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

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

相关内容

热门资讯

据了解!佛手在线十三道辅助器(... 据了解!佛手在线十三道辅助器(辅助挂)原来有挂(有挂手筋)-哔哩哔哩;无需打开直接搜索薇:13670...
第7分钟开挂!浙江同城游辅助器... 第7分钟开挂!浙江同城游辅助器,新畅游互娱辅助app,切实教程-2026最新版本1、新畅游互娱辅助a...
截至目前!中至九江黑科技app... 截至目前!中至九江黑科技app怎么下载(辅助挂)其实真的有挂(有挂总结)-哔哩哔哩>>您好:软件加薇...
第六分钟开挂!拱趴大菠萝有辅助... 您好,拱趴大菠萝有辅助吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
截至目前!四川游戏家园破解(辅... 截至目前!四川游戏家园破解(辅助挂)原来真的有挂(有挂手筋)-哔哩哔哩>>您好:软件加薇136704...
在玩家背景下!拱趴大菠萝卦(辅... 在玩家背景下!拱趴大菠萝卦(辅助挂)一直真的有挂(有挂方案)-哔哩哔哩;无需打开直接搜索加(薇:13...
九分钟开挂!湛江吴川0759程... 九分钟开挂!湛江吴川0759程序,天天爱柳州辅助器,揭秘教程-2026最新版本1、玩家可以在天天爱柳...
据相关数据显示!熟客温州游戏辅... 据相关数据显示!熟客温州游戏辅助器(辅助挂)原来确实有挂(有挂资料)-哔哩哔哩;亲,熟客温州游戏辅助...
第十分钟开挂!衢州都莱有没有挂... 第十分钟开挂!衢州都莱有没有挂,道游互娱辅助,必胜教程-2026最新版本所有人都在同一条线上,像星星...
网友热议!皮皮跑子辅助(辅助挂... 皮皮跑子辅助是一款专注玩家量身打造的游戏记牌类型软件,在皮皮跑子辅助这款游戏中我们可以记录下每张牌的...