Skip to content

Commit

Permalink
Adds deeplink navigation for push notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
pushchris committed Jul 7, 2023
1 parent 3f38475 commit 5ebd1c2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Parcelvoy.shared.register(token: deviceToken)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
Parcelvoy.shared.handle(application, userInfo: userInfo)
return .newData
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

guard let url = userActivity.webpageURL else {
Expand Down
2 changes: 1 addition & 1 deletion Parcelvoy.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Parcelvoy'
s.version = '0.1.1'
s.version = '0.2.0'
s.summary = 'A short description of Parcelvoy.'

# This description is used to generate tags and improve search results.
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,27 @@ Parcelvoy.shared.track(
)
```

### Register Device
### Notifications
#### Register Device
In order to send push notifications to a given device you need to register for notifications and then register the device with Parcelvoy. You can do so by using the `register(token: Data?)` method. If a user does not grant access to send notifications, you can also call this method without a token to register device characteristics.
```swift
Parcelvoy.shared.register(token: "APN_TOKEN_DATA")
```

### Deeplink Navigation
#### Handle Notifications
When a notification is received it can contain a deeplink that will trigger when a user opens it. To properly handle the routing you need to pass the received push notification to the Parcelvoy handler.
```swift
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any]
) async -> UIBackgroundFetchResult {
Parcelvoy.shared.handle(application, userInfo: userInfo)
return .newData
}

```

### Deeplink & Universal Link Navigation
To allow for click tracking links in emails can be click-wrapped in a Parcelvoy url that then needs to be unwrapped for navigation purposes. For information on setting this up on your platform, please see our [deeplink documentation](https://docs.parcelvoy.com/advanced/deeplinking).

Parcelvoy includes a method which checks to see if a given URL is a Parcelvoy URL and if so, unwraps the url, triggers the unwrapped URL and calls the Parcelvoy API to register that the URL was executed.
Expand Down
41 changes: 34 additions & 7 deletions Sources/Parcelvoy/Parcelvoy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ public class Parcelvoy {
///
/// - Parameters:
/// - universalLink: The URL that the app is trying to open
///
@discardableResult
public func handle(universalLink: URL) -> Bool {
guard isParcelvoyDeepLink(url: universalLink.absoluteString),
let queryParams = universalLink.queryParameters,
Expand All @@ -212,23 +214,48 @@ public class Parcelvoy {
self.network?.request(request: request)

/// Manually redirect to the URL included in the parameter
let userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
userActivity.webpageURL = redirectUrl
let _ = UIApplication.shared.delegate?.application?(
UIApplication.shared,
continue: userActivity,
restorationHandler: { _ in }
)
open(url: redirectUrl)
return true
}

/// Handle push notification receipt
///
/// Push notifications may come with an internal redirect to execute when
/// the notification is opened. This method opens a URL if one is provided
/// and returns if there was a match or not.
///
/// - Parameters:
/// - application: The application surounding the context of the notification
/// - userInfo: The dictionary of attributes included in the notification
///
@discardableResult
public func handle(_ application: UIApplication, userInfo: [AnyHashable: Any]) -> Bool {
if let _ = userInfo["method"] as? String,
let urlString = userInfo["url"] as? String,
let url = URL(string: urlString) {
open(url: url)
return true
}
return false
}

public func isParcelvoyDeepLink(url: String) -> Bool {
guard let endpoint = self.config?.urlEndpoint else {
return false
}
return url.starts(with: "\(endpoint)/c")
}

private func open(url: URL) {
let userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
userActivity.webpageURL = url
let _ = UIApplication.shared.delegate?.application?(
UIApplication.shared,
continue: userActivity,
restorationHandler: { _ in }
)
}

/// Reset session such that a new anonymous ID is generated
///
public func reset() {
Expand Down

0 comments on commit 5ebd1c2

Please # to comment.