首页 > 解决方案 > 如何在通知点击时将用户重定向到页面

问题描述

我正在使用 Firebase 服务器开发 Flutter 应用程序和 Web。我使用云功能发送通知。我想在用户收到通知并单击它时将其重定向到特定页面。目前,当我收到通知并单击它时,我的应用程序会显示“主页”。当用户收到通知并单击它时,如何重定向到“请求详细信息页面”而不是“主页”?

标签: functionfluttercloud

解决方案


在 AndroidManifest 文件中添加这个

<intent-filter>
  <action android:name="FLUTTER_NOTIFICATION_CLICK" />
  <category android:name="android.intent.category.DEFAULT" />

在通知数据中,添加 click_action 和导航到特定页面所需的参数

'data': {
     'click_action': 'FLUTTER_NOTIFICATION_CLICK',
     'product_id': 1,
     ...
   },

当用户点击通知时,这三个事件之一将根据您的应用状态触发。

_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
    _showItemDialog(message);
  },
  onBackgroundMessage: myBackgroundMessageHandler,
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
    _navigateToItemDetail(message);
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
    _navigateToItemDetail(message);
  },
);

从消息中检索通知数据并导航到您想要的页面。


推荐阅读