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

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

相关内容

热门资讯

七分钟揭幕!决战卡五星游戏辅助... 七分钟揭幕!决战卡五星游戏辅助器,新九五辅助(都是有挂)-哔哩哔哩决战卡五星游戏辅助器辅助器中分为三...
第9分钟外挂!老友广东潮汕麻雀... 第9分钟外挂!老友广东潮汕麻雀,四川游戏家园辅助软件(都是有挂)-哔哩哔哩1、四川游戏家园辅助软件系...
五分钟揭露!约局吧脚本透视开挂... 五分钟揭露!约局吧脚本透视开挂,捉住捣蛋鸡辅助(原来真的有挂)-哔哩哔哩1、完成约局吧脚本透视开挂的...
第5分钟专业!欢乐对决脚本,微... 第5分钟专业!欢乐对决脚本,微信微乐小程序修改器(总是有挂)-哔哩哔哩该软件可以轻松地帮助玩家将微信...
第四分钟推荐!潮汕木虱有辅助挂... 第四分钟推荐!潮汕木虱有辅助挂吗,广西老友玩方片破解(总是是有挂)-哔哩哔哩该软件可以轻松地帮助玩家...
7分钟揭幕!新西部透视挂辅助器... 7分钟揭幕!新西部透视挂辅助器,阿拉斗牌辅助(好像是真的挂)-哔哩哔哩1、7分钟揭幕!新西部透视挂辅...
第五分钟解谜!掌上程序辅助器下... 第五分钟解谜!掌上程序辅助器下载,樱花之盛辅助软件下载(切实存在有挂)-哔哩哔哩1、任何樱花之盛辅助...
第4分钟分享!友间十三道有什么... 第4分钟分享!友间十三道有什么窍门,福建天天开心技巧(其实真的有挂)-哔哩哔哩;1、友间十三道有什么...
2分钟普及!指尖四川辅助脚本苹... 2分钟普及!指尖四川辅助脚本苹果版,吉祥填大坑技巧(总是真的有挂)-哔哩哔哩1、许多玩家不知道指尖四...
九分钟详细!微信小程序大贰辅助... 九分钟详细!微信小程序大贰辅助工具,天天微友有辅助吗(果然真的是有挂)-哔哩哔哩1)微信小程序大贰辅...