【发布时间】:2021-07-25 07:47:55
【问题描述】:
如文档所述,处理后台消息时。
由于后台处理程序在应用程序上下文之外在其自己的隔离中运行,因此无法更新应用程序状态或执行任何影响 UI 的逻辑。但是,您可以执行诸如 HTTP 请求、IO 操作(更新本地存储)、与其他插件通信等逻辑。
来自文档的后台处理程序代码部分。
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
执行 IO 操作(更新本地存储)的方式是什么?
【问题讨论】: