【发布时间】:2017-05-16 09:00:18
【问题描述】:
根据https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages,我可以使用将 100% 收到 onMessageReceived 方法的数据消息并采取相应措施。但就我而言,当应用程序被杀死时,我的任何消息都没有收到。虽然它们有时会在我仅在 Nexus 5 上进行测试时收到。下面是我的数据负载。
05-16 06:46:01.663 13199-29574/jss.test E/MyFirebaseMsgService: Data Payload: {data={"image":null,"title":"My message"}}
05-16 06:46:01.667 13199-29574/jss.test E/Providerfalse: true
05-16 06:46:01.667 13199-29574/jss.test E/MyFirebaseMsgService: Notification JSON {"data":{"image":null,"title":"My message"}}
FCM 清单如下:
<service
android:name=".FirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Firebase 消息服务:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
//this method will display the notification
//We are passing the JSONObject that is received from
//firebase cloud messaging
private void sendPushNotification(JSONObject json) {
//optionally we can display the json into log
Log.e(TAG, "Notification JSON " + json.toString());
try {
//getting the json data
JSONObject data = json.getJSONObject("data");
//parsing json data
String title = data.getString("title");
String message = data.getString("message");
String imageUrl = data.getString("image");
//code to call service if message has specific data values.
}
catch (Exception e) {
e.printStackTrace(); }}
您能否指导我修复的位置以使其完美运行。
【问题讨论】:
-
你能分享你的 FirebaseMessagingService.java 代码吗?
-
为 FMS.java 添加了代码
-
一些可能有用的细节here
-
您找到解决方案了吗?找不到解决方案,真是令人沮丧。
-
您好,如果您有解决方案,请提供解决方案。谢谢。
标签: android firebase push-notification firebase-cloud-messaging