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(透视能赢开挂辅助平台)1、下载安装好微乐...
有挂讲解辅助!四川游戏家园通用... >>您好:四川游戏家园通用辅助确实是有挂的,很多玩家在这款四川游戏家园通用辅助游戏中打牌都会发现很多...
有挂功能辅助!微乐家乡app下... 有挂功能辅助!微乐家乡app下载,wepoker开挂事件(我来教教大家开挂辅助工具)>>您好:软件加...
有挂细节开挂!钱塘十三水辅助器... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
揭秘有挂开挂!山西扣点点脚本辅... >>您好:山西扣点点脚本辅助器确实是有挂的,很多玩家在这款山西扣点点脚本辅助器游戏中打牌都会发现很多...
有挂规律辅助!极速官方游戏暗堡... 极速官方游戏暗堡辅助器是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以...
有挂攻略开挂!潮友会鱼虾蟹塞子... 有挂攻略开挂!潮友会鱼虾蟹塞子概率计算方式,川南休闲辅助(重大推荐开挂辅助平台);打开点击测试直接进...
有挂分析开挂!琼戏互娱破解版,... 大家好,今天小编来为大家解答琼戏互娱破解版这个问题咨询软件客服可以免费测试直接加微信(1367043...
有挂详情辅助!连连看辅助源码,... 有挂详情辅助!连连看辅助源码,杭州都莱辅助软件有没有用(科普攻略开挂辅助安装);亲,杭州都莱辅助软件...