Skip to content

Commit

Permalink
Merge pull request #7 from joinflux/feature/configurable-universal-li…
Browse files Browse the repository at this point in the history
…nks-notification-name
  • Loading branch information
anonimitoraf authored Nov 13, 2023
2 parents 8edf070 + 6b5c472 commit e96c47d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions ios/Plugin/FirebaseDynamicLinksPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit e96c47d

Please sign in to comment.