Follow

iOS Native Push Notification Code Example

This section describes the steps involved in writing an Unity iOS Application that uses Push Notifications. If you are using Unity iOS please follow this guide. Unity IOS Push Notification Code Example.

You will have needed to complete the steps in the article Creating Your IOS Push Credentials in order to use the code in this example.

 

The following bits of code will be added into your AppDelegate.m file. If you do not have any of the callback functions in your AppDelegate.m file you will need to add them

Registering for A Push Notification:

You will need to input code in two spots. First you will need to add the didRegisterForRemoteNotificationsWithDeviceToken function as shown below.

 - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)devToken
{
     [Apmetrix registeredForPushNotifications:devToken];
     NSLog(@"did register for remote notifications");

}

Also in the didFinishLaunchingWithOptions function you will need to add the following if block. This if statement handles the push registration for iOS 8+ OS and any OS lower than iOS 8. 

// Let the device know we want to receive push notifications
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

 

Configuring A Push Notification:

After you init the Apmetrix SDK by calling [Apmetrix initWithDataset] you will then be able to call the function

[Apmetrix notificationAlertConfig:@"Activate" andCloseName:@"Terminate"];

This function allows you to configure the default button names for the in-app push notification. *Note: The in-app push notification is not the same as in-app messaging. The in-app push notification requires push notifications to be setup in-order to display the dialog box

 

Receiving A Push Notification:

 // received remote notification while application active in either foreground or background
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
     NSLog(@"Message From Notification: %@", [Apmetrix   notificationHandler:userInfo]);

}

Detecting Launch From A Push Notification:

This bit of code detects if the app launched from a Push Notification

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     // check if launching application from push notification
     NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

     if (remoteNotification != nil) {
          [Apmetrix notificationHandler:remoteNotification];
     }

}

Setting Up the Xcode Project:

You will now want to click on Your App Name under Targets. Then click on Build Settings and scroll down to Code Signing. You should see something similar to the picture below

 

Remember in the document Creating Your IOS Push Credentials when you added your provising profile to Xcode. We will be using that profile with Push Enabled here.

Set your Code Signing Identity to your Apple Account and then set the Provising Profile to the one with Push Enabled. Your screen should look like similar to the image below.

Now go ahead and press the Play Button in the top Left of Xcode to finish building the app to your iOS device. You should get the pop-up prompting you for Push Notifications. If you do not see this prompt then please review the steps in this article and the Creating Your IOS Push Credentials

 

Sending A Push Notification:

Next, log into our dashboard (www.apmetrix.com/login)  and click on your Profile Image->Messaging and Send Push Notification and send yourself some tests to verify everything is working properly.

Note: Apple has 2 different servers... Development and Live... use Development while you're testing... and use Live for published products.

If you are still having issues please contact Apmetrix Support at support@apmetrix.com

Have more questions? Submit a request