FlutterFire Messaging 中的连接和未知错误(仅限 iOS)

Connection and unknown errors in FlutterFire Messaging (iOS Only)

我使用 subscribeToTopic 方法实现了 FlutterFire Messaging,如下面的代码片段:

final _topics = ['topicName'];

Future<void> subscribeTopics() async {
  for (final topic in _topics) {
    await FirebaseMessaging.instance.subscribeToTopic(topic);
  }
}

Future<void> unsubscribeTopics() async {
  for (final topic in _topics) {
    await FirebaseMessaging.instance.unsubscribeFromTopic(topic);
  }
}

在调试模式下一切正常,但在我的 Crashlytics 中我收到了一些关于它的报告(仅 iOS):

所有错误似乎都与互联网连接有关,所以我的问题是:“我应该在使用 FCM 之前验证用户连接,还是库已准备好处理它但仅在 Android 中?”。 =14=]

Crashlytics 堆栈跟踪

Non-fatal Exception: FlutterError
0  ???                            0x0 MethodChannelFirebaseMessaging.subscribeToTopic + 368 (method_channel_messaging.dart:368)
1  ???                            0x0 Messaging.subscribeTopics + 40 (messaging.dart:40)
2  ???                            0x0 Messaging.observeMessaging + 22 (messaging.dart:22)

这是一个一般性错误,因此您不必担心。当互联网连接恢复时,该方法有望自行重启。您还应该考虑应用程序在后台时可能会产生错误的情况。

您可以使用 try catch 块来避免在您的 crashlytics 仪表板中显示这些消息。

final _topics = ['topicName'];

Future<void> subscribeTopics() async {
  try {
    for (final topic in _topics) {
      await FirebaseMessaging.instance.subscribeToTopic(topic);
    }
  } on FirebaseException {
    // do nothing
  }
}

Future<void> unsubscribeTopics() async {
  try {
    for (final topic in _topics) {
      await FirebaseMessaging.instance.unsubscribeFromTopic(topic);
    }
  } on FirebaseException {
    // do nothing
  }
}

注意 它也会忽略所有其他错误消息。在这种情况下 否则你可以处理它

final _topics = ['topicName'];

Future<void> subscribeTopics() async {
  try {
    for (final topic in _topics) {
      await FirebaseMessaging.instance.subscribeToTopic(topic);
    }
  } on FirebaseException catch (e) {
    if (e.code == 'unknown') {
      // do nothing
    } else {
      // do something
    }
  }
}

Future<void> unsubscribeTopics() async {
  try {
    for (final topic in _topics) {
      await FirebaseMessaging.instance.unsubscribeFromTopic(topic);
    }
  } on FirebaseException catch (e) {
    if (e.code == 'unknown') {
      // do nothing
    } else {
      // do something
    }
  }
}