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

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

相关内容

热门资讯

透视黑科技!多乐辅助下载够机,... 透视黑科技!多乐辅助下载够机,newpoker脚本-素来有挂开挂辅助神器1、下载安装好多乐辅助下载够...
透明科技!腾讯欢乐卡五星辅助器... 腾讯欢乐卡五星辅助器是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加...
透视真的!蛮王辅助器,wepo... >>您好:蛮王辅助器确实是有挂的,很多玩家在这款蛮王辅助器游戏中打牌都会发现很多用户的牌特别好,总是...
透视游戏!科米台州麻将辅助,w... 透视游戏!科米台州麻将辅助,wpk俱乐部是做什么的-一向有挂开挂辅助脚本;无需打开直接搜索薇:136...
透视app!新海贝辅助器,we... 透视app!新海贝辅助器,we poker辅助器下载-往昔有挂开挂辅助挂;无需打开直接搜索微信(13...
透明有挂!小程序牵手跑得辅助,... 透明有挂!小程序牵手跑得辅助,wepoker正确养号方法-原先有挂开挂辅助软件1、下载安装好小程序牵...
透明新版!传送屋透视,wepo... 传送屋透视开挂教程视频分享装挂详细步骤在当今的网络游戏中,传送屋透视作为一种经典的娱乐方式,吸引了无...
透视智能ai!友玩广西修改器,... 透视智能ai!友玩广西修改器,哈糖大菠萝怎么开挂-固有有挂开挂辅助挂;无需打开直接搜索加薇13670...
透视线上!山西打锅子技巧,aa... 透视线上!山西打锅子技巧,aa poker辅助-原先有挂开挂辅助挂 了解更多开挂安装加(136704...
透明软件!逍遥辅助器怎么用,a... 透明软件!逍遥辅助器怎么用,aapoker透视脚本-本然有挂开挂辅助工具这是一款可以让一直输的玩家,...