Interactive push notifications in iPhone app

Apple in its new update iOS 8 launched  a new API which will make Push notifications interact with the app from the lock screen. With the help of this API, we can create a new functionality outside of your application. This blog will help you in making your notifications interact with app from lock screen.

We need three new classes which have been added in iOS 8 to do this. They are:

1. UIUserNotificationSettings

2. UIUserNotificationCategory

3. UIUserNotificationAction

Earlier, we used to register notifications for custom sounds, badges, deeplinking payloads. Now with this, we can register notifications for custom actions and eventsfrom the app.

Here is the code snippet you have to add in AppDelegate.m file, using which you can make interactive push notifications :

1. (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

…..

NSString *const NotificationAction1= “Your_custom_action_1”;

NSString *const NotificationAction2= “Your_custom_action_2”;

[self registerForNotifications];

…..

}

2. Now create a function in AppDelegate.m which will register for push notifications:

-(void)registerForNotifications

{

UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];

[action1 setActivationMode: UIUserNotificationActivationModeBackground];

[action1 setTitle:@”Your_custom_action_1″];

[action1 setIdentifier:NotificationAction1];

[action1 setAuthenticationRequired:NO];

[action1 setDestructive: NO];

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];

[action2 setActivationMode: UIUserNotificationActivationModeBackground];

[action2 setTitle:@”Your_custom_action_2″];

[action2 setIdentifier:NotificationAction2];

[action2 setAuthenticationRequired:NO];

[action2 setDestructive: NO];

UIMutableUserNotificationCategory *newCategories = [[UIMutableUserNotificationCategory alloc] init];

[newCategories setActions:@[action1,action2] forContext:UIUserNotiticationActionContextDefault];

NSSet *allCategories = [[NSSet setWithObject:newCategories];

UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:allCategories];

[UIApplication sharedApplication] regitserUserNotificationSettings: settings];

}

3. Now to send interactive push notification to device, follow this link.

4. Now to handle user actions we need a new method in UIApplicationDelegate protocol. Here is the code snippet to handle the user actions on push notifications:

-(void)application:(UIApplication *) application handleActionWithIdentifier:(NSString *) identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {

if( identifier isEqualToString:NotificationAction1) {

NSLog(“Your action 1”);

}

else if(identifier isEqualToString:NotificationAction2) {

NSLog(“Your action2”);

}

if(completionHandler) {

completionHandler();

}

}

 

Here is how you can configure your app to receive interactive push notifications in iPhone app. Feel free to comment to this post or you can also drop an email to naik899@gmail.com

Leave a Reply

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