【问题标题】:FirebaseMessagingService in not runningFirebaseMessagingService 未运行
【发布时间】:2019-10-03 09:36:00
【问题描述】:

我正在开发接收 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));

【问题讨论】:

  • 您不必启动 FirebaseMessagingService。它应该自动运行。删除 startService(new Intent(this,MyService.class));然后再试一次。
  • @omarshady 你确定通知在应用打开时不起作用(前台)而在应用关闭时(后台)起作用吗??
  • @BhoomikaPatel 是的,我确定
  • @MarkWalczak 谢谢你,它有效,我已经编辑了服务代码,它运作良好

标签: android firebase firebase-cloud-messaging


【解决方案1】:

Firebase 云消息传递 提供了不同的消息类型供使用。它们是:

  • 通知消息。它会在前台和后台状态下作为推送通知自动显示给用户。
  • 数据消息。此消息不是关于显示推送,而是关于将信息从服务器传递到移动应用程序。您可以使用自定义FirebaseMessagingService 处理任何数据通知。如果您处理数据消息,您仍然可以手动显示通知
  • 带有可选数据负载的通知消息。第一种和第二种类型的组合。当应用程序处于后台时,api 将自动显示通知。如果您在前台运行,则可以使用您的服务处理此消息。

现在更具体

消息的类型取决于消息对象内部的内容。你应该关心里面的notificationdata 字段。 如果您指定 notification 对象(它是您的推送通知的标题和正文)并且不包含 data 对象 - 那么您的消息类型是 通知 如果反之,则仅指定data 对象,则类型为data 结合这两种类型会导致最后一种类型

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

您可以在official documentation page找到更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多