简体   繁体   中英

Listen to iOS Capacitor notifications in Ionic React

I'm working with Ionic framework in React. I've created an iOS app from it using the Capacitor. Now in the Capacitor app, in AppDelegate when application(_:open:options:) is called, capacitor calls a handlerOpenUrl(_:_:) method which in turn posts the notifications .

  func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    return CAPBridge.handleOpenUrl(url, options)
  }

  public static func handleOpenUrl(_ url: URL, _ options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
    NotificationCenter.default.post(name: Notification.Name(CAPNotifications.URLOpen.name()), object: [
      "url": url,
      "options": options
    ])
    NotificationCenter.default.post(name: NSNotification.Name.CDVPluginHandleOpenURL, object: url)
    CAPBridge.lastUrl = url
    return true
  }

Now, I want to listen to these notifications in my ionic react codebase so I can show the url and options on the UI.

I couldn't find any relevant content on how to resolve this.

For Capacitor notification use App plugin

import { Plugins } from '@capacitor/core';

const { App } = Plugins;

App.addListener('appUrlOpen', (data: any) => {
  console.log('App opened',  data.url, data.options);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM