gpt4 book ai didi

android - 当应用程序被杀死时,Firebase 通知不会在某些手机上运行

转载 作者:行者123 更新时间:2023-11-30 00:22:06 27 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用中实现 FCM。在一些手机上,当应用程序处于前台/后台/被杀死时会收到通知。但在某些手机上,只有当应用程序处于前台/后台时才会收到。当应用程序被终止时, onMessageReceived 和启动器 getExtra 也不会被调用。我同时使用 API 中的数据和通知对象。这是我的 Android 代码 -

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e("Here", "here");
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}

最佳答案

Add BootBroadcastReceiver in Manifest file

<receiver
android:name=".OnBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

Create Following Java file. call the FirebaseMessagingReceiveService inside it.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
i.setClass(context, FirebaseMessagingReceiveService.class);
context.startService(i);
}
}

关于android - 当应用程序被杀死时,Firebase 通知不会在某些手机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46092014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com