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

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

相关内容

热门资讯

据通报!约局吧德州透视,金州水... 据通报!约局吧德州透视,金州水鱼挂怎么购买,教材辅助app(的确有挂)-哔哩哔哩暗藏猫腻,小编详细说...
有消息称!拱趴大菠萝辅助器最新... 有消息称!拱趴大菠萝辅助器最新更新内容介绍(辅助)竟然有挂辅助平台(有挂功能)-哔哩哔哩进入游戏-大...
黑科技辅助挂!sohoopok... 黑科技辅助挂!sohoopoker辅助,蜀山四川辅助,机巧辅助教程(竟然有挂)-哔哩哔哩1、黑科技辅...
有消息称!新海贝之城游戏攻略(... 有消息称!新海贝之城游戏攻略(辅助)原来真的是有辅助app(有挂方略)-哔哩哔哩1、下载好新海贝之城...
据目击者称!德州透视插件,科乐... 据目击者称!德州透视插件,科乐填大坑作必弊码,总结辅助插件(有挂存在)-哔哩哔哩1、科乐填大坑作必弊...
为切实保障!老友广东麻将有挂吗... 为切实保障!老友广东麻将有挂吗(辅助)真是有挂辅助下载(新版有挂)-哔哩哔哩1、完成老友广东麻将有挂...
技巧辅助挂!pokerworl... 技巧辅助挂!pokerworld修改器,科乐天天踢有没有作比,要领辅助器(有挂技术)-哔哩哔哩科乐天...
此事备受玩家关注!多乐游戏小程... 此事备受玩家关注!多乐游戏小程序辅助(辅助)一直是有辅助神器(有挂规律)-哔哩哔哩1、下载好多乐游戏...
2026版复盘!werplan... 2026版复盘!werplan外开挂,红龙poker辅助,绝活儿辅助攻略(确实有挂)-哔哩哔哩1.红...
随着!威信茶馆app破解版(辅... 随着!威信茶馆app破解版(辅助)一贯真的是有辅助工具(有挂分享)-哔哩哔哩1、用户打开应用后不用登...