{"_id":"5b1f2cbdfd653400031d8d76","__v":10,"project":"55e67aaa9cc7c62b00c4a1ea","user":"55d29988486de50d00327118","version":{"_id":"5b1f2cbdfd653400031d8d9f","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"forked_from":"55e67aab9cc7c62b00c4a1ed","createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z","categories":["5b1f2cbdfd653400031d8d42","5b1f2cbdfd653400031d8d43","5b1f2cbdfd653400031d8d44","5b1f2cbdfd653400031d8d45","5b1f2cbdfd653400031d8d46","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","5b1f2cbdfd653400031d8d47","5b1f2cbdfd653400031d8d48","5af0fe494ca2730003cbc98a","5af0fe55ec80af0003804ca2"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"rebrand","version_clean":"1.3.0","version":"1.3"},"category":{"_id":"5b1f2cbdfd653400031d8d47","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"version":"5b1f2cbdfd653400031d8d9f","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-02-08T21:31:11.878Z","from_sync":false,"order":7,"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":4,"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/8fc1b9e-image_12.png\",\n        \"image_12.png\",\n        566,\n        460,\n        \"#e4ecf3\"\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.","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/8fc1b9e-image_12.png", "image_12.png", 566, 460, "#e4ecf3" ] } ] } [/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.