![](/img/trans.png)
[英]FCM Notification is not received when my app foreground service is killed
[英]FCM Notification not working on some devices when application is killed or not in foreground
當應用程序被用戶殺死(用戶從應用程序托盤上滑走)或被系統OS殺死時,是否進行過使FCM通知在某些設備上起作用的研究,但是沒有找到任何解決方案,如果您可以提供解決方案或告訴我我錯了。 通知是數據有效載荷
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String payload = "",notification_message = "", channelId ="", channelName="AAA", channelDescription="";
Log.d(TAG, "From: " + remoteMessage.getFrom());
String title = null, body = null;
if(remoteMessage.getNotification() != null) {
Log.d("REMOTE_DATA",remoteMessage.getNotification().getBody());
title = remoteMessage.getNotification().getTitle();
body = remoteMessage.getNotification().getBody();
}
if(remoteMessage.getData() != null) {
if(remoteMessage.getData().get("title") != null)
title = remoteMessage.getData().get("title");
if(remoteMessage.getData().get("message") != null)
body = remoteMessage.getData().get("message");
if(remoteMessage.getData().get("channelName") != null)
channelName = remoteMessage.getData().get("channelName");
}
}
//Setting up Notification channels for android O and above
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Log.e(TAG, "onMessageReceived: => " + android.os.Build.VERSION.SDK_INT);
if(notificationChannels.containsKey(channelName)) {
JSONObject channelData = (JSONObject) notificationChannels.get(channelName);
try {
channelId = channelData.getString("channelId");
channelName = channelData.getString("channelName");
channelDescription = channelData.getString("channelDescription");
} catch (JSONException e) {
e.printStackTrace();
}
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
if (notificationChannel == null)
setupChannel(channelId, channelName, channelDescription);
}
}
Log.d(TAG, "Notification Message Body: " + title);
Log.d(TAG, "Notification Message Body: " + body);
if(title != null && body != null) {
apptivity_variable.createNotification(channelId,title, body);
db.insertNotification(title, body, remoteMessage.getData().toString());
}
}
嘗試注冊其他服務以在后台標識該應用以觸發該服務
public class FirebaseBackgroundService extends BroadcastReceiver{
private static final String TAG = "FirebaseService";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "I'm in!!!");
if (intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Object value = intent.getExtras().get(key);
Log.e("FirebaseDataReceiver", "Key: " + key + " Value: " + value);
if(key.equalsIgnoreCase("gcm.notification.body") && value != null) {
Bundle bundle = new Bundle();
Intent backgroundIntent = new Intent(context, BackgroundSyncJobService.class);
bundle.putString("push_message", value + "");
backgroundIntent.putExtras(bundle);
context.startService(backgroundIntent);
}
}
}
}
}
另外,不要忘記在manifest.xml中注冊它
<receiver android:exported="true" android:name=".FirebaseBackgroundService" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.