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

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

相关内容

热门资讯

记者发布辅助器插件!趣玩齐齐哈... 记者发布辅助器插件!趣玩齐齐哈尔多功能辅助器插件,趣玩齐齐哈尔竟然真的是有辅助器插件1)趣玩齐齐哈尔...
我来教教大家辅助app!营口9... 我来教教大家辅助app!营口92麻将多功能辅助app,营口92麻将都是是真的有辅助app1、完成营口...
揭秘一下开挂技巧!掌上约牌多功... 揭秘一下开挂技巧!掌上约牌多功能开挂技巧,掌上约牌一贯真的是有开挂技巧1、掌上约牌模拟器是什么优化,...
技术分享外挂技巧!响水麻将多功... 技术分享外挂技巧!响水麻将多功能外挂技巧,响水麻将真是真的是有外挂技巧在进入响水麻将软件靠谱后,参与...
推荐几款新版作弊器技巧!响水麻... 推荐几款新版作弊器技巧!响水麻将多功能作弊器技巧,响水麻将都是是有作弊器技巧所有人都在同一条线上,像...
重要通知辅助app!乐通多功能... 重要通知辅助app!乐通多功能辅助app,乐通一直真的是有辅助app1、首先打开乐通辅助器下载最新版...
新手必备辅助器插件!朱雀吴江三... 新手必备辅助器插件!朱雀吴江三缺一多功能辅助器插件,朱雀吴江三缺一切实是有辅助器插件1、朱雀吴江三缺...
关于辅助器方法!澳门永利集团多... 关于辅助器方法!澳门永利集团多功能辅助器方法,澳门永利集团其实存在有辅助器方法1、在澳门永利集团插件...
一分钟快速了解辅助器app!乐... 一分钟快速了解辅助器app!乐夏多功能辅助器app,乐夏一贯存在有辅助器app1、起透看视 乐夏辅助...
免费测试版透视挂攻略!欢乐赣游... 免费测试版透视挂攻略!欢乐赣游GZ多功能透视挂攻略,欢乐赣游GZ竟然存在有透视挂攻略1.欢乐赣游GZ...