【问题标题】:Display Flutter Notifications even when application has being Killed即使应用程序已被终止,也显示 Flutter 通知
【发布时间】:2021-08-14 05:48:17
【问题描述】:

我正在尝试显示通过 FCM 发送到 Android 设备的消息。

为了清楚起见,我不想在 fcm 消息中使用通知属性。我更喜欢 data 属性,让移动开发者能够自定义通知。

我们目前有以下设置

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  log('Got a message whilst in the Background!');
  log('Message data: ${message.data}');
  displayNotification();
}

Future<void> _firebaseMessagingForegroundHandler() async {
  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    log('Got a message whilst in the foreground!');
    log('Message data: ${message.data}');
    displayNotification();
  });
}

还有以下内容:

Future<void> initFirebase() async {
  await Firebase.initializeApp();
  initFirebaseComponents();
}

void initFirebaseComponents() {
  _firebaseTokenRefreshHandler();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  _firebaseMessagingForegroundHandler();
}

当 Android 设备在前台时,通知会完美显示,当应用最小化时,通知也会完美显示在后台,但当我们终止应用时,通知不再显示在后台。

我已经搜索并没有找到解决方案,任何见解将不胜感激。

【问题讨论】:

    标签: flutter firebase-cloud-messaging


    【解决方案1】:

    由于您的有效负载是数据消息,因此设备似乎会忽略您的消息,因为数据消息被视为低优先级。

    这是documentation的引述:

    仅数据消息被设备视为低优先级,当您 应用程序在后台或终止,将被忽略。 但是,您可以通过发送额外的 FCM 有效负载上的属性:

    在 Android 上,将优先级字段设置为高。在 Apple(iOS 和 macOS)上, 将内容可用字段设置为 true。

    这里的解决方案是在有效负载上设置其他属性。以下是文档中的示例负载,显示了如何发送附加属性。

    {
      token: "device token",
      data: {
        hello: "world",
      },
      // Set Android priority to "high"
      android: {
        priority: "high",
      },
      // Add APNS (Apple) config
      apns: {
        payload: {
          aps: {
            contentAvailable: true,
          },
        },
        headers: {
          "apns-push-type": "background",
          "apns-priority": "5", // Must be `5` when `contentAvailable` is set to true.
          "apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
        },
      },
    }
    

    注意:这只会提高消息的优先级,但不能保证传递。

    More information

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-09
      • 2015-10-20
      • 2021-03-02
      • 2022-01-26
      • 2016-11-28
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多