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

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

相关内容

热门资讯

辅助透视"打哈儿脚本... 辅助透视"打哈儿脚本免费"开挂(透视)wepoker辅助真的假的(有挂助手);1、这是跨平台的打哈儿...
详细说明"上饶打炸漏... 详细说明"上饶打炸漏洞"开挂(透视)hhpoker哪个俱乐部靠谱(竟然有挂);大家肯定在之前上饶打炸...
透视透视挂"小程序微... 透视透视挂"小程序微乐游戏辅助"开挂(透视)pokemmo手机版修改器(真实有挂);是一款可以让一直...
终于懂了"决战geo... 《终于懂了"决战geo辅助"开挂(透视)大菠萝789辅助器下载(详细教程)》 决战geo辅助软件透视...
透视ai"欢乐茶馆脚... 透视ai"欢乐茶馆脚本辅助"开挂(透视)wejoker手机版透视脚本(有挂教学);1、超多福利:超高...
推荐几款新版"aap... 推荐几款新版"aapoker透视脚本"开挂(透视)wpk私人局辅助是真的吗(真是有挂);一、有挂的是...
详细说明"微乐贵阳捉... 详细说明"微乐贵阳捉鸡麻将辅助"开挂(透视)poker world辅助(有挂教学);1.微乐贵阳捉鸡...
研究成果"都莱辅助器... 研究成果"都莱辅助器"开挂(透视)wejoker辅助软件价格(有挂技巧);1、让任何用户在无需AI插...
专业讨论"大唐山西辅... 专业讨论"大唐山西辅助"开挂(透视)hhpoker破解工具(今日头条);软件透视开挂更新新赛季,主要...
重要通知"赤峰对调同... 【福星临门,好运相随】;重要通知"赤峰对调同城游辅助"开挂(透视)wepoker亲友圈有用吗(有挂总...