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

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

相关内容

热门资讯

一分钟了解!乐酷辅助图片(透视... 一分钟了解!乐酷辅助图片(透视)拱趴大菠萝挂(透明教程有挂秘籍);无需打开直接搜索打开薇:13670...
透视软件!椰岛常胜辅助器(透视... 您好:hhpoker软件安装包这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
2024教程!wepoker透... 2024教程!wepoker透视脚本免费(透视)wepoker安装教程(分享教程有挂详细);无需打开...
每日必备!欢乐对决脚本(透视)... 每日必备!欢乐对决脚本(透视)hhpoker辅助软件(德州教程有挂猫腻)您好:hhpoker辅助软件...
查到实测!浙江宝宝游戏辅助器哪... 浙江宝宝游戏辅助器哪里能下载 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四...
玩家必备攻略!广东闲来辅助软件... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
透视智能ai!相约十三张辅助(... wepoker私人局俱乐部怎么进 无需打开直接搜索微信:136704302本司针对手游进行,选择我们...
揭秘几款!如何设置钱塘十三水辅... 揭秘几款!如何设置钱塘十三水辅助器(透视)wepoker有机器人吗(2026新版有挂方针);无需打开...
透视辅助!威胁你小程序雀神挂件... 透视辅助!威胁你小程序雀神挂件(透视)wejoker手机版透视脚本(2026版教程有挂存在) 了解更...
透视教学!欢乐情怀怎么开挂(透... 大家好,今天小编来为大家解答欢乐情怀怎么开挂这个问题咨询软件客服可以免费测试直接加微信(136704...