Flutter - 在 IOS 上使用 FCM 的后台通知

Posted

技术标签:

【中文标题】Flutter - 在 IOS 上使用 FCM 的后台通知【英文标题】:Flutter - background notification using FCM on IOS 【发布时间】:2020-02-11 03:39:26 【问题描述】:

我正在尝试使用 Flutter 在 ios 设备上接收静默/后台推送通知。我已经设法让它在 android 上运行,只是 IOS 是问题。

设置 - 我正在使用firebase_messaging 5.1.6 插件 - 我在 XCode 中勾选了 remote notificationsbackground fetch,并且还在 Capabilities 中启用了推送通知 - 带有数据的推送通知似乎工作正常

我的有效载荷(我尝试过有数据和没有数据)

"registration_ids":["<IOS DEVICE IDS>"],"priority":"high","content_available":true,"mutable_content":true,"data":"title":"Title.","body":"Message."

我的firebase配置如下:

onBackgroundMessage 设置为 null 的原因是我收到以下错误:

Unhandled Exception: MissingPluginException(No implementation found for method FcmDartService#start on channel plugins.flutter.io/firebase_messaging)
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async 
  // test code
  NetworkService networkService = NetworkService();
  networkService.getData("test"); // hit a test endpoint to make sure the code excuted

  print("myBackgroundMessageHandler: $message");


_firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async 
      print("onMessage: $message");
      NetworkService networkService = NetworkService();
      networkService.getData("test");
    ,
    onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
    onLaunch: (Map<String, dynamic> message) async 
      print("onLaunch: $message");
    ,
    onResume: (Map<String, dynamic> message) async 
      print("onResume: $message");
    ,
  );

完全错误

