首页 > 解决方案 > Flutter 推送通知应用程序背景:firebase_messaging

问题描述

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

推送通知与 firebase_messaging 以及当我的应用程序是后台时效果很好。
但是,以下方法:

 // The following handler is called, when App is in the background.
 FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

如果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);
}

因此,我必须传递message.data一个Map<String, dynamic>.

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

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

问题 :

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

谢谢

标签: flutterdartfirebase-cloud-messagingflutter-notification

解决方案


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

要替换该firebase_messaging.Notification项目,我们需要覆盖androidapns配置。

这在 python 中看起来像这样:

apnsConfig = messaging.APNSConfig(
            payload=messaging.APNSPayload(
                aps=messaging.Aps(
                    content_available=True,
                )
            ),
     
        )

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


推荐阅读