Flutter 推送通知应用程序背景:firebase_messaging

Tom*_*652 5 dart flutter firebase-cloud-messaging flutter-notification

我想当我的应用程序在后台运行时显示推送通知。
我正在使用flutter_local_notifications包和firebase_messaging包。

当我的应用程序是后台时,推送通知与 firebase_messaging 配合得很好。
然而,下面的方法:

 // The following handler is called, when App is in the background.
 FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
Run Code Online (Sandbox Code Playgroud)

RemoteNotification如果对象通过以下方式传递,则不会被调用RemoteMessage

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print('message from background handler');
  print("Handling a background message: ${message.messageId}");
  
  // If this is null, then this handler method is called
  RemoteNotification remoteNotification = message.notification; 

  showNotificationFromData(message.data);
}
Run Code Online (Sandbox Code Playgroud)

因此,我必须传递message.dataa 对象Map<String, dynamic>

firebaseMessagingBackgroundHandler我的问题是,当调用此处理程序方法时,我不再收到推送通知。

所以我一直在尝试使用该flutter_local_notification包来显示推送通知,但正如所说,它是“本地”的,因此它在前台工作正常,但显然不在后台(相同的代码,具有相同的数据没有显示在背景作为推送通知)。

问题 :

我需要调用firebaseMessagingBackgroundHandler处理程序来处理我的应用程序中的事件。那么当我的应用程序在后台时我可以做些什么仍然有推送通知吗?

谢谢

Tom*_*652 2

好吧,我已经找到了解决方案。它是在服务器端代码中设置content_available值。True

要替换该firebase_messaging.Notification项目,我们需要重写androidandapns配置。

这在 python 中看起来像这样:

apnsConfig = messaging.APNSConfig(
            payload=messaging.APNSPayload(
                aps=messaging.Aps(
                    content_available=True,
                )
            ),
     
        )
Run Code Online (Sandbox Code Playgroud)

我现在正在接收推送通知并调用 dart 代码内的后台处理程序。