2019-10-15 08:44:52.766262+0100 Runner[16132:1794539]  - <AppMeasurement>[I-ACS036001] Analytics screen reporting is disabled. UIViewController transitions will not be logged.
2019-10-15 08:44:52.909787+0100 Runner[16132:1794333] Metal API Validation Enabled
2019-10-15 08:44:53.173885+0100 Runner[16132:1794552] flutter: Observatory listening on http://127.0.0.1:58105/hC1vIwLgB_o=/
2019-10-15 08:44:53.294824+0100 Runner[16132:1794539] 6.9.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60102000 started
2019-10-15 08:44:53.295297+0100 Runner[16132:1794539] 6.9.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see https://help.apple.com/xcode/mac/8.0/#/dev3ec8a1cb4)
2019-10-15 08:44:53.940639+0100 Runner[16132:1794545] flutter: 1 /var/mobile/Containers/Data/Application/E0ADA03B-747D-4A66-9A96-97160F70D80A/Documents/db
2019-10-15 08:44:54.030056+0100 Runner[16132:1794545] [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: MissingPluginException(No implementation found for method FcmDartService#start on channel plugins.flutter.io/firebase_messaging)
#0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:7)
<asynchronous suspension>
#1      FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:118:16)
#2      configureFirebaseMessaging (package:gard/utilities/service_locator.dart:53:22)
#3      _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
#4      configureFirebaseMessaging (package:gard/utilities/service_locator.dart:47:40)
#5      main (package:gard/main.dart:31:9)
#6      _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#7      _rootRunUnary (dart:async/zone.dart:1132:38)
#8      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#9      _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#10     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#11     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#12     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#13     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#14     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#15     setupDatabase (package:gard/utilities/service_locator.dart)
#16     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#17     _rootRunUnary (dart:async/zone.dart:1132:38)
#18     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#19     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#20     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#21     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#22     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#23     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#24     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#25     DatabaseCreator.initDatabase (package:gard/database/database_creator.dart)
#26     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#27     _rootRunUnary (dart:async/zone.dart:1132:38)
#28     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#29     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#30     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#31     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#32     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#33     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#34     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#35     BasicLock.synchronized (package:synchronized/src/basic_lock.dart)
#36     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#37     _rootRunUnary (dart:async/zone.dart:1132:38)
#38     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#39     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#40     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#41     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#42     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#43     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#44     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#45     ReentrantLock.synchronized.<anonymous closure> (package:synchronized/src/reentrant_lock.dart)
#46     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#47     _rootRunUnary (dart:async/zone.dart:1132:38)
#48     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#49     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#50     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#51     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#52     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#53     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#54     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#55     SqfliteDatabaseFactoryMixin.openDatabase.<anonymous closure> (package:sqflite/src/factory_mixin.dart)
#56     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#57     _rootRunUnary (dart:async/zone.dart:1132:38)
#58     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#59     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#60     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#61     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#62     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#63     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#64     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#65     SqfliteDatabaseOpenHelper.openDatabase (package:sqflite/src/database.dart)
#66     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#67     _rootRunUnary (dart:async/zone.dart:1132:38)
#68     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#69     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#70     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#71     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#72     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#73     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#74     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#75     SqfliteDatabaseMixin.doOpen (package:sqflite/src/database_mixin.dart)
#76     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#77     _rootRunUnary (dart:async/zone.dart:1132:38)
#78     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#79     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#80     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#81     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#82     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#83     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#84     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#85     SqfliteDatabaseMixin.txnSynchronized (package:sqflite/src/database_mixin.dart)
#86     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#87     _rootRunUnary (dart:async/zone.dart:1132:38)
#88     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#89     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#90     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#91     Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#92     Future._completeWithValue (dart:async/future_impl.dart:522:5)
#93     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#94     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#95     SqfliteDatabaseMixin._runTransaction (package:sqflite/src/database_mixin.dart)
#96     _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#97     _rootRunUnary (dart:async/zone.dart:1132:38)
#98     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#99     _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#100    Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#101    Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#102    Future._completeWithValue (dart:async/future_impl.dart:522:5)
#103    _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#104    _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#105    SqfliteDatabaseMixin.endTransaction (package:sqflite/src/database_mixin.dart)
#106    _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#107    _rootRunUnary (dart:async/zone.dart:1132:38)
#108    _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#109    _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#110    Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#111    Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#112    Future._completeWithValue (dart:async/future_impl.dart:522:5)
#113    _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#114    _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#115    SqfliteDatabaseMixin.txnSynchronized (package:sqflite/src/database_mixin.dart)
#116    _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#117    _rootRunUnary (dart:async/zone.dart:1132:38)
#118    _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#119    _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#120    Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#121    Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#122    Future._completeWithValue (dart:async/future_impl.dart:522:5)
#123    _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#124    _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#125    wrapDatabaseException (package:sqflite/src/exception_impl.dart)
#126    _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#127    _rootRunUnary (dart:async/zone.dart:1132:38)
#128    _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#129    _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#130    Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#131    Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#132    Future._completeWithValue (dart:async/future_impl.dart:522:5)
#133    _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:30:15)
#134    _completeOnAsyncReturn (dart:async-patch/async_patch.dart:288:13)
#135    MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart)
#136    _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:71:64)
#137    _rootRunUnary (dart:async/zone.dart:1132:38)
#138    _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#139    _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
#140    Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
#141    Future._propagateToListeners (dart:async/future_impl.dart:707:32)
#142    Future._completeWithValue (dart:async/future_impl.dart:522:5)
#143    Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:552:7)
#144    _rootRun (dart:async/zone.dart:1124:13)
#145    _CustomZone.run (dart:async/zone.dart:1021:19)
#146    _CustomZone.runGuarded (dart:async/zone.dart:923:7)
#147    _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
#148    _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#149    _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
2019-10-15 08:44:54.520569+0100 Runner[16132:1794333] [TSBackgroundFetch configure]: 
    enableHeadless = 1;
    minimumFetchInterval = 15;
    stopOnTerminate = 0;

2019-10-15 08:44:54.525630+0100 Runner[16132:1794333] [TSBackgroundFetch addListener]: com.transistorsoft/flutter_background_fetch
2019-10-15 08:44:54.525691+0100 Runner[16132:1794333] [TSBackgroundFetch start]
2019-10-15 08:44:54.761953+0100 Runner[16132:1794545] flutter: ---- BackgroundFetch configure success: 2
2019-10-15 08:44:56.139196+0100 Runner[16132:1794545] flutter: -----setNotificationToken-----
2019-10-15 08:44:56.139413+0100 Runner[16132:1794545] flutter: fSpD_VrnO2k:APA91bEwfT083oNVxPDy-kiF9mTPZiyoQbS2owQTK18erSARU8akzANG6H1zWr5bgMpAePkx3jc3F3spSAOSsf4Wr_zj3bDEi1yw1E5UeHSJCsMh3flDxNxq2mE__FwhiP2M35szQi0l

【问题讨论】:

您是否尝试停止并重新运行应用程序(不是热重启)?因为这是MissingPluginException 错误的解决方案。 您是否在“功能”部分启用了后台模式下的远程通知?在 Xcode 中检查。 @IbrahimKarahan - 是的,我已经完全关闭了应用程序,完成了clean build folder,然后从 XCode 运行应用程序,但是没有运气。 @MilapShah - 是的,我在 Capabilities 中添加了背景模式 - 但没有乐趣 刚刚尝试了所有这些,但是同样的问题 - IOS 可能不支持此插件的 onBackgroundMessage 你解决了吗?我也面临着类似的问题。 【参考方案1】:

截至firebase_messaging 6.0.9,iOS 不支持后台消息处理。

正如您在更新日志中看到的,仅添加了对 Android 的支持:https://pub.dev/packages/firebase_messaging#515

查看此 github 问题,如果您希望尽快实施,请添加回复:https://github.com/FirebaseExtended/flutterfire/issues/47

【讨论】:

【参考方案2】:

firebase_messaging &gt; 8.0.0-dev 插件现在也支持 iOS 的后台消息处理。

更新日志:https://pub.dev/packages/firebase_messaging/versions/8.0.0-dev.11/changelog

迁移到 v8: https://firebase.flutter.dev/docs/migration/

【讨论】:

以上是关于Flutter - 在 IOS 上使用 FCM 的后台通知的主要内容,如果未能解决你的问题,请参考以下文章

未收到 Flutter FCM IOS 静默(仅数据)通知

Flutter 应用程序的 FCM 通知未显示在 iOS 系统托盘中

iOS 14+ 上的 Flutter FCM 9+

FCM 将消息从 php 复制到 iOS Flutter 应用程序

Flutter / FCM 通知未到达 Codemagic iOS 版本

Flutter FCM iOS 问题 - 在检索 FCM 令牌之前未设置 APNS 设备令牌