Configure & Send Push Notification on iOS Device

To increase engagement Push Notification is widely used in mobile native apps. Though seems extremely useful feature it is a bit tricky to implement it. Configuring your own server, creating p12 files are some of the steps which makes things little difficult for developers.push Configure & Send Push Notification on iOS Device

We have also been receiving a few queries in the last few days on configuring push notification service on an iOS device using App42. So, we decided to put down the steps here for iOS developers for their benefit and ready reference.

Steps involved in configuring the push notification service:

1. First you have to create a certificate from iOS Dev Center, Here is a step by step tutorial to do that: http://app42.sh/12dpgQW
2. Double click the .cer file, which was downloaded in the above step, to install it.
3. Convert the iPhone developer certificate into a .p12 file by following the below mentioned steps:

  • Open the Keychain Access application (in the Applications/Utilities folder)
  • Select the Keys category in Keychain Access
  • Select the private key associated with your iPhone Development Certificate.The private key is identified by the iPhone Developer as: public certificate that is paired with it.
  • Select File > Export Items
  • Save your key in the Personal Information Exchange (.p12) file format
  • You will be prompted to create a password which will be used when you will attempt to import this key on another computer.

4. Make your .p12 file compatible with APP42 server by following the below mentioned steps:

  • Keep your .cer file and .p12 file in a single folder
  • Open terminal and go to the folder that has both the files
shephertz-technologiess-iMac:~ shephertztechnologies$ cd "your folder path"
  • Now execute the following commands in the terminal
1
2
3
openssl x509 -in developer_identity.cer -inform DER -out developer_identity.pem -outform PEM
openssl pkcs12 -nocerts -in yourPrivateKey.p12 -out yourPrivateKey.pem
openssl pkcs12 -export -inkey yourPrivateKey.pem -in developer_identity.pem -out iphone_dev.p12

Where,
- developer_identity.cer  <= certificate you downloaded from Apple
- yourPrivateKey.p12      <= your private key

5. Upload iphone_dev.p12 file to the AppHQ consoleapphq console push Configure & Send Push Notification on iOS Device

  • Login to AppHQ console and select Push Notification in AppHQ Menu
  • Select iOS settings in Settings
  • Select your App Name from the drop down menu
  • Browse your iphone_developer.p12 file by clicking ‘choose file’ option to upload the file
  • Enter password given during creation of the iphone_developer.p12 file
  • Select the environment from the drop down menu
  • Click on the submit button and it’s done here

6. Open your Xcode project and navigate to the AppDelegate.m class and change the application:didFinishLaunchingWithOptions: method to look like this:

1
2
3
4
5
6
7
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
   (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    return YES;
}

The above code tells the OS that this App wants to receive push notifications.

7. Now you need to add the following delegate methods in your AppDelegate.m in order to receive push notification:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
   NSLog(@"My token is: %@", deviceToken);
 
  // Prepare the Device Token for Registration (remove spaces and  &lt; &gt;)
  NSString *devToken = [[[[deviceToken description]
  stringByReplacingOccurrencesOfString:@"&lt;"withString:@""] stringBy  ReplacingOccurrencesOfString:@"&gt;" withString:@""]
  stringByReplacingOccurrencesOfString: @" " withString: @""];
 
NSLog(@"My Device token is: %@", devToken);
}
 
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}

When your App registers for remote (push) notifications, it tries to obtain a “device token”. This is a 32-byte number that uniquely identifies your device. A device token is the address to which a push notification will be delivered.

8. After getting the device token you need to register your device with the App42 Server to set up a connection to APNS to send the push notification to this device token. Change the delegate method application:didRegisterForRemoteNotificationsWithDeviceToken:  in the  AppDelegate.m class to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
 
// Prepare the Device Token for Registration (remove spaces and &lt; &gt;)
NSString *devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"&lt;"withString:@""] stringByReplacingOccurrencesOfString:@"&gt;" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
 
NSLog(@"My Device token is: %@", devToken);
/**
*  Register the device token for App42 Push notification services
*/
[self registerUserForPushNotificationToApp42Cloud:devToken];
}
-(void)registerUserForPushNotificationToApp42Cloud:(NSString*)deviceToken
{
ServiceAPI *serviceObj = [[ServiceAPI alloc]init];
serviceObj.apiKey = APP42_APP_KEY;
serviceObj.secretKey = APP42_SECRET_KEY;
PushNotificationService *pushObj = [serviceObj buildPushService];
 
@try
{
PushNotification *pushNotification =[pushObj registerDeviceToken:deviceToken withUser:@"User Name"];
[pushNotification release];
}
@catch (App42Exception *exception)
{
NSLog(@"%@",exception.reason);
}
@finally
{
[serviceObj release];
[pushObj release];
}
}

9. Now to send a push notification, call the following method in a commonly used class in your project so that you can call this whenever you want to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-(void)sendPush:(NSString*)message
{
ServiceAPI *serviceObj = [[ServiceAPI alloc]init];
serviceObj.apiKey = APP42_APP_KEY;
serviceObj.secretKey = APP42_SECRET_KEY;
PushNotificationService *pushObj = [serviceObj buildPushService];
 
@try
{
NSMutableDictionary *pushDictionary = [NSMutableDictionary dictionary];
[pushDictionary setObject:message forKey:@"alert"];
[pushDictionary setObject:@"default" forKey:@"sound"];
[pushDictionary setObject:@"1" forKey:@"badge"];
 
PushNotification *pushNotification = [pushObj sendPushMessageToUser:@"User Name" withMessageDictionary: pushDictionary];
[pushNotification release];
 
}
@catch (App42Exception *exception)
{
NSLog(@"%@",exception.reason);
}
@finally
{
[serviceObj release];
[pushObj release];
}
}

The pushDictionary in the above code should always follow the same structure as mentioned above to deliver the push notification successfully using App42 Server. You can remove or add the items to the pushDictionary if needed as per the Apple guidelines.

10. If you want to take any actions when you receive push notification then you need to add the following delegate method in the AppDelegate.m class:

1
2
3
4
5
6
7
8
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo);
 
/**
* Dump your code here according to your requirement after receiving push
*/
}

With the above mentioned step, your App has been successfully set up to receive/send push notifications through our App42 Server using App42 Push Notification Service.

It would be great to hear the views from the readers of this blog. In case you have any more questions, please feel free to reach out to us at support@shephertz.com

twitter Configure & Send Push Notification on iOS Devicefacebook Configure & Send Push Notification on iOS Devicegoogle Configure & Send Push Notification on iOS Devicelinkedin Configure & Send Push Notification on iOS Devicepinterest Configure & Send Push Notification on iOS Devicereddit Configure & Send Push Notification on iOS Devicestumbleupon Configure & Send Push Notification on iOS Devicetumblr Configure & Send Push Notification on iOS Device

Leave a Reply

Your email address will not be published. Required fields are marked *


one × 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>