【问题标题】:How to open a link when user clicks on Firebase notification in a Flutter App?当用户点击 Flutter App 中的 Firebase 通知时如何打开链接?
【发布时间】:2020-03-12 05:07:43
【问题描述】:

我在 Flutter 应用中使用 Firebase。要发送通知,我使用Firebase Messaging 插件。每当我使用 Firebase 网站发送通知时,通知只会在用户点击它时打开应用程序。我想发送一个打开 URL 而不是应用程序的通知。

我不知道这些信息是否有用:在 Firestore 上撰写通知时,我总是将 click_actionFLUTTER_NOTIFICATION_CLICK 放在“附加选项”的“自定义数据”部分中。

【问题讨论】:

  • 我认为您仍然需要通过打开应用程序的中间步骤,然后处理消息并将浏览器启动到所需的 URL。
  • @samthecodingman 好的,但应用程序如何知道用户点击了通知以及打开哪个 URL?
  • 与您在自定义数据部分中嵌入 FLUTTER_NOTIFICATION_CLICK 的方式相同。只需在同一位置添加类似于 {"action": "open-url", "url": "https://exampe.com"} 的内容
  • @samthecodingman 像这样:imgur.com/XYISEUq?
  • 没错。然后在每个通知处理程序中,查找该属性并在存在时对其进行操作。

标签: firebase flutter firebase-cloud-messaging firebase-notifications


【解决方案1】:

您可以使用 onLaunch() 和 onResume() 方法来处理通知的打开动作。

更多关于不同平台通知反应的信息,请查看以下链接: https://pub.dev/packages/firebase_messaging#receiving-messages

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

您可以使用 url_launcher 打开 URL: https://pub.dev/packages/url_launcher

【讨论】:

    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2019-05-31
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多