b2cloud

6th October 2014

Interactive Notifications in iOS 8

Guides | Tutorial By 2 years ago

In iOS 8 developers can now present interactive notifications in their apps.

To do so, first you need to register all possible notification “categories”. A category is defined by a set of actions. In my example I’m going to setup a category with reply and delete actions.

Screen Shot 2014-10-06 at 4.14.40 pm

Each action is shown to the user as a button. Actions have two possible activation modes: foreground or background. If your app can perform the action with no further interaction from the user then you want to use the background activation mode. If the action needs further user input then you can set the activation mode to foreground to have your app launched when actioned, for example, presenting a keyboard in your app when they reply button is tapped.

UIMutableUserNotificationAction* deleteAction = [[UIMutableUserNotificationAction alloc] init];
[deleteAction setIdentifier:@"delete_action_id"];
[deleteAction setTitle:@"Delete"];
[deleteAction setActivationMode:UIUserNotificationActivationModeBackground];
[deleteAction setDestructive:YES];

UIMutableUserNotificationAction* replyAction = [[UIMutableUserNotificationAction alloc] init];
[replyAction setIdentifier:@"reply_action_id"];
[replyAction setTitle:@"Reply"];
[replyAction setActivationMode:UIUserNotificationActivationModeForeground];
[replyAction setDestructive:NO];

UIMutableUserNotificationCategory* deleteReplyCategory = [[UIMutableUserNotificationCategory alloc] init];
[deleteReplyCategory setIdentifier:@"custom_category_id"];
[deleteReplyCategory setActions:@[replyAction, deleteAction] forContext:UIUserNotificationActionContextDefault];

Note the string identifiers used above. These are used later on in order to setup and respond to a selected action. In a real app it would be recommended to not write the identifiers more than once, instead define them in a single place.

Now with the delete reply category setup, we need to get the user’s permission to allow these types of notifications. This will show the “App would like to send you notifications” alert box.

NSSet* categories = [NSSet setWithArray:@[deleteReplyCategory]];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

Now we can schedule a notification to use the previously setup category. This is the string identifier from before. This notification is scheduled after 10 seconds.

UILocalNotification* notification = [[UILocalNotification alloc] init];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
[notification setAlertBody:@"Somebody sent you a message"];
[notification setCategory:@"custom_category_id"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

When the user selects an option we get a callback to the app delegate.

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
	if([notification.category isEqualToString:@"custom_category_id"])
	{
		if([identifier isEqualToString:@"delete_action_id"])
		{
			NSLog(@"Delete was pressed");
		}
		else if([identifier isEqualToString:@"reply_action_id"])
		{
			NSLog(@"Reply was pressed");
		}
	}
	
	//	Important to call this when finished
	completionHandler();
}

That’s about it.

This also works with remote notifications. Your notification payload can specify the category identifier, and you can get a callback with another delegate method:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
	//	Remote notification callback
	completionHandler();
}
Recommended Posts

Notifications in iOS 8

Post by 2 years ago

In iOS 8 the notification behavour has changed. Previously an app could send a user a local notification without their initial permission, but would need to get their permission if the user was to get

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!