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

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

相关内容

热门资讯

透视项目!wepoker私局代... 透视项目!wepoker私局代打,WePoKer线上一直真的有脚本,第2分钟教程(有挂规律)在进入w...
透视教程书!aapoker辅助... 透视教程书!aapoker辅助软件合法吗,AApoker软件原来是真的有开挂,3分钟教程(确实有挂)...
透视演示!如何下载wpk透视版... 透视演示!如何下载wpk透视版,WPK安卓一贯存在有工具,第六分钟教程(有挂详细)1、上手简单,内置...
透视举措!aapoker俱乐部... 透视举措!aapoker俱乐部靠谱吗,AApoker发牌逻辑其实是有神器,六分钟教程(有挂技术)1、...
透视步骤!聚星ai辅助工具激活... 透视步骤!聚星ai辅助工具激活码,WePoKer透视底牌一直有工具,第6分钟教程(有挂方法)1、聚星...
透视妙招!德州hhpoker是... 透视妙招!德州hhpoker是真的吗,HHpoker开挂本来是真的有教程,九分钟教程(确实有挂)1、...
透视方式!德普之星透视免费,德... 透视方式!德普之星透视免费,德普之星埋牌原来真的是有透视,第三分钟教程(有挂秘籍)1、每一步都需要思...
透视大纲!德普之星有透视辅助吗... 透视大纲!德普之星有透视辅助吗,德普之星介绍竟然真的是有开挂,第九分钟教程(有挂教程)1、完成德普之...
透视模块!aapoker怎么设... 透视模块!aapoker怎么设置提高好牌几率,AApoker工具切实有脚本,第5分钟教程(确实有挂)...
透视方式!wpk刷入池率脚本,... 透视方式!wpk刷入池率脚本,WPK链接好像存在有辅助,第八分钟教程(有挂猫腻)1)wpk刷入池率脚...