【发布时间】:2021-06-02 02:22:55
【问题描述】:
当用户点击通知时,我想导航到某个页面。我正在使用云消息传递和flutter_local_notifications。我已经设法通过前台通知来做到这一点。这很简单。我将相同的代码粘贴到我的后台通知处理程序中,但没有用。我也在寻找通知的 onTap 回调,但找不到与此相关的任何内容。
这是我的后台通知处理程序。
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
var androidDetails = AndroidNotificationDetails(
"Channel ID",
"Shannonside",
"Shannonside Channel",
);
var iosDetails = IOSNotificationDetails();
var details = NotificationDetails(
android: androidDetails,
iOS: iosDetails,
);
if (message.notification != null) {
final title = message.notification.title;
final body = message.notification.body;
await NotificationService.localNotification.show(0, title, body, details);
}
if (message.data != null) {
var articleId = message.data['articleId'];
var category = message.data['category'];
if (articleId != null && category != null) {
print("ArticleID: $articleId Category $category");
//@TODO Add navigation service and move to the article detail
NavigatorService.instance.navigateTo("/articlePage", arguments: {
"articleId": articleId.toString().toLowerCase(),
"category": category.toString().toLowerCase(),
});
}
}
}
它不起作用,甚至我的功能都没有启动。他们还在文档中声明这是不可能的。
由于处理程序在您的应用程序上下文之外在其自己的隔离中运行,因此无法更新应用程序状态或执行任何影响 UI 的逻辑。但是,您可以执行诸如 HTTP 请求、IO 操作(更新本地存储)、与其他插件通信等逻辑。
我知道有些应用会这样做,当您单击通知时它们会打开一些页面。就像一个动态链接。我想在我的应用中实现它。
【问题讨论】:
-
@Andrej 我已经阅读了有关 Firebase 消息传递的 FlutterFire 文档。还粘贴了文档中的一些引用。如果您认为我遗漏了什么,请告诉我,而不是发送整个文档。
标签: firebase flutter firebase-cloud-messaging