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

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

相关内容

热门资讯

通报透视"wepok... 通报透视"wepoker真的有挂透视辅助吗"开挂(透视)辅助神器(最新技巧揭秘攻略)【福星临门,好运...
教会辅助!蜀山四川破解版安卓版... 教会辅助!蜀山四川破解版安卓版,逸趣鄱阳翻精辅助(太实锤了辅助开挂下载),蜀山四川破解版安卓版是用手...
正版透视"wepok... 正版透视"wepoker德州透视软件是真的吗"开挂(透视)辅助平台(科技介绍攻略方法)正版透视"we...
科技辅助!腾旭欢乐全集辅助,创... 科技辅助!腾旭欢乐全集辅助,创思维激k看底牌辅助(太离谱了开挂辅助工具);大家肯定在之前创思维激k看...
通报透视"wepok... 通报透视"wepoker透视教程"开挂(透视)辅助平台(玩家必看科普详细教程)通报透视"wepoke...
传授辅助!微乐斗地主有挂吗,小... 传授辅助!微乐斗地主有挂吗,小程序牵手跑得辅助(太夸张了开挂辅助安装);1、不需要AI权限,帮助你快...
细节透视"wepok... 细节透视"wepoker是不是有辅助"开挂(透视)辅助软件(玩家必看秘籍解密教程)细节透视"wepo...
细节开挂!衡阳丫丫辅助,玩吧辅... 细节开挂!衡阳丫丫辅助,玩吧辅助工具(太坑了开挂辅助平台),玩吧辅助工具是用手机号来登录游戏的系统规...
教会透视"微乐扑克辅... 教会透视"微乐扑克辅助器下载"开挂(透视)辅助安装(实测发现德州教程)教会透视"微乐扑克辅助器下载"...
详细开挂!榆林打锅子技巧,来来... 详细开挂!榆林打锅子技巧,来来拼十免费辅助(太嚣张了开挂辅助工具);来来拼十免费辅助软件透视开挂作为...