【发布时间】:2017-10-25 16:20:12
【问题描述】:
我目前正在构建一个使用 FCM 的 android 应用程序。不幸的是,我无法将这一切放在一起。我已成功将 firebase 连接到我的应用程序,.json 文件(由我的 API 提供程序生成)也放置得很好。如果应用程序在前台,则正确调用 onMessagReceived。推送通知也被正确接收,但通知上的点击事件(在后台)绝对没有任何作用。通知消失而不打开启动活动。如果我将 .json 文件切换到我使用示例 FCM 应用程序生成的文件,单击通知会以所需的方式工作。我不知道发生了什么。
下面我展示了我的 AndroidManifest.xml 的一部分,其中包含启动器活动/fcm 服务和由我的 api 提供程序生成的 .json 文件(一些数据已更改,但特定值是相同的):
<application
android:name=".xyz"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/xyz"
android:roundIcon="@mipmap/ic_launcher"
android:theme="@style/Theme.xyz.Light.DarkActionBar">
<activity
android:name=".views.activities.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.xyz.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data
android:host="xyz.com"
android:pathPrefix="/api,xyz"
android:scheme="https"/>
<data
android:host="xyz.pl"
android:pathPrefix="/api,xzz"
android:scheme="https"/>
</intent-filter>
</activity>
...
<service android:name=".services.xyzFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".services.xyzFirebaseInstanceService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</activity>
</application>
{
"project_info": {
"project_number": "123456789123",
"firebase_url": "https://xxx.firebaseio.com",
"project_id": "xxx",
"storage_bucket": "xxx.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:123456789123:android:a12a3456789012e1",
"android_client_info": {
"package_name": "com.mycompany.myapp"
}
},
"oauth_client": [
{
"client_id": "123456789123-zzz.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "current_key"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 2
}
}
}
],
"configuration_version": "1"
}
我还包括我的 api 提供者生成的示例 fcm 消息。
[2017-11-02 12:10:06] https://fcm.googleapis.com/fcm/send
HEADERS: [
"Authorization:key=my_auth_key"
]
DATA: {
"notification": {
"icon": "https://www.myavatar.com/avatar.png",
"title": "Message title",
"body": "aaa",
"click_action": "https://www.app.com/notifications,open?_id=678696787lkju82"
},
"data": {
"date_create": "2017-11-02 12:10:04",
"sender_type": "user",
"sender_app_id": "2",
"sender_app_display": "MyApp",
"sender_user_id": "43",
"sender_display": "WK",
"type": "message"
},
"to": "fYqHDJEVUUM:APAasdasdasdHS_GOsen-kkjghkasjhd898098klj-8797hoijo8"
}
RESPONSE: {
"multicast_id": 556579726904--2753,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1880938748%hkk4bjl2k1"
}
]
}
更新
我发现了问题。 “click_action”参数导致了问题,我的 api 提供者将 url 地址设置为这个参数的值,以帮助自己在 web 端,就是这样;)非常感谢@Barns52 指导我解决问题。
【问题讨论】:
-
请阅读有关不同消息负载的 Firebase 文档。 FCM 提供两种不同的有效载荷。当应用程序处于后台时,每种负载类型的处理方式都不同。
-
如果我以前不阅读文档,我不会创建新主题。它说“当您的应用程序在后台时传递的通知消息。在这种情况下,通知将传递到设备的系统托盘。用户点击通知会默认打开应用程序启动器。”我的 api 提供商向我发送通知消息,然后单击托盘上的通知什么也不做。
-
在这种情况下,您需要发布用于生成消息的代码以及用于接收消息的代码。仅仅发布
AndroidManifest将使我们基本上无法诊断您所犯的错误。 -
感谢您的回复。基本上我没有任何接收消息的高级实现,只是一个扩展 FirebaseMessagingService 的类,带有空的 onMessageReceived() 方法。我认为 android 自己处理点击带有类型通知的推送通知。我还没有任何两者或数据类型,所以我真的没有看到任何在 android 端犯错误的字段。 api端错误是否可能导致发送无法打开通知?
-
我需要消息 json。它看起来像这样:: { "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification":{ "title":"Portugal vs. Denmark", "body":"great match! " },“数据”:{“尼克”:“马里奥”,“房间”:“葡萄牙VSDenmark”}}}
标签: android firebase push-notification notifications firebase-cloud-messaging