【问题标题】:How to get Firebase Combined Message in Background app如何在后台应用程序中获取 Firebase 组合消息
【发布时间】:2017-04-22 17:24:58
【问题描述】:

combined way阅读有关云消息的firebase文档,我在android设备中获取数据消息时遇到了问题。

var payload = {
    notification: {
        title: "Test",
        body: "Test body"
    },
    data: {    
        score: "850",
        close: "635.67"
    }
};

我希望在 android 设备中的“onMessageReceived”处获得数据字段,但我只收到一条通知消息。我试图点击消息,但在“onMessageReceived”中仍然没有任何消息。

我添加了一个意图过滤器来触发主要活动,在通知有效负载中添加“clickAction”(如 firebase 参考中所述)和“click_action”(如我在某些问题中所见),我也得到了相同的结果。

清单

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/blue" />


<service
android:name=".connection.TestFirebaseMessagingService">
<intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>


<service
android:name=".connection.TestFirebaseIDService">
<intent-filter>
    <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

Java 代码:

public class FirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "TestFirebase";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());
    LogNotification.d(TAG, "NOTIFICATION - From: " + remoteMessage.getFrom());


    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        LogNotification.d(TAG, "NOTIFICATION - Message data payload: " + remoteMessage.getData().toString());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        LogNotification.d(TAG, "NOTIFICATION - Message Notification Body: " + remoteMessage.getData().toString());

    }
}

}

一些提示?

【问题讨论】:

  • 请添加您的 Java 代码
  • 好的,我也添加清单。完成。

标签: android firebase firebase-cloud-messaging


【解决方案1】:

如果您的有效负载中有notification 键,则该消息将成为通知消息。

应用在后台时会自动显示通知消息。

当点击通知时,FCM 启动您的主要活动(如果您指定了click_action,则启动另一个活动)

在活动中,您可以使用getIntent() 来获取启动活动的意图。

如果您的 Activity 是由 FCM 启动的,您将在用于启动 Activity 的 Intent 中找到 data 有效负载。

【讨论】:

  • 指定为click_action?因为我在firebase文档中看到了clickAction。两者都正确吗?
  • 这取决于您使用的语言/上下文。如果您正在发送 HTTP 请求,则该参数称为 click_action。 firebase.google.com/docs/cloud-messaging/…
猜你喜欢
  • 2020-04-05
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
相关资源
最近更新 更多