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

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

相关内容

热门资讯

外挂模板!红龙poker辅助工... 外挂模板!红龙poker辅助工具,约局吧app有挂吗-一直一直都是有辅助工具(哔哩哔哩)1、在红龙p...
外挂秘籍!聚星ai辅助工具收费... 外挂秘籍!聚星ai辅助工具收费多少,德普之星透视软件免费入口官网-一直有辅助方法(哔哩哔哩)1、完成...
外挂模板!竞技联盟透视,聚星a... 外挂模板!竞技联盟透视,聚星ai辅助工具收费多少-果然存在有辅助app(哔哩哔哩)1、在聚星ai辅助...
外挂绝活!来玩app破解版,哈... 外挂绝活!来玩app破解版,哈糖大菠萝破解器-真是有辅助神器(哔哩哔哩)1、哈糖大菠萝破解器免费辅助...
外挂学习!来玩app破解,we... 外挂学习!来玩app破解,werplan有挂吗-都是有辅助工具(哔哩哔哩)1、werplan有挂吗辅...
外挂办法!如何下载德普之星辅助... 外挂办法!如何下载德普之星辅助软件,德普之星辅助软件-好像真的是有辅助工具(哔哩哔哩)辅助器是一种具...
外挂总结!werplan免费挂... 外挂总结!werplan免费挂下载,xpoker怎么作必弊-确实存在有辅助app(哔哩哔哩)1、we...
外挂攻略!pokerworld... 您好,拱趴大菠萝怎么开挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
外挂阶段!云扑克有透视吗,德普... 外挂阶段!云扑克有透视吗,德普辅助器怎么用-好像一直都是有辅助教程(哔哩哔哩)1、游戏颠覆性的策略玩...
外挂方式!聚星ai辅助工具激活... 外挂方式!聚星ai辅助工具激活码,德普之星透视辅助软件-总是真的有辅助教程(哔哩哔哩)1、下载好聚星...