Flutter 推送通知应用背景:firebase_messaging
Posted
技术标签:
【中文标题】Flutter 推送通知应用背景:firebase_messaging【英文标题】:Flutter push notifications app background : 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<String, dynamic>
。
我的问题是,当调用此 firebaseMessagingBackgroundHandler
处理程序方法时,我不再收到推送通知。
所以我一直在尝试使用flutter_local_notification
包来显示推送通知,但正如它所说,它是“本地的”,所以它在前台工作正常,但显然不在后台(相同的代码,与相同的数据不会在后台显示为推送通知)。
问题:
我需要调用firebaseMessagingBackgroundHandler
处理程序来处理我的应用程序中的事件。所以当我的应用程序在后台时,我可以做些什么来推送通知?
谢谢
【问题讨论】:
【参考方案1】:好的,我找到了解决方案。就是在服务端代码中将content_available
的值设置为True
。
要替换 firebase_messaging.Notification
项目,我们需要覆盖 android
和 apns
配置。
这在 python 中是这样的:
apnsConfig = messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
content_available=True,
)
),
)
我现在正在接收推送通知并调用 dart 代码中的后台处理程序。
【讨论】:
以上是关于Flutter 推送通知应用背景:firebase_messaging的主要内容,如果未能解决你的问题,请参考以下文章
如果应用程序未启动,Flutter Firebase 推送通知无法数据
在 Flutter 应用上向用户发送独特的 Firebase 推送通知
如何使用firebase推送通知唤醒flutter android应用程序
Flutter Firebase前台推送通知未显示但后台正在运行