Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement iOS8 Interactive Notification

I am developing an iOS8 application which supports interactive notification. But I don't have clarity on the features supported by interactive notification and how to send/handle interactive notifications. If any one can give an example, that would be very much helpful for me.

Thanks in Advance :)

like image 633
Augustine P A Avatar asked Sep 19 '14 08:09

Augustine P A


People also ask

What is interactive notification?

Interactive Notifications are push notifications that include buttons for secondary, complementary actions, allowing users to interact with notifications from outside of the app. They can drive the user to: Take immediate, specific action. Make decisions or choices.

What do you need to do to integrate push notifications in an iOS app?

Setting up Push Notifications in iOS. Go back to the iOS project you created earlier and select the main target. Under the Signing & Capabilities tab, click on the + Capability button, then select Push Notifications. This will enable your app to receive push notifications from OneSignal.


2 Answers

  1. First you need to create the notification Action.
  2. Second you need to create the notification category and set its actions.You can set for two contexts. UIUserNotificationActionContextDefault or UIUserNotificationActionContextMinimal
  3. Third you need to create the notification setting and assign the above categories
  4. Fourth step would be to create local notification and assign it the identifier of the category.

UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = @"Accept";
notificationAction1.title = @"Accept";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;

UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = @"Reject";
notificationAction2.title = @"Reject";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = YES;
notificationAction2.authenticationRequired = YES;

UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
notificationAction3.identifier = @"Reply";
notificationAction3.title = @"Reply";
notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
notificationAction3.destructive = NO;
notificationAction3.authenticationRequired = YES;

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Email";
[notificationCategory setActions:@[notificationAction1,notificationAction2,notificationAction3] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];

NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];

UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"Testing";
localNotification.category = @"Email"; //  Same as category identifier
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
like image 192
Sourav Gupta Avatar answered Oct 21 '22 16:10

Sourav Gupta


I would like to extend Sourav Gupta's answer a bit. Once you have done up to what Sourav explained, you have to implement the delegates for receiving push notification actions. The delegates are,

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {

// Handle actions of local notifications here. You can identify the action by using "identifier" and perform appropriate operations

     if(completionHandler != nil)    //Finally call completion handler if its not nil
         completionHandler();
}

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

// Handle actions of remote notifications here. You can identify the action by using "identifier" and perform appropriate operations

     if(completionHandler != nil)    //Finally call completion handler if its not nil
         completionHandler();
}

You can refer remote notification payload sample here iOS8 Push notification Payload

like image 23
Augustine P A Avatar answered Oct 21 '22 15:10

Augustine P A