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

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

相关内容

热门资讯

最新通报!新超圣正版辅助(余干... 最新通报!新超圣正版辅助(余干小程序中至五十k有挂装)素来有作弊挂开挂辅助神器(证实有挂);1、最新...
热点推荐!超级三加一辅助工具安... 热点推荐!超级三加一辅助工具安装(余干六副真有辅助吗)好像有作弊挂开挂辅助插件(有挂秘诀);超级三加...
我来教教大家!悠悠互娱辅助(新... 我来教教大家!悠悠互娱辅助(新鸿狐辅助软件是真的)一直有作弊挂开挂辅助下载(有挂方针);新鸿狐辅助软...
实操分享!新九游辅助(jj斗地... 实操分享!新九游辅助(jj斗地主辅助)从前有作弊挂开挂辅助插件(有挂解惑);jj斗地主辅助软件透明挂...
大神推荐!樱花之盛牛牛挂(哥哥... 大神推荐!樱花之盛牛牛挂(哥哥打大a辅助)起初有作弊器开挂辅助安装(有挂辅助);1、超多福利:超高返...
热点讨论!新众亿软件可以安装(... 热点讨论!新众亿软件可以安装(湖北卡五星技能)从前有作弊挂开挂辅助脚本(发现有挂);小薇(透视辅助)...
新手必备!新金龙辅助(新九哥开... 新手必备!新金龙辅助(新九哥开挂)往昔有作弊挂开挂辅助软件(有挂秘诀)是一款可以让一直输的玩家,快速...
三分钟了解!新星游辅助软件试用... 您好,微信小程序白金岛修改器视频这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
重大通报!新西楚辅助器(微乐家... 重大通报!新西楚辅助器(微乐家乡麻将辅助)本然有作弊挂开挂辅助脚本(有人有挂)是一款可以让一直输的玩...
技巧知识分享!海贝之城有辅助(... 技巧知识分享!海贝之城有辅助(天天互娱辅助器免费下载)竟然有作弊器开挂辅助安装(有挂教学);最新版2...