即使应用程序被杀死,也显示颤振通知

时间:2021-05-26 00:40:53

标签: flutter firebase-cloud-messaging

我正在尝试显示通过 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设备在前台时,通知完美显示,当应用程序最小化时,通知也完美地显示在后台,但当我们杀死应用程序时,通知不再显示在后台。< /p>

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

1 个答案:

答案 0 :(得分:0)

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

这是documentation的引述:

<块引用>

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

在 Android 上,将优先级字段设置为高。在 Apple(iOS 和 macOS)上, 将 content-available 字段设置为 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