diff --git a/README.md b/README.md index 0f801bb..b385a5d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,71 @@ Nothing more needed None +## Configuration + +| Name | Type | Description | +| ------------------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------- | +| universalLinksNotificationName | string | (IOS only) Overrides the default notification name used by Capacitor. Configure this if you have other plugins (e.g. Apps Flyer) that also use deep links. See more info below | + +Provide configuration in root `capacitor.config.json` + +```json +{ + "plugins": { + "FirebaseDynamicLinks": { + "universalLinksNotificationName": "firebaseOpenUniversalLink" + } + } +} +``` + +or in `capacitor.config.ts` + +```ts +const config: CapacitorConfig = { + plugins: { + FirebaseDynamicLinks: { + universalLinksNotificationName: 'firebaseOpenUniversalLink', + }, + }, +}; +``` + +### (IOS only) Additional set up for `universalLinksNotificationName` + +If you use this configuration, you need to intercept Firebase Dynamic links in your `AppDelegate.swift` file. For example: + +``` swift + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + ...omitted for brevity + + func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { + // To get Firebase dynamic links to play along nicely with Apps Flyer, + // we intercept them and use a custom notification name + let url = userActivity.webpageURL?.absoluteString + if (url != nil && isFirebaseDynamicLink(url: url!)) { + // The notification name has to match what you've specified in your Capacitor config + NotificationCenter.default.post(name: Notification.Name("firebaseOpenUniversalLink"), object: [ + "url": userActivity.webpageURL + ]) + return true + } + + // Called when the app was launched with an activity, including Universal Links. + // Feel free to add additional processing here, but if you want the App API to support + // tracking app url opens, make sure to keep this call + return ApplicationDelegateProxy.shared.application(application, continue: userActivity, restorationHandler: restorationHandler) + } + + func isFirebaseDynamicLink(url: String) -> Bool { + return url.hasPrefix("https://your-firebase-links.page.link") + } +} +``` + ## Methods ### AddListener diff --git a/ios/Plugin/FirebaseDynamicLinksPlugin.swift b/ios/Plugin/FirebaseDynamicLinksPlugin.swift index cf054ca..cf19a4c 100644 --- a/ios/Plugin/FirebaseDynamicLinksPlugin.swift +++ b/ios/Plugin/FirebaseDynamicLinksPlugin.swift @@ -7,16 +7,24 @@ typealias JSObject = [String:Any] @objc(CapacitorFirebaseDynamicLinks) public class CapacitorFirebaseDynamicLinks: CAPPlugin { - + public override func load() { if (FirebaseApp.app() == nil) { FirebaseApp.configure() } + var universalLinkNotificationName = Notification.Name.capacitorOpenUniversalLink + + let config = self.getConfig() + let universalLinkConfig = config.getString("universalLinksNotificationName") + if (universalLinkConfig != nil) { + universalLinkNotificationName = Notification.Name(universalLinkConfig!) + } + NotificationCenter.default.addObserver(self, selector: #selector(self.handleUrlOpened(notification:)), name: Notification.Name.capacitorOpenURL, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(self.handleUniversalLink(notification:)), name: Notification.Name.capacitorOpenUniversalLink, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(self.handleUniversalLink(notification:)), name: universalLinkNotificationName, object: nil) } - + @objc func handleUrlOpened(notification: NSNotification) { guard let object = notification.object as? [String:Any?] else { return @@ -25,12 +33,12 @@ public class CapacitorFirebaseDynamicLinks: CAPPlugin { guard let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: object["url"] as! URL) else { return } - + let params = dynamicLink.url?.query ?? "" let path = dynamicLink.url?.path ?? "" self.sendNotification(path: path, params: params) } - + @objc func handleUniversalLink(notification: NSNotification) { guard let object = notification.object as? [String:Any?] else { return @@ -42,7 +50,7 @@ public class CapacitorFirebaseDynamicLinks: CAPPlugin { self.sendNotification(path: path, params: params) } } - + func sendNotification(path: String, params: String) { let data = ["slug": path, "query": params ] self.notifyListeners("deepLinkOpen", data: data, retainUntilConsumed: true) diff --git a/package.json b/package.json index 8790f9d..f8458c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@joinflux/capacitor-firebase-dynamic-links", - "version": "2.0.0", + "version": "2.1.0", "description": "Capacitor Plugin for Firebase Dynamic Links", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js",