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

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

相关内容

热门资讯

今日百科"山西扣点有... 今日百科"山西扣点有没有辅助器"开挂(透视)werplan怎么作弊(有挂猫腻);1、很好的工具软件,...
推荐一款"丰城双剑辅... 推荐一款"丰城双剑辅助"开挂(透视)wpk软件是真的吗(有挂技巧);亲真的是有正版授权,小编(透视辅...
终于懂了"微信后羿大... 终于懂了"微信后羿大厅辅助"开挂(透视)约局吧辅助器(有挂技术);AI辅助机器人普及解说快速成为一个...
一起来讨论"七千在线... 【福星临门,好运相随】;一起来讨论"七千在线辅助"开挂(透视)wepoker辅助是真的吗(今日头条)...
热点推荐"微信小程序... 热点推荐"微信小程序功夫川辅助"开挂(透视)hhpoker辅助器视频(有挂猫腻);1、不需要AI权限...
玩家必备教程"哈糖大... 玩家必备教程"哈糖大菠萝破解器"开挂(透视)sohoo辅助(有挂分析);简单的透视挂控制,仅凭一根手...
透视脚本"广西老友玩... 透视脚本"广西老友玩有破解视频"开挂(透视)pokemmo脚本最新版(证实有挂);广西老友玩有破解视...
一分钟了解"新道游辅... 一分钟了解"新道游辅助器免费版"开挂(透视)约局吧德州真的有透视挂吗(有挂攻略);1、很好的新道游辅...
免费测试版"新道游拼... 【福星临门,好运相随】;免费测试版"新道游拼十辅助器"开挂(透视)如何下载德普之星辅助软件(有挂教程...
最新通报"微乐小程游... 最新通报"微乐小程游戏破解器下载"开挂(透视)德普之星有辅助软件吗(有挂秘诀);1、不需要AI权限,...