Deep Linking

MessagingSDK allows for Push Notifications to contain a link to a section inside of the Application or to a Web Page link.

For this we have 3 modalities:

  1. Explicit behavior using Payload

  2. URL Schema

  3. Universal Links

Payload

To use this modality, indicate the behavior that will occur once the Push Notification is received, in the following way:

...
import MessagingSDK @UIApplicationMain
class AppDelegate: MessagingAppDelegate or ... MessagingUserNotificationDelegate { ...
override func messagingReceivedDeepLink(_ notification: MessagingNotification) { let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
if let root = window?.rootViewController as? UINavigationController,
let detail = storyboard.instantiateViewController(withIdentifier: "detailVCI detail.notification = notification
root.pushViewController(detail, animated: true)
} }
...
}

In this example, we ask if the root view of the application is a navigation controller and we look in the Main.storyboard file for a view controller that has the detailVCId identifier, if both premises are correct.

You pass the notification parameter to make use of the data within the definition of my class NotificationDetailViewController and you place this screen for the user to visualize it.

MessagingReceivedDeepLink is a function that is invoked when a notification of type deeplink is received and is found in MessagingAppDelegate or MessagingUserNotificationDelegate, the idea is to define what to do with the overwriting of this method.

Note: For the execution of this method, the user is required to interact with the Push Notification, so it must be shown on the screen with themessagingReceivedNotificationfunction:

override func messagingReceivedNotification(_ notification: MessagingNotification, w -> UNNotificationPresentationOptions {
if notification.deepLink {                                        
return [.alert, .sound] }                                        
} 
...
}

In this example, if a Deep Link Notification is received, it is displayed on the screen.

URL Schema

A linking Scheme allows you to associate a link with your Application.

This approach uses the linking scheme to determine the Application linked to that Scheme.

Once configured, you will have a link that will open your Application and process this link as needed.

To associate a URL Scheme to your Application, open your project settings, enter into the Info tab, and click on the URL types section.

image11.png

This is an example of a linking scheme:

yourapp://sectionapp?param1=value1&...&paramN=valueN

You can use this link in a Push Notification or Website. Indicate that it is a Deeplink Push Notification and enter the link in an attribute called Schema.

As an option, you may put other parameters of interest since these will be added to the URL when it is being processed.

Example: Website
...
<a href="yourapp://sectionapp?param1=value1&...&paramN=valueN">See more</a> 
...
Example: Push Notification Data
...
"data": {
 "deepLink": true,
 "schema":   "yourapp://sectionapp?param1=value1&...&paramN=valueN", "another1": "value1",
 ...
 "anotherX": "valueX"
}
...

The following method is used to indicate the behavior that will be done to process the URL:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOpt 
   // your code
  // example: open your view controller using the data url.
  return true
}

Now all the information received by the URL can be extracted from the URL object, or if you wish, use another structure like NSURLComponents to support the methods you have and obtain the path or the query parameters

In this example, we use NSURLComponents to extract the information from the URL and place it in a dictionary:

var urlData:[String: Any?] = [:]
urlData["url.absoluteString"] = url.absoluteString
if let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true){

Universal Link

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.