我需要帮助。 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());
}
}
答案 0 :(得分:0)
是的,除非您通知您的服务器代码,否则Firebase不会在应用程序处于后台时调用onMessageReceived()。
答案 1 :(得分:0)
当app处于后台或被杀时,您必须使用数据有效负载进行通知。 Firebase onMessageReceived not called when app in background
答案 2 :(得分:0)
您是要发送数据消息(而不是通知消息)吗?
通知消息不要致电onMessageReceived()
当您希望FCM处理显示时,请使用通知消息 代表您的客户端应用程序发送通知。在您使用时使用数据信息 想要处理客户端应用上的消息。
在此处阅读更多内容: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
答案 3 :(得分:0)
只需删除'通知'您通过推送通知发送的json部分。 只需发送'数据' section,onMessageReceived将正常工作
答案 4 :(得分:-1)
当在后台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)"
}