我想在我的应用在后台时显示推送通知。
我正在使用 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
处理程序来处理我的应用程序中的事件。所以我可以在我的应用程序在后台时仍然有推送通知吗?
谢谢
答案 0 :(得分:0)
好的,我找到了解决方案。它是在服务器端代码中将 content_available
值设置为 True
。
要替换 firebase_messaging.Notification
项,我们需要覆盖 android
和 apns
配置。
这在python中看起来像这样:
apnsConfig = messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
content_available=True,
)
),
)
我现在正在接收推送通知并在 dart 代码中调用后台处理程序。