Skip to content

Commit

Permalink
Allow the configuration of the notification name
Browse files Browse the repository at this point in the history
  • Loading branch information
anonimitoraf committed Nov 8, 2023
1 parent 5df0210 commit 62f78f7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 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. Override 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
14 changes: 9 additions & 5 deletions ios/Plugin/FirebaseDynamicLinksPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import FirebaseDynamicLinks

typealias JSObject = [String:Any]

extension Notification.Name {
public static let firebaseDynamicLink = Notification.Name(rawValue: "FirebaseDynamicLink")
}

@objc(CapacitorFirebaseDynamicLinks)
public class CapacitorFirebaseDynamicLinks: CAPPlugin {

Expand All @@ -17,8 +13,16 @@ public class CapacitorFirebaseDynamicLinks: CAPPlugin {
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.firebaseDynamicLink, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleUniversalLink(notification:)), name: universalLinkNotificationName, object: nil)
}

@objc func handleUrlOpened(notification: NSNotification) {
Expand Down

0 comments on commit 62f78f7

Please sign in to comment.