How do I insert an external web link into an iOS push notification?

Mixpanel’s iOS push notifications allow for deeplinking a user to a web link. To do so, use a custom JSON payload in the Push builder and modify the AppDelegate in the App build to listen for a custom JSON key from a terminated state. Read more about deep linking in iOS and Android.

Look for the Custom Data field in the message builder that allows for including JSON objects. Unlike Mixpanel’s Android SDK, there is no JSON key that will allow for automatic handling of web links. As such, the web link can be passed along under any custom parameter name.

In this example, let’s name this parameter “web_link.” Here’s a sample of the complete JSON payload with a custom "web_link" key containing the URL:

{"aps": {"alert": "TEXT GOES HERE"}, "web_link": "https://www.mixpanel.com"}

If the AppDelegate is set up correctly to handle this custom key, the push notification should link directly to the URL specified above. For example, the following expects the custom “web_link” key in didReceiveRemoteNotification and parses out the link value based on that key. Then, convert the string to a URL and invoke openURL:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo{
    [[Mixpanel sharedInstance] trackPushNotification:userInfo];
    NSLog(@"%@", userInfo);
    NSString *web_link = [userInfo objectForKey:@"web_link"];
    NSLog(@"%@", web_link);
    NSURL *url = [[NSURL alloc] initWithString:web_link];
    if (url){
         [[UIApplication sharedApplication] openURL:url];
    }
}

Lastly, include the following in didFinishLaunchingWithOptions to ensure that didReceiveRemoteNotification is triggered when the app is launched from a terminated state:

if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
    }

There is no need to manually handle the case where the app is backgrounded or active, as didReceiveRemoteNotification will be called automatically.