flutter - Flutter Web中收到后台通知时如何执行flutter代码?
问题描述
在 Flutter Web 中,当收到 Firebase 通知并且应用程序在前面时,我确实执行了“onMessage”处理程序。但是,当应用程序在后台时,我确实看到了带有消息预览的 Chrome 通知,但应用程序中没有执行任何操作。我找不到让服务人员“ping”正在运行的应用程序的方法,甚至无法简单地运行独立的 Flutter 代码来设置某些内容,SharedPreferences
以便用户在打开应用程序时收到完整的消息。
例如,这项工作在 Android 中按预期工作。
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
if (message.notification != null) {
SharedPreferences.getInstance().then((SharedPreferences p) {
p.setString("notification", message.notification.body.toString());
});
}
}
//[... initialization code : ...]
String notif = prefs.getString("notification");
if (notif != null) {
showSnack(notif, duration: Duration(days: 365));
prefs.remove("notification");
}
FirebaseMessaging.onBackgroundMessage(
_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
showSnack(message.notification.body.toString(),
duration: Duration(days: 365));
}
});
解决方案
推荐阅读
- python - Tensorflow/Keras GPU 使用率低:Jupyter Notebook 似乎是在 CPU 而不是 GPU 上进行训练
- python - 计算连续行之间的秒数差异为第一行返回 NaN
- excel - 根据 Excel 中的另一个单元格值验证单元格
- javascript - 如何调用主应用程序文件的函数?反应原生
- java - 如何使用视频视图和意图播放视频
- node.js - Docker 在 docker-compose up 上同时运行 Yarn update 和 npm update
- firebase - 客户的 GCP 结算帐号
- go - 测试用例中的 Golang 数据竞争条件
- android - 从 Android Sudio 3.2.1 切换到 4.1.2 后的 java.lang.UnsatisfiedLinkError
- memory-management - cudaMalloc 增加主机内存成本?