18

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 :)

Augustine P A
  • 5,008
  • 3
  • 35
  • 39

2 Answers2

34
  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];
rintaro
  • 51,423
  • 14
  • 131
  • 139
Sourav Gupta
  • 762
  • 11
  • 13
  • Thank you very much @Sourav Gupta. But what about remote notification? – Augustine P A Sep 19 '14 at 10:59
  • 2
    Same code will work for remote notification also. You just need to add the "category" key in push payload. "category" key should have the same identifier value which you are defining in your code. – Sourav Gupta Sep 19 '14 at 11:03
  • Sorry Sourav I don't understand the concept. Could you please little elaborate your answer? When I am sending remote notification, the payload should contain all the details? – Augustine P A Sep 19 '14 at 11:11
  • You can have n number of different categories in you code having different actions associated with it.For ex. in above code we have "notificationCategory" having actions "notificationAction1","notificationAction2","notificationAction3". Suppose you have 2 different categories in your code. then how will you differentiate one from other. you just need to add the category key like this. Depending on which category identifier you are sending it will show the corresponding actions. { "alert": "Hello", "category": "Email" } – Sourav Gupta Sep 19 '14 at 11:30
  • @Augustine:: please refer to this repositry for more info ..https://github.com/sgup77/SGNotification – Sourav Gupta Sep 22 '14 at 05:47
  • Thanks Sourav.. Do u have idea about iOS8 remote notification payload? – Augustine P A Sep 25 '14 at 14:15
  • 2
    it should be similar to earlier. only change would be to sent category key like "category":"Email" in the existing payload depending on which category you want – Sourav Gupta Sep 25 '14 at 14:22
  • One thing that I think is worth noting is that it seems that you can't register the notification settings before appDidFinishLaunching finishes. In my trials when I attempted to put the above code in appDidFinishLaunching, the registration never seemed to work. Notifications would arrive, but no custom actions were available. – Roderic Campbell Dec 03 '14 at 18:26
  • Is it possible to have two categories? Can we still add two categories to UserSetting and have different buttons for both? I am having issue with setting two categories. – Meet Dec 23 '14 at 13:28
  • I;m unable to get3 actions on recieving notification. Please help – Vaibhav Jhaveri Jul 06 '15 at 10:03
  • Is there any way to add more than 2 buttons? or any way to add images? Please guide. – Aanchal Chaurasia Jan 19 '17 at 08:53
11

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

Community
  • 1
  • 1
Augustine P A
  • 5,008
  • 3
  • 35
  • 39