{"_id":"561c6592be5fb20d0007774a","__v":10,"project":"55e67aaa9cc7c62b00c4a1ea","user":"55d29988486de50d00327118","version":{"_id":"55e67aab9cc7c62b00c4a1ed","project":"55e67aaa9cc7c62b00c4a1ea","__v":10,"createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z","categories":["55e67aac9cc7c62b00c4a1ee","55e67b5556007d23005fee7d","55e67b5dde6fef23009480ca","55e680efde6fef23009480db","55e6829485a9741900314e99","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","589b8e1fdbb7cd190026732c","58b8ca5e3265d70f001788d4"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"589b8e1fdbb7cd190026732c","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"version":"55e67aab9cc7c62b00c4a1ed","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-02-08T21:31:11.878Z","from_sync":false,"order":6,"slug":"advanced-techniques","title":"Advanced Techniques"},"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-10-13T01:59:46.676Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"Key Value Payloads are a great tool for sending custom data to your application inside of a push notification. You can specify the key and value in the push creation pane of the message creation screen. \n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/ddPaqExoTv2PCAbkb8Y6_push-payload.png\",\n        \"push-payload.png\",\n        \"633\",\n        \"336\",\n        \"#4e869b\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n## iOS\n\nOn iOS, you need to implement two methods and use the `userInfo` dictionary to retrieve the value for a given key.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {\\n        [[Game shared] setLevel:userInfo[:::at:::\\\"special_level_jump_award\\\"];\\n    }\\n\\n    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\\n        NSDictionary *pushPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];\\n\\n        if (pushPayload) {\\n            [[Game shared] setLevel:pushPayload[@\\\"special_level_jump_award\\\"];\\n        }\\n    }\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nOn iOS, you can also create [Deep Links](doc:deep-linking) in the same way to parts of your application *without* creating an In App Message via push payloads. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {\\n    if ([[userInfo objectForKey:@\\\"screen\\\"] isEqualToString:@\\\"new_post\\\"]) {\\n        NewPostViewController *npViewController = [NewPostViewController alloc] init];\\n        [self.window.rootViewController presentViewController:npViewController];\\n    }\\n}\\n\\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\\n    NSDictionary *pushPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];\\n\\n    if (pushPayload) {\\n        if ([[pushPayload objectForKey:@\\\"screen\\\"] isEqualToString:@\\\"new_post\\\"]) {\\n            NewPostViewController *npViewController = [NewPostViewController alloc] init];\\n            [self.window.rootViewController presentViewController:npViewController];\\n        }\\n    }\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n## Android\n\nOn android the key value payload is passed through as a bundle on the Intent. If the application is not currently running, then you can get the bundle from the launching Intent:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"protected void onCreate(Bundle savedInstanceState) {\\n  ...\\n  Bundle extras = getIntent().getExtras();\\n  if (extras != null) {\\n    myValue = extras.get(\\\"myKey\\\");\\n  }\\n  ...\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nIf the receiving Activity is currently running, then the intent is passed to the Activity's `onNewIntent(intent)` method.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"protected void onNewIntent(Intent intent) {\\n\\tsuper.onNewIntent(intent);\\n\\t\\n  Bundle extras = intent.getExtras();\\n  if (extras != null) {\\n    myValue = extras.get(\\\"myKey\\\");\\n  }\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"On Android, Key-Value Payloads can only be used with standard push notifications, not Messages. This is because the data is passed to the Activity launched by the pending intent in the push notification, and Message pushes open directly to the Full Screen Message.\"\n}\n[/block]\n# Deep Linking with Key-Value payloads\n\nFor both iOS and Android, `_u` is a special key that is handled differently to other Key-Value payloads. If found in a push payload, tapping that notification will instruct the operating system to open it. In the case of deep links, this means that your app will be called back. \n\nThe ways these are called back are the same as described in [Deep Linking](doc:deep-linking).\n\nIf the app is open on iOS, nothing will happen. If it is open on Android, you will still get the notification in your system tray, as per convention. \n\nUsing `_u` will override your notification listeners and observers. If you wish to always trigger your code, simply use another key for deep linking and handle them yourself.\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"For Android, we will attribute an impression towards a push only if it deep links into the app.\\n\\nIf the deep link goes directly to the browser, we won't attribute an impression towards a push because the App never opened (it opened a browser instead).\\n\\nAlso, if the app is already open and the user taps on a notification, that session won't be attributed to that notification because it has already started.\\n\\nYou can change the way impressions are attributed by using another key for deep linking and handling them yourself. In this case, your app should always open, track an impression, then launch a relevant Activity (a browser for an external link, or an Activity from your app).\",\n  \"title\": \"Impression Count on Android\"\n}\n[/block]","excerpt":"","slug":"key-value-payloads","type":"basic","title":"Key-Value Payloads"}

Key-Value Payloads


Key Value Payloads are a great tool for sending custom data to your application inside of a push notification. You can specify the key and value in the push creation pane of the message creation screen. [block:image] { "images": [ { "image": [ "https://files.readme.io/ddPaqExoTv2PCAbkb8Y6_push-payload.png", "push-payload.png", "633", "336", "#4e869b", "" ] } ] } [/block] ## iOS On iOS, you need to implement two methods and use the `userInfo` dictionary to retrieve the value for a given key. [block:code] { "codes": [ { "code": " - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {\n [[Game shared] setLevel:userInfo[@\"special_level_jump_award\"];\n }\n\n - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\n NSDictionary *pushPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];\n\n if (pushPayload) {\n [[Game shared] setLevel:pushPayload[@\"special_level_jump_award\"];\n }\n }\n", "language": "objectivec" } ] } [/block] On iOS, you can also create [Deep Links](doc:deep-linking) in the same way to parts of your application *without* creating an In App Message via push payloads. [block:code] { "codes": [ { "code": "- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {\n if ([[userInfo objectForKey:@\"screen\"] isEqualToString:@\"new_post\"]) {\n NewPostViewController *npViewController = [NewPostViewController alloc] init];\n [self.window.rootViewController presentViewController:npViewController];\n }\n}\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\n NSDictionary *pushPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];\n\n if (pushPayload) {\n if ([[pushPayload objectForKey:@\"screen\"] isEqualToString:@\"new_post\"]) {\n NewPostViewController *npViewController = [NewPostViewController alloc] init];\n [self.window.rootViewController presentViewController:npViewController];\n }\n }\n}", "language": "objectivec" } ] } [/block] ## Android On android the key value payload is passed through as a bundle on the Intent. If the application is not currently running, then you can get the bundle from the launching Intent: [block:code] { "codes": [ { "code": "protected void onCreate(Bundle savedInstanceState) {\n ...\n Bundle extras = getIntent().getExtras();\n if (extras != null) {\n myValue = extras.get(\"myKey\");\n }\n ...\n}", "language": "java" } ] } [/block] If the receiving Activity is currently running, then the intent is passed to the Activity's `onNewIntent(intent)` method. [block:code] { "codes": [ { "code": "protected void onNewIntent(Intent intent) {\n\tsuper.onNewIntent(intent);\n\t\n Bundle extras = intent.getExtras();\n if (extras != null) {\n myValue = extras.get(\"myKey\");\n }\n}", "language": "java" } ] } [/block] [block:callout] { "type": "warning", "body": "On Android, Key-Value Payloads can only be used with standard push notifications, not Messages. This is because the data is passed to the Activity launched by the pending intent in the push notification, and Message pushes open directly to the Full Screen Message." } [/block] # Deep Linking with Key-Value payloads For both iOS and Android, `_u` is a special key that is handled differently to other Key-Value payloads. If found in a push payload, tapping that notification will instruct the operating system to open it. In the case of deep links, this means that your app will be called back. The ways these are called back are the same as described in [Deep Linking](doc:deep-linking). If the app is open on iOS, nothing will happen. If it is open on Android, you will still get the notification in your system tray, as per convention. Using `_u` will override your notification listeners and observers. If you wish to always trigger your code, simply use another key for deep linking and handle them yourself. [block:callout] { "type": "info", "body": "For Android, we will attribute an impression towards a push only if it deep links into the app.\n\nIf the deep link goes directly to the browser, we won't attribute an impression towards a push because the App never opened (it opened a browser instead).\n\nAlso, if the app is already open and the user taps on a notification, that session won't be attributed to that notification because it has already started.\n\nYou can change the way impressions are attributed by using another key for deep linking and handling them yourself. In this case, your app should always open, track an impression, then launch a relevant Activity (a browser for an external link, or an Activity from your app).", "title": "Impression Count on Android" } [/block]