【问题标题】:Flutter push notifications app background : firebase_messagingFlutter 推送通知应用背景:firebase_messaging
【发布时间】:2021-08-09 23:41:34
【问题描述】:

我想在我的应用处于后台时显示推送通知。
我正在使用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&lt;String, dynamic&gt;

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

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

问题:

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

谢谢

【问题讨论】:

    标签: flutter dart firebase-cloud-messaging flutter-notification


    【解决方案1】:

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

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

    这在 python 中是这样的:

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 2021-07-24
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2019-09-11
      • 2020-06-29
      相关资源
      最近更新 更多