FirebaseMessagingService未运行

时间:2019-10-03 09:36:00

标签: android firebase firebase-cloud-messaging

我正在处理收到FCM(火警消息云)的android应用,当该应用在后台或关闭时,android系统会接收到通知并正确显示,但是当该应用变为在前台,我已经检查了扩展FirebaseMessagingService的服务,该服务未运行,并且我不知道如何使其运行。

服务代码:

public class MyService extends FirebaseMessagingService {
    public MyService() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    }
}

清单代码:

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

主要活动代码:

startService(new Intent(this,MyService.class));

1 个答案:

答案 0 :(得分:0)

Firebase Cloud Messaging 提供了不同的消息类型供使用。这些是:

  • 通知消息。它会自动以推送通知的形式在前景和背景状态下向用户显示。
  • 数据消息。该消息与显示推送无关,而与从服务器向移动应用程序传递信息有关。您可以使用自定义for every item in the string: if it's the first item: while the next item is not equivalent to the last item: count the items if they're equivalent and add them to a result list elif it's not the first item or the last item and it hasn't been previously counted: count the items that are equivalent and continues and add them to the result list else: this must be the last item and simply add '21' to the result list return the result list to the caller function 处理任何数据通知。如果您处理数据信息,仍然可以手动显示通知
  • 带有可选数据有效负载的通知消息。第一种和第二种类型的组合。当应用程序在后台运行时,api将自动显示通知。如果您正在运行前台,则可以使用服务来处理此消息。

现在更具体

消息的类型取决于消息对象内部的内容。您应该注意其中的FirebaseMessagingServicenotification字段。 如果您指定data对象(即标题和推送通知的主体)并且不包含notification对象-则消息类型为 notification 如果反之亦然,则仅指定data对象,则类型为 data 并结合两种类型会导致最后一种类型

因此,如果您只想使用推送通知显示一些信息,请考虑使用 notification 类型。

您可以在official documentation page

上找到更多信息。