Update the app to enable push messaging - Adobe-Marketing-Cloud/aml-summit-lab GitHub Wiki
Overview
In this section, we'll update the app to first enable and then handle push notifications. Please note that push messaging does not work with simulators. So even if you make the following code changes in your app and run it on the simulator, you would not be able to see your push messages coming through. We'll still talk about what the code snippets are for though.
For the purposes of this demo, the app installed on the demo device has the following code implemented already and thus supports push messages. In order to show the whole workflow end to end, you'll use the provided cert and device token to send push messages you configure directly to the device connected to the screen using the "TEST" feature on Adobe Mobile Services.
Implementation Steps
- Navigate to AppDelegate.m located within the SummitMobileLab2016 folder in Xcode.

- Register for remote notifications.
- Go to line #39 and delete "/*"
- Go to line #47 and delete "*/"
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [push message] Register app for push notifications of sound/ badge/ alert type.
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
[application registerForRemoteNotifications];
/* // [in app message] Enable the collection of Lifecycle data.
// 1. turn debug logging on so we can see activity in the Xcode console
[ADBMobile setDebugLogging:YES];
// 2. collect lifecycle data with additional data reporting the year
[ADBMobile collectLifecycleDataWithAdditionalData:@{@"summit.year":@"2017"}];
*/
// initialize our fake data for the app
[self loadDestinations];
[self loadTransportationModes];
return YES;
}
- Collect the device’s push token.
- Go to line #90 and delete "//".
- It should look like as follows.
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/* Adobe Analytics
*
* 1. pass the push token for this device to the Adobe SDK
*/
[ADBMobile setPushIdentifier:deviceToken];
}
- (optional) Update device’s push token to keep estimated audience more accurate.
- Go to line #99 and delete "//".
- It should look as follows:
- (void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
/* Adobe Analytics
*
* (optional)
* 1. set the push token to nil for this device (this helps keep the estimated audience more accurate)
*/
[ADBMobile setPushIdentifier:nil];
}
- Everytime a push notification comes through, do two things:
- For all push messages: report the click through to the SDK.
- For handling advanced push message: The application can choose to handle the custom data that might come in with the push message any way it wants. In this example, we are extracting a color value from the custom payload, to apply it to user profile text.
- For OS version less than iOS 7,
- Go to line #110 and delete "//"
- Go to line #115 and delete "//"
- Go to line #116 and delete "//"
- It should look as follows:
// app target < iOS 7
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// only send the hit if the app is not active
if (application.applicationState != UIApplicationStateActive) {
/* Adobe Analytics
*
* 1. when the user opens your app by clicking through your push message, report it to the Adobe SDK
*/
[ADBMobile trackPushMessageClickThrough:userInfo];
/*
* Handle custom payload
*/
UIColor *customColor = [self extractColorFromData:userInfo];
[self customizeUsingPushData:customColor];
}
}
- For OS version greater than or equal to iOS 7,
- Go to line #129 and delete "//"
- Go to line #134 and delete "//"
- Go to line #135 and delete "//"
- It should look as follows:
// app target >= iOS 7
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// only send the hit if the app is not active
if (application.applicationState != UIApplicationStateActive) {
/* Adobe Analytics
*
* 1. when the user opens your app by clicking through your push message, report it to the Adobe SDK
*/
[ADBMobile trackPushMessageClickThrough:userInfo];
/*
* Handle custom payload
*/
UIColor *customColor = [self extractColorFromData:userInfo];
[self customizeUsingPushData:customColor];
}
completionHandler(UIBackgroundFetchResultNoData);
}
// applies the received color to view's text
- (void)customizeUsingPushData:(UIColor *)customColor{
}
// extracts color value from the custom payload
- (UIColor *)extractColorFromData:(NSDictionary *)data{
}
Next steps
NEXT: Create a push message.