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...
一分钟快速了解“wepoker... 您好:wepoker真的能透视吗这款游戏是可以开挂的,软件加【添加微信客服136704302】确实是...
开挂辅助下载"wep... 开挂辅助下载"wepoker轻量版透视系统"开挂(透视)辅助软件(证实有挂);无需打开直接搜索加薇1...
技巧知识分享“wpk辅助期免费... 无需打开直接搜索;操作使用教程:技巧知识分享“wpk辅助期免费版”开挂辅助平台(透视)大神讲解1、界...
开挂辅助插件"pok... 开挂辅助插件"pokemmo脚本辅助器下载"开挂(透视)辅助下载(有挂细节)【无需打开直接搜索加薇1...
热点推荐“wpk透视app有哪... wpk透视app有哪些是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以...
开挂辅助插件"wep... wepoker透视功能下载 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大...
2026教程“微扑克有猫腻吗”... >>您好:微扑克有猫腻吗确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总是好牌,...
开挂辅助脚本"wep... wepoker免费脚本弱密码是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
玩家亲测“wepoker机制”... >>您好:wepoker机制确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总是好...