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

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

相关内容

热门资讯

一分钟快速了解!创思维激k软件... 一分钟快速了解!创思维激k软件助手,广西友乐app辅助工具,详细app(有挂细节)广西友乐app辅助...
教程辅助!微信小程序微乐家乡辅... 教程辅助!微信小程序微乐家乡辅助器,丽水茶苑手机辅助,详细攻略(有挂分析)1、完成微信小程序微乐家乡...
一分钟揭秘!互游辅助脚本,微乐... 一分钟揭秘!互游辅助脚本,微乐老友广东辅助器,详细工具(有挂规律)1、微乐老友广东辅助器模拟器是什么...
一分钟揭秘!新超凡大厅辅助,全... 一分钟揭秘!新超凡大厅辅助,全民内蒙古辅助器,详细攻略(有挂秘笈)在进入全民内蒙古辅助器软件靠谱后,...
六分钟了解!蜀渝牌血战到底辅助... 您好,蜀渝牌血战到底辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
重大推荐!奇迹掌趣辅助,科乐天... 重大推荐!奇迹掌趣辅助,科乐天天踢解码器,详细插件(真的有挂)1)科乐天天踢解码器免费钻石:进一步探...
记者揭秘!金虎爷辅助插件,微乐... 记者揭秘!金虎爷辅助插件,微乐卡五星祈福有用吗,详细神器(了解有挂)1、微乐卡五星祈福有用吗辅助软件...
重大来袭!微信新海贝之城辅助,... 重大来袭!微信新海贝之城辅助,福麻圈辅助器,详细软件(有挂细节)1、全新机制【微信新海贝之城辅助ai...
一起来讨论!斗棋有bug么开挂... 一起来讨论!斗棋有bug么开挂,多乐游戏小程序辅助器免费,详细技巧(有挂方法)小薇(辅助器软件下载)...
玩家爆料!广西老友玩游戏辅助器... 玩家爆料!广西老友玩游戏辅助器,老友广东潮汕麻雀,详细脚本(有挂规律)1.广西老友玩游戏辅助器 选牌...