Passing Extra Data

Push notifications can be sent with extra data to further customize where the user is directed or what happens when the notification is engaged with. This feature can be used for custom behaviors other than deep-linking or rich push, which are already handled automatically by the SDK. Marketers and Developers must determine how to handle each key–value pair.

Sending Custom Key-Value Pairs from Responsys

When designing the mobile app, marketers and mobile app developers must determine how to handle each key–value pair. The developers must code the mobile app to use the key-value data per the design, and marketers must know the values that the mobile app is expecting and how the mobile app will use the data.

To add a key-value pair:

  1. In the Custom key value section, click Add key value.

    The following picture shows where Marketers can enter the key-value pairs to pass when they create their push campaigns in Responsys.

  2. Type the key and the value in corresponding fields. Repeat for each key-value pair that you want to add.
  3. To remove a key-value pair, click x.

Handling Custom Key-Value Pairs

For APNs, you will need to make changes to your AppDelegate file to tell your application to act upon push notification "extra" data. In particular, you will be using the didReceiveRemoteNotification method to re-direct the user based on the extra data.

The following payload contains an aps dictionary with a simple alert message. The custom key u contains a URL which will be used to redirect to a webpage.

{ 
    "aps": {
         "alert": "Welcome"
     },
    "u": "https//duraham-denis.com/s"
}

For iOS add:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
	
// Implement PushIOManager method to handle notification     
[[PushIOManager sharedInstance] didReceiveRemoteNotification:userInfo];

    // Extract custom key-value pair
	NSString *urlString = [userInfo valueForKeyPath:@"u"];
    
    // handle the value appropriately  for example: opening url in app or tracking events 
	NSURL *url = [NSURL URLWithString:urlString];
	[[UIApplication sharedApplication] openURL:url];
}						
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]){
    
    // Implement PushIOManager method to handle notification 
    PushIOManager.sharedInstance().didReceiveRemoteNotification(userInfo)

    // Extract custom key-value pair
    if let urlString = userInfo["u"] as? String{
        if let url:URL = URL(string: (urlString)) {

            // handle the value appropriately  for example: opening url in app or tracking events 
            UIApplication.shared.openURL(url)
        }
    }
}

Important Note: We Strongly Recommend that Apps should let Responsys SDK handle Push Notification. App should not handle the Notification Payload directly as it contains several key attributes which are important for the internal functioning of the SDK.

UserNotificationCenterDelegate

Use the methods of the UNUserNotificationCenterDelegate protocol to handle user-selected actions from notifications, and to process notifications that arrive when your app is running in the foreground. Learn more about UserNotificationCenterDelegate here.

Running in foreground

When application running in foreground willPresentNotification will be called and it needs to override it in following way:

-(void) userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    
    // Implement PushIOManager method to handle notification 
    [[PushIOManager sharedInstance] userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];

    // Extract custom key-value pair
    NSString *urlString = [userInfo valueForKeyPath:@"u"];

    // handle the value appropriately for example: opening url in app or tracking events 
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];
}						
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, 
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
       
    // Implement PushIOManager method to handle notification 
    PushIOManager.sharedInstance().userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)

    // Extract custom key-value pair
    if let urlString = userInfo["u"] as? String {
        if let url:URL = URL(string: (urlString)) {

        // handle the value appropriately  for example: opening url in app or tracking events 
        UIApplication.shared.openURL(url)
        }
    }
}

Running in background / Launched application by tapping on notification

When application running in background or if application is launched by tapping on notification following didReceiveNotificationResponse gets called, and it needs to override in following way:

-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler{
    
    // Implement PushIOManager method to handle notification 
    [[PushIOManager sharedInstance] userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];

    // Extract custom key-value pair
    NSString *urlString = [userInfo valueForKeyPath:@"u"];

    // handle the value appropriately for example: opening url in app or tracking events 
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];
}						
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, 
withCompletionHandler completionHandler: @escaping () -> Void) {

// Implement PushIOManager method to handle notification 
PushIOManager.sharedInstance().userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)

    // Extract custom key-value pair
    if let urlString = userInfo["u"] as? String {
        if let url:URL = URL(string: (urlString)) {

        // handle the value appropriately  for example: opening url in app or tracking events 
        UIApplication.shared.openURL(url)
        }
    }
}

Learn more

iOS