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

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

相关内容

热门资讯

固有有辅助"西元文山... 西元文山攻略是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我微信(...
从来有辅助"温州茶园... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
一向有辅助"wepo... wepoker辅助器免费下载是一款专注玩家量身打造的游戏记牌类型软件,在wepoker辅助器免费下载...
本来有辅助"poke... 本来有辅助"pokemmo手机辅助软件"开挂(透视)辅助软件(玩家教你有挂功能);无需打开直接搜索加...
往昔有开挂"微信卡五... 往昔有开挂"微信卡五星辅助"开挂(透视)辅助透视(透视教程有挂规律) 【无需打开直接搜索加薇1367...
固有有辅助"福建兄弟... 固有有辅助"福建兄弟十三水有没有挂"开挂(透视)辅助下载(分享教程有挂教学);无需打开直接搜索加薇1...
本然有开挂"打两圈兴... 本然有开挂"打两圈兴化辅助"开挂(透视)辅助安装(揭秘教程有挂分享)《详细加薇136704302咨询...
从前有辅助"wepo... wepoker插件程序激活码是一款专注玩家量身打造的游戏记牌类型软件,在wepoker插件程序激活码...
往昔有辅助"小程序可... 往昔有辅助"小程序可以开挂吗"开挂(透视)辅助插件(高科技教程有挂教学)您好:小程序可以开挂吗这款游...
先前有开挂"钱塘十三... 钱塘十三水透视 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...