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

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

相关内容

热门资讯

十分钟了解!微信老铁13水辅助... 十分钟了解!微信老铁13水辅助,凑一桌游戏免费辅助器(总是是真的挂)-哔哩哔哩1、凑一桌游戏免费辅助...
2分钟解谜!爱玩联盟辅助下载,... 2分钟解谜!爱玩联盟辅助下载,浙江游戏大厅脚本软件(其实是有挂)-哔哩哔哩1、很好的工具软件,可以解...
第一分钟关于!全民内蒙古辅助器... 第一分钟关于!全民内蒙古辅助器,宁夏划水辅助(一贯有挂)-哔哩哔哩全民内蒙古辅助器辅助器中分为三种模...
第1分钟关于!功夫川嘛辅助器是... 第1分钟关于!功夫川嘛辅助器是真的假的,瓜瓜丰城双剑旧版攻略(都是真的有挂)-哔哩哔哩1、功夫川嘛辅...
七分钟必备!胡乐安庆辅助工具,... 七分钟必备!胡乐安庆辅助工具,丰城呱呱辅助器(切实有挂)-哔哩哔哩1、全新机制【胡乐安庆辅助工具软件...
七分钟推荐!新道游修改器,顺欣... 七分钟推荐!新道游修改器,顺欣茶楼开挂(都是真的有挂)-哔哩哔哩一、顺欣茶楼开挂软件透明挂的定义与意...
十分钟揭露!边锋老友内蒙古辅助... 十分钟揭露!边锋老友内蒙古辅助,丫丫老陕开挂(真是有挂)-哔哩哔哩1、十分钟揭露!边锋老友内蒙古辅助...
第四分钟专业!对战互娱有辅助吗... 第四分钟专业!对战互娱有辅助吗,小程序牵手跑得有开挂吗(都是真的有挂)-哔哩哔哩小程序牵手跑得有开挂...
第三分钟揭露!财神十三张脚本效... 第三分钟揭露!财神十三张脚本效果图,天天爱柳州辅助器(真是是有挂)-哔哩哔哩第三分钟揭露!财神十三张...
第1分钟分享!赣湘互娱透视挂,... 第1分钟分享!赣湘互娱透视挂,蜜瓜大厅辅助下载(真是是真的挂)-哔哩哔哩1、下载好蜜瓜大厅辅助下载辅...