【问题标题】:Flutter firebase_messaging Background Handler not Executing but Messages are Arriving on iOSFlutter firebase_messaging 后台处理程序未执行但消息到达 iOS
【发布时间】:2021-06-25 04:29:34
【问题描述】:

我已经让 firebase_messaging 9.1.0 在应用程序处于前台、后台和关闭时接收并显示我的 Android 和 iOS 应用程序通知。但是在 iOS 上,后台处理程序 future 似乎没有执行。我正在运行 firebase_messaging_example 应用程序,它在后台运行时不会打印“处理后台消息 ${message.messageId}”。

在 xcode 中检查后台获取和远程通知。

我试过了:

使用和不使用 clickAction 发送通知:“FLUTER_NOTIFICATION_CLICK”

有和没有

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

在 info.plist 中

在 xcode 中检查有无后台处理。

我希望后台处理程序执行一些在应用启动器图标上设置徽章的代码。有谁知道如何让处理程序与 iOS 一起使用,或者在应用关闭时以其他方式在启动器图标上设置徽章?

【问题讨论】:

  • 数据通知和非数据通知是否都会发生这种情况?另外,您确定您没有关闭通知吗?在 iOS 上,您必须再次手动启用它们/打开应用程序才能再次显示它们。在 iOS 上,您可以选择通过配置声音/横幅/警报来设置 Flutter 中的通知设置。
  • 是的,无论有无数据都会发生。通知正在通过,它们只是没有激活后台处理程序。在 android 上,当应用程序在后台时,我可以在调试控制台中看到打印它,我可以执行显示徽章的代码,但在 iOS 中什么都没有。声音/徽章/警报选项仅适用于应用程序在前台运行时。数据中的内容是否重要?
  • 请发布您的后台消息处理程序。
  • 我正在使用来自github.com/FirebaseExtended/flutterfire/blob/master/packages/…的firebase_messaging包中未修改的示例
    处理程序是:
    Future&lt;void&gt; _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print('Handling a background message ${message.messageId}'); }
  • 请检查我的回答,我相信它会帮助你处理你的情况。

标签: flutter firebase-cloud-messaging


【解决方案1】:

根据您在 cmets 部分的回复,请尝试使用这个:

这是你的顶级函数,在void main()之上

 Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
     if (Firebase.apps.isEmpty) await Firebase.initializeApp();
     print('Handling a background message ${message.messageId}');
    }


handleNotifications() async {
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(badge: true, alert: true, sound: true);//presentation options for Apple notifications when received in the foreground.

FirebaseMessaging.onMessage.listen((message) async {
print('Got a message whilst in the FOREGROUND!');
return;
}).onData((data) {
print('Got a DATA message whilst in the FOREGROUND!');
print('data from stream: ${data.data}');
});

FirebaseMessaging.onMessageOpenedApp.listen((message) async {
  print('NOTIFICATION MESSAGE TAPPED');
return;
}).onData((data) {
print('NOTIFICATION MESSAGE TAPPED');
print('data from stream: ${data.data}');
});

FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  FirebaseMessaging.instance
      .getInitialMessage()
      .then((value) => value != null ? _firebaseMessagingBackgroundHandler : false);
return;
}

那么, 在 void main 里面,这样写:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  handleNotifications(); 
  runApp(MyAppState());
}

请让我知道这之后会发生什么,但这很可能会解决您的问题。

【讨论】:

  • 它的行为仍然相同。通知在前台、后台和应用程序关闭时到达。但是“处理后台消息 ${message.messageId}');”当应用程序在后台时仍然不显示。我尝试了有无数据。
  • 打印除了${message.messageId}');"之外的东西,它有用吗?即print('displayingTest message instead of message ID')。它可能是message.messageId 中的内容。你能确认一下吗?
  • 感谢您的帮助。它也没有工作。我只是让我的通知服务器获取数据并将其发送到有效负载中,而不是让应用程序在通知到来时处理事情。
猜你喜欢
  • 2019-11-08
  • 2013-09-12
  • 2021-06-28
  • 2019-07-24
  • 2014-02-26
  • 2013-09-20
  • 1970-01-01
  • 2020-01-29
  • 2011-05-06
相关资源
最近更新 更多