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

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

相关内容

热门资讯

盘点透视!智星菠萝有挂吗,江西... 盘点透视!智星菠萝有挂吗,江西中指黑科技辅助软件,(透视)解密教程(有挂细节);无需打开直接搜索打开...
1.9分钟了解"微乐... 1.9分钟了解"微乐自建房脚本入口"开挂(透视)wepoker怎么提高运气(有挂工具);无需打开直接...
传授透视!wepoker手机版... 您好:这款微信小程序雀神挂件下载游戏是可以开挂的,确实是有挂的,很多玩家在这款微信小程序雀神挂件下载...
程序员教你"微信后羿... 程序员教你"微信后羿大厅辅助"开挂(透视)德州圈脚本(有挂教学);无需打开直接搜索打开薇:13670...
正品开挂!xpoker辅助工具... 正品开挂!xpoker辅助工具,湖北卡五星插件,(透视)高科技教程(有挂助手);无需打开直接搜索加(...
发现透视!hhpoker德州挂... 发现透视!hhpoker德州挂真的有吗,填大坑辅助工具排行,(透视)2026新版教程(有挂分析)hh...
透视好友"掌酷天天开... 掌酷天天开心辅助器是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以加我...
发现辅助!wepoker辅助透... wepoker辅助透视软件开挂教程视频分享装挂详细步骤在当今的网络游戏中,wepoker辅助透视软件...
玩家爆料"隶属茶苑苹... 隶属茶苑苹果手机辅助 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由:...
传授开挂!wepoker免费钻... 传授开挂!wepoker免费钻石,广东省神麻雀控制器,(透视)分享教程(有挂教程) 了解更多开挂安装...