Handle Custom Keys Sent with Message Payload

Send additional data with your push notifications using custom keys. Custom keys let you pass information beyond the standard notification title, body, and badge. Additional information can include product IDs, user preferences, tracking parameters, or any data your app needs. When a user taps a notification, your app can extract these custom values from the notification’s userInfo dictionary and use them to navigate to specific content, update the UI, trigger analytics events, or perform other custom logic.

For example, an e-commerce app can include a product ID in a custom key to open the product detail page directly. A news app can pass an article ID to display the full story. Or you can include tracking parameters that third-party analytics tools use to measure campaign effectiveness.

Enable custom keys on the Administration page in Marketing Cloud Engagement Setup before sending notifications with custom data.

The MobilePush Administration page in Marketing Cloud Engagement, showing the Custom Keys option enabled.

Next, implement custom key support in your app by extending your push notification handler to extract the push’s userInfo dictionary and the values it contains.

This code example shows how to handle custom keys using version 10 or later of the SDK.

1// The method is called on the delegate when the user responds to the
2// notification by opening the app, dismissing the notification, or choosing a
3// UNNotificationAction. Set the delegate before the application returns from
4// applicationDidFinishLaunching:.
5func userNotificationCenter(
6  _ center: UNUserNotificationCenter,
7  didReceive response: UNNotificationResponse,
8  withCompletionHandler completionHandler: @escaping () -> Void
9) {
10  // tell the SDK about the notification
11  PushFeature.requestSdk { pushFeature in
12    pushFeature?.setNotificationResponse(response)
13  }
14  // the dictionary containing custom keys
15  let userInfo = response.notification.request.content.userInfo
16  let someValue = userInfo["someKey"] as? String
17  // App-specific usage
18  print(someValue ?? "someValue is nil")
19  completionHandler()
20}

For version 9 of the SDK, use this code.

1// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
2func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
3    // tell the SDK about the notification
4    SFMCSdk.requestPushSdk { mp in
5        mp.setNotificationResponse(response)
6    }
7    // the dictionary containing custom keys
8    let userInfo = response.notification.request.content.userInfo
9    let someValue = userInfo["someKey"] as? String
10    // application-specific usage follows
11    print(someValue ?? "someValue is nil")
12    completionHandler()
13}

For version 8.1 of the SDK, use this code.

1// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
2func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
3    // tell the SDK about the notification
4    SFMCSdk.requestPushSdk { mp in
5        mp.setNotificationRequest(response.notification.request)
6    }
7    // the dictionary containing custom keys
8    let userInfo = response.notification.request.content.userInfo
9    let someValue = userInfo["someKey"] as? String
10    // application-specific usage follows
11    print(someValue ?? "someValue is nil")
12    completionHandler()
13}

For version 8.0 of the SDK, use this code.

1// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
2func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
3    // Tell the SDK about the notification
4    // See Step 5: Capture Notifications on Launch only for 8.0.x under Migrate to Mobile Push SDK Version 8.x for iOS to capture the notification request.
5    mp.setNotificationRequest(response.notification.request)
6
7    // the dictionary containing custom keys
8    let userInfo = response.notification.request.content.userInfo
9    let someValue = userInfo["someKey"] as? String
10    // application-specific usage follows
11    print(someValue ?? "someValue is nil")
12    completionHandler()
13}

For version 7 of the SDK, use this code.

1// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
2func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
3    // tell the MarketingCloudSDK about the notification
4    MarketingCloudSDK.sharedInstance().sfmc_setNotificationRequest(response.notification.request)
5    // the dictionary containing custom keys
6    let userInfo = response.notification.request.content.userInfo
7    let someValue = userInfo["someKey"] as? String
8    // application-specific usage follows
9    print(someValue ?? "someValue is nil")
10    completionHandler()
11}