flutter - 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
处理程序来处理我的应用程序中的事件。所以当我的应用程序在后台时,我可以做些什么来推送通知?
谢谢
解决方案
好的,我找到了解决方案。这是在服务器端代码中设置content_available
值。True
要替换该firebase_messaging.Notification
项目,我们需要覆盖android
和apns
配置。
这在 python 中看起来像这样:
apnsConfig = messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
content_available=True,
)
),
)
我现在正在接收推送通知并调用飞镖代码中的后台处理程序。
推荐阅读
- r - 将序数值分配给R中的数值向量
- python - 在简单数据上使用边界和初始参数的 Scipy curve_fit 混淆
- javascript - 在正则表达式 JavaScript 中提取值
- macos - Flutter 使用终端消息安装 MacOS Catalina 失败 -bash: flutter: command not found"
- node.js - Gatsby 电子商务网站缓存问题频繁构建
- excel - 如何动态锁定 Excel 中的单元格行?
- opengl - 在OpenGL中围绕XZ平面上的一个点旋转相机
- android - 我无缘无故在Android Build中获得MultipleBuildFailure
- python - 如何使用 pytest 测试 Python Eve 预测?
- chatbot - AIML 聊天机器人可以支持来自用户的无限查询吗?