{"__v":6,"_id":"55a6a23eaaf9cf19001151e2","category":{"__v":26,"_id":"55a4ff5b2e70c0250038050f","pages":["55a4ff7d750a9a23005332af","55a4ff8b750a9a23005332b1","55a4ff9b750a9a23005332b3","55a60bfcaaf9cf1900114efb","55a6184880c8a30d00b32526","55a61ba780c8a30d00b32532","55a61c97aaf9cf1900114f40","55a61ea9aaf9cf1900114f48","55a6206580c8a30d00b32544","55a64277aaf9cf1900114fc2","55a694d1aaf9cf1900115102","55a6a23eaaf9cf19001151e2","55a6a9b389c9da1900e2a41d","55a6aba45f88a70d0065b255","55a90687c8bd450d000dd157","55af84f3aa902f1700300daa","55afa3e3902fd51700f5f858","55b0cc5cb3171b3700b153fa","56015bdc3aa0520d00da0ced","5603fe3490ee490d004404c2","5633dd18d28a340d004004f5","56448c697a8cb50d00a3ea3f","56d7a2ec5208281500a2506c","56d859b8b159f10b00304577","56d9822add90610b002708a1","56ef44c6e8d6fa17006f244f"],"project":"5540ce1b31827a0d007ab1cc","version":"5540ce1c31827a0d007ab1cf","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-07-14T12:23:55.603Z","from_sync":false,"order":2,"slug":"features","title":"Features"},"parentDoc":null,"project":"5540ce1b31827a0d007ab1cc","user":"55a5003a750a9a23005332b6","version":{"__v":31,"_id":"5540ce1c31827a0d007ab1cf","project":"5540ce1b31827a0d007ab1cc","createdAt":"2015-04-29T12:27:08.390Z","releaseDate":"2015-04-29T12:27:08.390Z","categories":["5540ce1c31827a0d007ab1d0","5540d91bbb9e762d00f594ad","5540e5f131827a0d007ab212","5540e5febb9e762d00f594d3","5540e61331827a0d007ab213","5540e6195cf9682100d61afa","5540e62631827a0d007ab214","5540e63031827a0d007ab215","5540e63531827a0d007ab216","5540e63e5cf9682100d61afc","5540e6445cf9682100d61afd","5540e64a5cf9682100d61afe","55a4ff5b2e70c0250038050f","55acb28318eefd0d0071d504","55ae1abe8576b92300291c80","55ae453ef302af23000ac109","55af586d555b900d0036d296","55af91dac8a85321007a53c3","55b9fee204775a2f00628071","55b9ff0e04775a2f00628072","55b9ff4604775a2f00628073","55b9ff5fd72d1e1900276a38","55b9ff7f04775a2f00628074","55b9ff90eb08801900f833e5","55b9ffa5d72d1e1900276a39","55b9ffca04775a2f00628075","55b9fffdd72d1e1900276a3a","56bc2f033ee9e70d008b46af","56c2f6efbbf9ec2d00e0fe4f","57595bbb18760817001e8bbe","57d8d9793916800e003dde53"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-07-15T18:11:10.196Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"This guide will help you to better understand how to pass custom data to your apps in the push notification payload so your app could react to such pushes and perform various actions accordingly.\n\nThe ways you handle custom data can vary according to your business objectives. In this article we’ll show some basic examples of parsing custom data and performing simple actions:\n\n  * changing the background color of the app;\n  * opening a custom page in your app. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Prerequisites\"\n}\n[/block]\nThis guide covers iOS Native development. You can find the source code example for this guide in our [GitHub repo](https://github.com/Pushwoosh/pushwoosh-ios-sdk/tree/master/Samples/iPhone). It is assumed that you have iOS sample application configured and receiving push notifications as per [Native iOS Integration Guide](http://docs.pushwoosh.com/docs/native-ios-sdk).\n\nFor our guide we will use the app with single View Controller.\n\nIn AppDelegate in `didFinishLaunchingWithOptions` function we will use `self.viewController` as a delegate for push notifications processing:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\\n     \\n    //some code\\n \\n    //-----------PUSHWOOSH PART-----------\\n    // set custom delegate for push handling, in our case - view controller\\n    [PushNotificationManager pushManager].delegate = self.viewController;\\n     \\n    // handling push on app start\\n    [[PushNotificationManager pushManager] handlePushReceived:launchOptions];\\n     \\n    // make sure we count app open in Pushwoosh stats\\n    [[PushNotificationManager pushManager] sendAppOpen];\\n     \\n    // register for push notifications!\\n    [[PushNotificationManager pushManager] registerForPushNotifications];\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nOur **ViewController.m** implements the `PushNotificationDelegate` protocol:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \":::at:::interface ViewController : UIViewController <UITextFieldDelegate, PushNotificationDelegate>\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nAnd therefore has `onPushAccepted` function that handles received push notifications:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//user pressed OK on the push notification\\n- (void) onPushAccepted:(PushNotificationManager *)pushManager withNotification:(NSDictionary *)pushNotification {\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Retrieving data from push payload\"\n}\n[/block]\nIn order to process custom data from the push notification payload we would obviously need to extract it from the push payload first. To achieve that you have to call the following function:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"NSString *customDataString = [pushManager getCustomPushData:pushNotification];\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nIt is returned as string, but we would like to deal with JSON object. So, we are going to use NSJSONSerialization class to convert it to JSON payload:\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Please note that push notifications payload could be empty, so don’t forget to check it for NULL.\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"NSDictionary *jsonData = nil;\\nif (customDataString) {\\n    jsonData = [NSJSONSerialization JSONObjectWithData:[customDataString dataUsingEncoding:NSUTF8StringEncoding]\\n                                               options:NSJSONReadingMutableContainers error:nil];\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Changing the view background color\"\n}\n[/block]\nNow that we have our custom data extracted from the push payload, let’s do something with it. For instance, let’s change the view background color. We assume that custom data would contain “r”, “g”, “b” items in JSON object format as follows:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"NSString *redStr = [jsonData objectForKey:@\\\"r\\\"];\\nNSString *greenStr = [jsonData objectForKey:@\\\"g\\\"];\\nNSString *blueStr = [jsonData objectForKey:@\\\"b\\\"];\\n \\nif (redStr || greenStr || blueStr) {\\n    [self setViewBackgroundColorWithRed:redStr green:greenStr blue:blueStr];\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nWe will use the following function to set background color:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)setViewBackgroundColorWithRed:(NSString *)redString green:(NSString *)greenString blue:(NSString *)blueString {\\n    CGFloat red = [redString floatValue] / 255.0f;\\n    CGFloat green = [greenString floatValue] / 255.0f;\\n    CGFloat blue = [blueString floatValue] / 255.0f;\\n     \\n    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];\\n    [UIView animateWithDuration:0.3 animations:^{\\n        self.view.backgroundColor = color;\\n        self.presentedViewController.view.backgroundColor = color;\\n    }];\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nLet’s test our example. Go to the app and enter any push notification text:\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/fe0ed6a-custom_data.png\",\n        \"custom_data.png\",\n        857,\n        382,\n        \"#ebebea\"\n      ]\n    }\n  ]\n}\n[/block]\nThen switch to the *Action* tab. As we've decided that our custom data format would be JSON object with “r”, “g”, “b” values in it, we need to use “custom data” field in the Control Panel and populate it with JSON object `{\"r\":30, \"g\":144, \"b\":255}`:\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/cf202e0-custom_data_1.png\",\n        \"custom_data_1.png\",\n        855,\n        363,\n        \"#0baae8\"\n      ]\n    }\n  ]\n}\n[/block]\nThis should set the background color to dodger blue. Now scroll down and press “Woosh”!\n\nPush notification received!\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/YOacsRMTlOLQ8I9cnxNz_IMG_0107.png\",\n        \"IMG_0107.png\",\n        \"375\",\n        \"667\",\n        \"#466e82\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nAnd the screen turns dodger blue:\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/7UY8sX4BT9SjGetjQz0b_IMG_0108.png\",\n        \"IMG_0108.png\",\n        \"375\",\n        \"663\",\n        \"#1c93fb\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Opening a custom page\"\n}\n[/block]\nLet’s try something different like opening custom page of the app! Assume that we have special discount page in our app where user can redeem the voucher received in push notification.\n\nAs most of the view controllers require additional initialization and cannot be presented out of the box from push notification, some additional coding is required here.\n\nBut first let’s start with a simple new view controller. We’ll call it `CustomPageViewController`. Please refer to the `CustomPageViewController.xib` in the sample project for the controls layout. Our new controller will have label, a close button and two properties to initialize `bgColor` and `discount`.\n\nHeader file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"@interface CustomPageViewController : UIViewController\\n \\n@property (nonatomic, strong) UIColor *bgColor;\\n@property (nonatomic, assign) NSInteger discount;\\n \\n@property (nonatomic, weak) IBOutlet UILabel *titleLabel;\\n \\n@end\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nSource file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#import \\\"CustomPageViewController.h\\\"\\n \\n@implementation CustomPageViewController\\n \\n- (void)viewDidLoad {\\n    [super viewDidLoad];\\n    self.view.backgroundColor = self.bgColor;\\n    self.titleLabel.text = [NSString stringWithFormat:@\\\"Only today get %ld%% discount!\\\", (long)self.discount];\\n}\\n \\n- (IBAction)closeAction:(id)sender {\\n    [self dismissViewControllerAnimated:YES completion:nil];\\n}\\n \\n@end\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nLet’s get back to our push notifications handling function.\n\nWe assume that the discount value will come as `\"d\"` parameter in JSON of the push notifications payload. As push notification payload is limited in size you’d rather use short names for the parameters.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"NSString *discount = [jsonData objectForKey:@\\\"d\\\"];\\n \\nif (discount) {\\n    [self showPromoPage:discount];\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nAnd `showPromoPage` function:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)showPromoPage:(NSString *)discount {\\n    CustomPageViewController *vc = [[CustomPageViewController alloc] init];\\n    vc.bgColor = self.view.backgroundColor;\\n    vc.discount = [discount integerValue];\\n    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;\\n     \\n    if (self.presentedViewController) {\\n        [self dismissViewControllerAnimated:YES completion:^{\\n            [self presentViewController:vc animated:YES completion:nil];\\n        }];\\n    } else {\\n        [self presentViewController:vc animated:YES completion:nil];\\n    }\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\nNow go back to the Control Panel and add \"d\" key and its value to the custom data, and press Woosh!\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/8a80f43-Fig_5.jpg\",\n        \"Fig 5.jpg\",\n        765,\n        328,\n        \"#16a7dd\"\n      ]\n    }\n  ]\n}\n[/block]\nOpen the app from this push, and it’ll display the page with a discount:\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/NFnULXAcRfyNaj2xvvEo_IMG_0111.png\",\n        \"IMG_0111.png\",\n        \"375\",\n        \"667\",\n        \"#1b93fb\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou can write code to initialize and open different View Controllers depending on the parameters you pass in push notifications payload.","excerpt":"","slug":"using-custom-data","type":"basic","title":"Using Custom Data"}
This guide will help you to better understand how to pass custom data to your apps in the push notification payload so your app could react to such pushes and perform various actions accordingly. The ways you handle custom data can vary according to your business objectives. In this article we’ll show some basic examples of parsing custom data and performing simple actions: * changing the background color of the app; * opening a custom page in your app. [block:api-header] { "type": "basic", "title": "Prerequisites" } [/block] This guide covers iOS Native development. You can find the source code example for this guide in our [GitHub repo](https://github.com/Pushwoosh/pushwoosh-ios-sdk/tree/master/Samples/iPhone). It is assumed that you have iOS sample application configured and receiving push notifications as per [Native iOS Integration Guide](http://docs.pushwoosh.com/docs/native-ios-sdk). For our guide we will use the app with single View Controller. In AppDelegate in `didFinishLaunchingWithOptions` function we will use `self.viewController` as a delegate for push notifications processing: [block:code] { "codes": [ { "code": "- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\n \n //some code\n \n //-----------PUSHWOOSH PART-----------\n // set custom delegate for push handling, in our case - view controller\n [PushNotificationManager pushManager].delegate = self.viewController;\n \n // handling push on app start\n [[PushNotificationManager pushManager] handlePushReceived:launchOptions];\n \n // make sure we count app open in Pushwoosh stats\n [[PushNotificationManager pushManager] sendAppOpen];\n \n // register for push notifications!\n [[PushNotificationManager pushManager] registerForPushNotifications];\n}", "language": "objectivec" } ] } [/block] Our **ViewController.m** implements the `PushNotificationDelegate` protocol: [block:code] { "codes": [ { "code": "@interface ViewController : UIViewController <UITextFieldDelegate, PushNotificationDelegate>", "language": "objectivec" } ] } [/block] And therefore has `onPushAccepted` function that handles received push notifications: [block:code] { "codes": [ { "code": "//user pressed OK on the push notification\n- (void) onPushAccepted:(PushNotificationManager *)pushManager withNotification:(NSDictionary *)pushNotification {", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Retrieving data from push payload" } [/block] In order to process custom data from the push notification payload we would obviously need to extract it from the push payload first. To achieve that you have to call the following function: [block:code] { "codes": [ { "code": "NSString *customDataString = [pushManager getCustomPushData:pushNotification];", "language": "objectivec" } ] } [/block] It is returned as string, but we would like to deal with JSON object. So, we are going to use NSJSONSerialization class to convert it to JSON payload: [block:callout] { "type": "info", "body": "Please note that push notifications payload could be empty, so don’t forget to check it for NULL." } [/block] [block:code] { "codes": [ { "code": "NSDictionary *jsonData = nil;\nif (customDataString) {\n jsonData = [NSJSONSerialization JSONObjectWithData:[customDataString dataUsingEncoding:NSUTF8StringEncoding]\n options:NSJSONReadingMutableContainers error:nil];\n}", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Changing the view background color" } [/block] Now that we have our custom data extracted from the push payload, let’s do something with it. For instance, let’s change the view background color. We assume that custom data would contain “r”, “g”, “b” items in JSON object format as follows: [block:code] { "codes": [ { "code": "NSString *redStr = [jsonData objectForKey:@\"r\"];\nNSString *greenStr = [jsonData objectForKey:@\"g\"];\nNSString *blueStr = [jsonData objectForKey:@\"b\"];\n \nif (redStr || greenStr || blueStr) {\n [self setViewBackgroundColorWithRed:redStr green:greenStr blue:blueStr];\n}", "language": "objectivec" } ] } [/block] We will use the following function to set background color: [block:code] { "codes": [ { "code": "- (void)setViewBackgroundColorWithRed:(NSString *)redString green:(NSString *)greenString blue:(NSString *)blueString {\n CGFloat red = [redString floatValue] / 255.0f;\n CGFloat green = [greenString floatValue] / 255.0f;\n CGFloat blue = [blueString floatValue] / 255.0f;\n \n UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];\n [UIView animateWithDuration:0.3 animations:^{\n self.view.backgroundColor = color;\n self.presentedViewController.view.backgroundColor = color;\n }];\n}", "language": "objectivec" } ] } [/block] Let’s test our example. Go to the app and enter any push notification text: [block:image] { "images": [ { "image": [ "https://files.readme.io/fe0ed6a-custom_data.png", "custom_data.png", 857, 382, "#ebebea" ] } ] } [/block] Then switch to the *Action* tab. As we've decided that our custom data format would be JSON object with “r”, “g”, “b” values in it, we need to use “custom data” field in the Control Panel and populate it with JSON object `{"r":30, "g":144, "b":255}`: [block:image] { "images": [ { "image": [ "https://files.readme.io/cf202e0-custom_data_1.png", "custom_data_1.png", 855, 363, "#0baae8" ] } ] } [/block] This should set the background color to dodger blue. Now scroll down and press “Woosh”! Push notification received! [block:image] { "images": [ { "image": [ "https://files.readme.io/YOacsRMTlOLQ8I9cnxNz_IMG_0107.png", "IMG_0107.png", "375", "667", "#466e82", "" ] } ] } [/block] And the screen turns dodger blue: [block:image] { "images": [ { "image": [ "https://files.readme.io/7UY8sX4BT9SjGetjQz0b_IMG_0108.png", "IMG_0108.png", "375", "663", "#1c93fb", "" ] } ] } [/block] [block:api-header] { "type": "basic", "title": "Opening a custom page" } [/block] Let’s try something different like opening custom page of the app! Assume that we have special discount page in our app where user can redeem the voucher received in push notification. As most of the view controllers require additional initialization and cannot be presented out of the box from push notification, some additional coding is required here. But first let’s start with a simple new view controller. We’ll call it `CustomPageViewController`. Please refer to the `CustomPageViewController.xib` in the sample project for the controls layout. Our new controller will have label, a close button and two properties to initialize `bgColor` and `discount`. Header file: [block:code] { "codes": [ { "code": "@interface CustomPageViewController : UIViewController\n \n@property (nonatomic, strong) UIColor *bgColor;\n@property (nonatomic, assign) NSInteger discount;\n \n@property (nonatomic, weak) IBOutlet UILabel *titleLabel;\n \n@end", "language": "objectivec" } ] } [/block] Source file: [block:code] { "codes": [ { "code": "#import \"CustomPageViewController.h\"\n \n@implementation CustomPageViewController\n \n- (void)viewDidLoad {\n [super viewDidLoad];\n self.view.backgroundColor = self.bgColor;\n self.titleLabel.text = [NSString stringWithFormat:@\"Only today get %ld%% discount!\", (long)self.discount];\n}\n \n- (IBAction)closeAction:(id)sender {\n [self dismissViewControllerAnimated:YES completion:nil];\n}\n \n@end", "language": "objectivec" } ] } [/block] Let’s get back to our push notifications handling function. We assume that the discount value will come as `"d"` parameter in JSON of the push notifications payload. As push notification payload is limited in size you’d rather use short names for the parameters. [block:code] { "codes": [ { "code": "NSString *discount = [jsonData objectForKey:@\"d\"];\n \nif (discount) {\n [self showPromoPage:discount];\n}", "language": "objectivec" } ] } [/block] And `showPromoPage` function: [block:code] { "codes": [ { "code": "- (void)showPromoPage:(NSString *)discount {\n CustomPageViewController *vc = [[CustomPageViewController alloc] init];\n vc.bgColor = self.view.backgroundColor;\n vc.discount = [discount integerValue];\n vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;\n \n if (self.presentedViewController) {\n [self dismissViewControllerAnimated:YES completion:^{\n [self presentViewController:vc animated:YES completion:nil];\n }];\n } else {\n [self presentViewController:vc animated:YES completion:nil];\n }\n}", "language": "objectivec" } ] } [/block] Now go back to the Control Panel and add "d" key and its value to the custom data, and press Woosh! [block:image] { "images": [ { "image": [ "https://files.readme.io/8a80f43-Fig_5.jpg", "Fig 5.jpg", 765, 328, "#16a7dd" ] } ] } [/block] Open the app from this push, and it’ll display the page with a discount: [block:image] { "images": [ { "image": [ "https://files.readme.io/NFnULXAcRfyNaj2xvvEo_IMG_0111.png", "IMG_0111.png", "375", "667", "#1b93fb", "" ] } ] } [/block] You can write code to initialize and open different View Controllers depending on the parameters you pass in push notifications payload.