Firebase API 推送不会在 Android 设备上打开后台应用程序
Firebase API push doesn't open background app on Android device
Firebase API 从后端推送负载:
{
"registration_ids": [
"IKSiok9Zpip5cDcebQ67wc7TnpDuhMAFJm1xdGGI44s48JbXEu5iYa"
],
"notification": {
"body": "Push Test Facility Category",
"title": null,
"icon": "myicon",
"sound": "mySound",
"vibrate": 1,
"Badge": 1,
"click_action": "FCM_PLUGIN_ACTIVITY"
},
"data": {
"message": "Push Test Facility Category",
"title": null,
"b": "22",
"id": "2",
"category_name": "Restaurants",
"h": "1",
"p": 1,
"type": "2"
}
}
fcm.service.ts
fcmListeners() {
this.firebase.onNotificationOpen().subscribe(async (data: any) => {
if (data.tap) {// when user tapped the background notification
} else { // Received in foreground
}
});
}
你能告诉我为什么当我使用上面的有效负载并且应用程序在后台时应用程序没有打开吗?这是 Android 设备上的行为。但在 iOS 设备上没有问题。
如果我使用 firebase 控制台云消息(即仪表板),则没有问题。即它甚至在 android 设备的后台打开应用程序。有什么线索吗?
注:我这里用的是firebase plugin
问题是 FCM_PLUGIN_ACTIVITY
activity 可能没有在 AndroidManifest.xml
中定义,或者即使在那里定义,也可能无法正确实现。
为了修复它,不要在通知的有效负载中指定 click_action
- 默认情况下,通知将使用 MainActivity
并且将打开应用程序。
{
"registration_ids": [
...
],
"notification": {
...,
"click_action": "FCM_PLUGIN_ACTIVITY" // <--- Remove this
},
"data": {
...
}
}
并且还像这样定义了你的activity
<activity
android:name=".NotificationPushTapedActivity"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:taskAffinity=""
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="CLICK_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
上面标记为答案的回复可能有效,但这不是 Android 上做事的理想方式。
当android 查找后端推送通知的“ClickAction”中指定的意图并没有找到时,就会出现上述问题。
这将导致 android 不知道使用什么 activity 来处理您在通知中指定的意图,并且您将在输出中包含此消息 window.
Notification pending intent canceled
处理此问题的最佳方法是在您的通知中精确指定“click_action”的字符串值(上面我们有“FCM_PLUGIN_ACTIVITY”)
然后,如果您的清单文件,请为此意图添加一个操作过滤器,同时精确设置“默认”类别。如下图
<activity
.... >
....
<intent-filter>
<action android:name="FCM_PLUGIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
如果您在 Xamarin.Android,有一种更简单的方法,
在您的 activity 上方,只需添加 Intentfilter 标志,并精确指定您的意图名称:
[IntentFilter(new []
{
"FCM_PLUGIN_ACTIVITY"
},
Categories=new[]
{
global::Android.Content.Intent.CategoryDefault
})]
public class MainActivity : ....
如本文所述comment。
Firebase API 从后端推送负载:
{
"registration_ids": [
"IKSiok9Zpip5cDcebQ67wc7TnpDuhMAFJm1xdGGI44s48JbXEu5iYa"
],
"notification": {
"body": "Push Test Facility Category",
"title": null,
"icon": "myicon",
"sound": "mySound",
"vibrate": 1,
"Badge": 1,
"click_action": "FCM_PLUGIN_ACTIVITY"
},
"data": {
"message": "Push Test Facility Category",
"title": null,
"b": "22",
"id": "2",
"category_name": "Restaurants",
"h": "1",
"p": 1,
"type": "2"
}
}
fcm.service.ts
fcmListeners() {
this.firebase.onNotificationOpen().subscribe(async (data: any) => {
if (data.tap) {// when user tapped the background notification
} else { // Received in foreground
}
});
}
你能告诉我为什么当我使用上面的有效负载并且应用程序在后台时应用程序没有打开吗?这是 Android 设备上的行为。但在 iOS 设备上没有问题。
如果我使用 firebase 控制台云消息(即仪表板),则没有问题。即它甚至在 android 设备的后台打开应用程序。有什么线索吗?
注:我这里用的是firebase plugin
问题是 FCM_PLUGIN_ACTIVITY
activity 可能没有在 AndroidManifest.xml
中定义,或者即使在那里定义,也可能无法正确实现。
为了修复它,不要在通知的有效负载中指定 click_action
- 默认情况下,通知将使用 MainActivity
并且将打开应用程序。
{
"registration_ids": [
...
],
"notification": {
...,
"click_action": "FCM_PLUGIN_ACTIVITY" // <--- Remove this
},
"data": {
...
}
}
并且还像这样定义了你的activity
<activity
android:name=".NotificationPushTapedActivity"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:taskAffinity=""
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="CLICK_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
上面标记为答案的回复可能有效,但这不是 Android 上做事的理想方式。 当android 查找后端推送通知的“ClickAction”中指定的意图并没有找到时,就会出现上述问题。 这将导致 android 不知道使用什么 activity 来处理您在通知中指定的意图,并且您将在输出中包含此消息 window.
Notification pending intent canceled
处理此问题的最佳方法是在您的通知中精确指定“click_action”的字符串值(上面我们有“FCM_PLUGIN_ACTIVITY”) 然后,如果您的清单文件,请为此意图添加一个操作过滤器,同时精确设置“默认”类别。如下图
<activity
.... >
....
<intent-filter>
<action android:name="FCM_PLUGIN_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
如果您在 Xamarin.Android,有一种更简单的方法, 在您的 activity 上方,只需添加 Intentfilter 标志,并精确指定您的意图名称:
[IntentFilter(new []
{
"FCM_PLUGIN_ACTIVITY"
},
Categories=new[]
{
global::Android.Content.Intent.CategoryDefault
})]
public class MainActivity : ....
如本文所述comment。