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

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

相关内容

热门资讯

开挂辅助!吉安中至小程序微信辅... 开挂辅助!吉安中至小程序微信辅助,wepokerplus万能挂(透视)开挂辅助脚本(有挂教程);相信...
开挂辅助!雀友会广东潮汕麻雀万... 开挂辅助!雀友会广东潮汕麻雀万能辅助器,wepoker免费脚本咨询(透视)开挂辅助下载(有挂秘籍);...
开挂辅助!琼崖海南麻将辅助器,... 开挂辅助!琼崖海南麻将辅助器,poker world辅助(透视)开挂辅助下载(有挂解惑);琼崖海南麻...
辅助开挂!微乐小程序辅助脚本,... 辅助开挂!微乐小程序辅助脚本,cloudpoker辅助(透视)开挂辅助软件(有挂工具);1、很好的微...
辅助开挂!微信牵手跑辅助,wp... 【福星临门,好运相随】;辅助开挂!微信牵手跑辅助,wpk辅助插件(透视)开挂辅助下载(有挂秘诀);暗...
辅助开挂!四川乐易麻将辅助脚本... 辅助开挂!四川乐易麻将辅助脚本,wepoker辅助器下载(透视)开挂辅助工具(有挂方略);四川乐易麻...
辅助开挂!微信微乐跑得快游戏辅... 辅助开挂!微信微乐跑得快游戏辅助脚本,wejoker辅助软件价格(透视)开挂辅助工具(有挂透视);相...
辅助开挂!福建兄弟十三水挂件去... 辅助开挂!福建兄弟十三水挂件去那买,wepoker亲友圈有用吗(透视)开挂辅助神器(存在有挂);1....
今日公布“wepoker开挂辅... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
推荐攻略“wepoker能开挂... 推荐攻略“wepoker能开挂吗”开挂辅助挂(透视)解说技巧是一款可以让一直输的玩家,快速成为一个“...