[英]Where is FCM Intent with data delivered, when app is in background?
I've implemented Firebase in my app and I'm sending push with extra data.我已经在我的应用程序中实施了 Firebase,并且正在发送带有额外数据的推送。 When my app is in foreground I'm handling data properly and showing my own notification, but I have a problem with fetching data, when Firebase shows Notification "automatically" when app was "homed" (not killed).
当我的应用程序处于前台时,我正在正确处理数据并显示我自己的通知,但是当 Firebase 在应用程序“归巢”(未终止)时“自动”显示通知时,我在获取数据时遇到了问题。 According DOCS
Activity
should get new Intent
with extras fulfilled with my values, instead app just get back to front, old state is restored.根据DOCS ,
Activity
应该获得新的Intent
,并用我的价值观实现额外的功能,而不是应用程序回到前面,恢复旧状态。
Scenario:设想:
Notification
WITHOUT calling onMessageReceived
(according to table in docs, it should?)Notification
而不调用onMessageReceived
(根据文档中的表,它应该?)Intent
is fulfilled with "original" extras used for open Activity
on topIntent
被用于在顶部打开Activity
的“原始”附加功能实现I have logs in onCreate
, onNewIntent
, onResume
( Activity
) and in onMessageReceived
( Service
), only onResume
is called, in which I'm printing extras like below:我在
onCreate
、 onNewIntent
、 onResume
( Activity
) 和onMessageReceived
( Service
) 中登录,只调用onResume
,我在其中打印如下所示的额外内容:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Log.d("Activity onResume", "Key: " + key + " Value: " + value);
}
}
Messages with both notification and data payload, both background and foreground.具有通知和数据负载的消息,包括背景和前景。 In this case the data payload is delivered to extras of the intent of your launcher activity.
在这种情况下,数据有效负载被传送到您的启动器活动的意图的额外部分。 If you want to get it on some other activity, you have to define click_action on the data payload.
如果您想在其他活动中获取它,则必须在数据负载上定义 click_action。 So get the intent extra in your launcher activity.
因此,在您的启动器活动中获得额外的意图。
Edit:-编辑:-
From the documentation :- When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.从文档中:- 在后台时,应用程序在通知托盘中接收通知有效负载,并且仅在用户点击通知时处理数据有效负载。
So check the launcher activity's oncreate() method with getIntent() extras upon click of notification.因此,在单击通知时使用 getIntent() extras 检查启动器活动的 oncreate() 方法。
So I was looking for this answer and read the same.所以我一直在寻找这个答案并阅读相同的内容。 It has to be some where.
它必须在某个地方。 I know this is a year old but they still haven't really made it clear.
我知道这是一岁了,但他们还没有真正说清楚。 In the pendingActivity associated with the notification ( in my case it was MainActivity ) I added the following.
在与通知关联的 pendingActivity 中(在我的例子中是 MainActivity )我添加了以下内容。 Please note I was working through the Google Codelabs com.example.android.eggtimernotifications not that this was in the answers.
请注意,我正在通过 Google Codelabs com.example.android.eggtimernotifications 工作,而不是在答案中。
override fun onResume() {
super.onResume()
intent?.run {
val keys = this.extras?.keySet()
if (!keys.isNullOrEmpty()) {
keys.forEach { key ->
when (this.extras!![key]) {
is Long -> println("$key = ${this.getLongExtra(key, 0L)}")
is Int -> println("$key = ${this.getIntExtra(key, 0)}")
is String -> println("$key = ${this.getStringExtra(key)}")
is Boolean -> println("$key = ${this.getBooleanExtra(key, false)}")
else -> println("unkonwn Type")
}
}
}
}
}
This resulted in the following.这导致了以下结果。
The data keys were cereal, from, milk and sugar.数据键是谷物、牛奶和糖。
According to documentation Cloud messaging docs message can be delivered in 3 states:根据文档Cloud messaging docs消息可以以 3 种状态传递:
Background handling:后台处理:
For 2. onMessageReceived
will be called without UI and additional handling.对于 2.
onMessageReceived
将在没有 UI 和额外处理的情况下被调用。 Later you can show something custom.稍后你可以展示一些定制的东西。
When it comes to 3. android will show notification and hold data until there is user interaction.当涉及到3时,android会显示通知并保持数据,直到有用户交互。 After click it's just resuming your existing activity.
单击后,它只是恢复您现有的活动。 Your
onNewIntent
is not triggered because your launch mode is not FLAG_ACTIVITY_SINGLE_TOP
.您的
onNewIntent
未被触发,因为您的启动模式不是FLAG_ACTIVITY_SINGLE_TOP
。
You should also check your logcat output for: Google Play services out of date
.您还应该检查您的 logcat 输出:
Google Play services out of date
。
onMessageReceived() is not called when application is on background.应用程序在后台时不会调用 onMessageReceived()。 I have this problem few months a ago.
几个月前我遇到了这个问题。 I resolve it by override handlerIntent method.
我通过覆盖 handlerIntent 方法来解决它。 But your firebase messaging library should
但是你的 firebase 消息库应该
implementation 'com.google.firebase:firebase-messaging:10.2.1'
after that:在那之后:
@Override
public void handleIntent(Intent intent) {
try
{
if (intent.getExtras() != null)
{
RemoteMessage.Builder builder = new RemoteMessage.Builder("MessagingService");
for (String key : intent.getExtras().keySet())
{
builder.addData(key, intent.getExtras().get(key).toString());
}
onMessageReceived(builder.build());
}
else
{
super.handleIntent(intent);
}
}
catch (Exception e)
{
super.handleIntent(intent);
}
}
if you don't want to downgrade your library then如果你不想降级你的图书馆那么
You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user.您可以指定一个 click_action 来指示当用户点击通知时应启动的意图。 The main activity is used if no click_action is specified.
如果未指定 click_action,则使用主要活动。
When the intent is launched you can use the启动意图时,您可以使用
getIntent().getExtras();
to retrieve a Set that would include any data sent along with the notification message.检索一个 Set,其中包含随通知消息一起发送的任何数据。
For more on notification message see https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive有关通知消息的更多信息,请参阅https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.