Firebase通知不在后台工作
问题描述:
我需要帮助。 Firebase通知不适用于后台。这是我的代码:Firebase通知不在后台工作
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "FROM:" + remoteMessage.getFrom());
sharedPreference = getSharedPreferences(Global.SECURETRADE, 0);
UID = sharedPreference.getString(Global.ID, "");
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)`enter code here`;
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.mipmap.small_secure_trade_app_icon);
} else {
notificationBuilder.setSmallIcon(R.drawable.small_secure_trade_app_icon);
}
notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.securetrade_icon));
notificationBuilder.setContentTitle(remoteMessage.getData().get("title"));
notificationBuilder.setContentText(remoteMessage.getData().get("body"));
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
答
是,火力地堡不会调用onMessageReceived()当应用程序在后台除非你从yoour服务器代码的通知要求身体的变化。
答
当应用程序在后台运行onMessageReceive将无法正常工作
离线消息来发射活动刚才复制的代码,这个和平的发射活动,并检查它。它会工作。
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Logger.info(TAG, "FIRE BASE OFF LINE NOTIFICATIONS COMING TO THIS BLOCK--->");
JSONObject json = new JSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
Logger.info(TAG, "json object--->" + key + "---values--" + JSONObject.wrap(bundle.get(key)));
}
}
,你的有效载荷应该是这样的
{ "notification": {
"title": "Your Title",
"text": "Your Text",
"click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
},
"data": {
"keyname": "any value " //you can get this data as extras in your activity and this data is optional
},
"to" : "to_id(firebase refreshedToken)"
}
答
你发送数据的消息(不通知的消息)?
通知的消息当你想FCM处理显示代表您的客户端应用程序的一个 通知不叫onMessageReceived()
通知邮件。 想要处理客户端应用程序中的消息时使用数据消息。
在这里阅读更多:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
PS:FCM Web控制台始终发送通知的消息
如果您发送的是数据的消息,并onMessageReceived()
不叫..
那么这是一个不同的问题。
它甚至可能是特定设备的问题。
请参阅Push notifications using FCM not received when app is killed android
您是如何发送消息的? –
我也想知道它是相同的,之前它正常工作 –
我也想知道为什么它不工作,Firebase给出了很多错误,这是很有趣的 –