MoEngage Docs

Actionable Notifications

Actionable notifications let you add custom action buttons to the standard iOS push notifications. Actionable notifications give the user a quick and easy way to perform relevant tasks in response to a notification. These actionable notifications are available from iOS 8 onwards.

NOTE:

Actionable Notifications are available from MoEngage SDK version 2.2. And to support using UserNotifications framework from iOS 10 onwards use MoEngage SDK 3.0 and above.

How to implement Actionable Notifications?

To use actionable notification with MoEngage SDK, you have to define the actions and group them into categories as shown in below example. In the example, you can see that we are getting two different Sets of categories, one for iOS10 and above(i.e, set of UNNotificationCategory instances) and the other for iOS version below iOS10 (i.e, set of MONotificationCategory instances). And while registering for push provide both the sets in parameters as shown below :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption
{
 ---------
    
    if (@available(iOS 10.0, *)) {
      NSSet* categorySetForiOS10 = [self getNotificationCategories];
      [[MoEngage sharedInstance] registerForRemoteNotificationWithCategories:categorySetForiOS10 withUserNotificationCenterDelegate:self];
    } else {
      NSSet* categorySetForEarlierOS = [self getNotificationCategoriesForEarlierVersion];
			[[MoEngage sharedInstance] registerForRemoteNotificationForBelowiOS10WithCategories:categorySetForEarlierOS];
    }
  
 ---------
    return YES;
}

//Example to define categories
//This method gives categories for iOS version 10.0 and above
-(NSSet*)getNotificationCategories{

    UNNotificationAction *acceptAction = [UNNotificationAction actionWithIdentifier:@"ACCEPT_IDENTIFIER" title:@"Accept" options:UNNotificationActionOptionAuthenticationRequired];
    UNNotificationAction *declineAction = [UNNotificationAction actionWithIdentifier:@"DECLINE_IDENTIFIER" title:@"Decline" options:(UNNotificationActionOptionDestructive)];
    UNNotificationAction *maybeAction = [UNNotificationAction actionWithIdentifier:@"MAYBE_IDENTIFIER" title:@"May Be" options:UNNotificationActionOptionNone];
    
    UNNotificationCategory* inviteCategory = [UNNotificationCategory categoryWithIdentifier:@"INVITE_CATEGORY"actions:@[acceptAction,maybeAction, declineAction,opt4Action] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
    
  	NSSet *categories = [NSSet setWithObjects:inviteCategory,nil];
    
    return categories;
}

//This method gives categories for iOS version below 10.0
-(NSSet*)getNotificationCategoriesForEarlierVersion{
    UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
    acceptAction.identifier = @"ACCEPT_IDENTIFIER";
    acceptAction.title = @"Accept";
    acceptAction.activationMode = UIUserNotificationActivationModeBackground;
    acceptAction.destructive = NO;
    acceptAction.authenticationRequired = NO;
    
    UIMutableUserNotificationAction *declineAction = [[UIMutableUserNotificationAction alloc] init];
    declineAction.identifier = @"DECLINE_IDENTIFIER";
    declineAction.title = @"Decline";
    declineAction.activationMode = UIUserNotificationActivationModeBackground;
    declineAction.destructive = YES;
    declineAction.authenticationRequired = NO;
    
    UIMutableUserNotificationAction *maybeAction = [[UIMutableUserNotificationAction alloc] init];
    maybeAction.identifier = @"MAYBE_IDENTIFIER";
    maybeAction.title = @"May Be";
    maybeAction.activationMode = UIUserNotificationActivationModeBackground;
    maybeAction.destructive = YES;
    maybeAction.authenticationRequired = NO;
    
    MONotificationCategory *inviteCategory = [[MONotificationCategory alloc] init];
    inviteCategory.identifier = @"INVITE_CATEGORY";
    
    
    [inviteCategory setActions:@[maybeAction, declineAction, acceptAction]
                    forContext:UIUserNotificationActionContextDefault];
    
    [inviteCategory setActions:@[declineAction,acceptAction]
                    forContext:UIUserNotificationActionContextMinimal];
    
    NSSet *categories = [NSSet setWithObjects:inviteCategory,nil];
    
    return categories;
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate{

    var window: UIWindow?
    

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        //--- Rest of Implementation
      
        //For registering for remote notification
        if #available(iOS 10.0, *) {
      		let categoriesForiOS10 = self.getCategoriesForiOS10()
					MoEngage.sharedInstance().registerForRemoteNotification(withCategories: categoriesForiOS10, withUserNotificationCenterDelegate: self)
        } else {
    			let categoriesForBelowiOS10 = self.getCategoriesForEarlierVersions()
          MoEngage.sharedInstance().registerForRemoteNotificationForBelowiOS10(withCategories: categoriesForBelowiOS10)
        }
        
        //--- Rest of Implementation
        
        return true
    }
    
    //Example to define categories
    //This method gives categories for iOS version 10.0 and above
    @available(iOS 10.0, *)
    func getCategoriesForiOS10() -> Set<UNNotificationCategory>{
        
        let acceptAction = UNNotificationAction.init(identifier: "ACCEPT_IDENTIFIER", title: "Accept", options: .authenticationRequired)
        let declineAction = UNNotificationAction.init(identifier: "DECLINE_IDENTIFIER", title: "Decline", options: .destructive)
        let maybeAction = UNNotificationAction.init(identifier: "MAYBE_IDENTIFIER", title: "May Be", options: .foreground)
        
        let inviteCategory = UNNotificationCategory.init(identifier: "INVITE_CATEGORY", actions: [acceptAction,declineAction,maybeAction], intentIdentifiers: [], options: .customDismissAction)
        let categoriesSet = Set.init([inviteCategory])
        
        return categoriesSet;
    }
    
    
    //This method gives categories for iOS version below 10.0
    func getCategoriesForEarlierVersions() -> Set<MONotificationCategory>{
        let acceptAction = UIMutableUserNotificationAction.init()
        acceptAction.identifier = "ACCEPT_IDENTIFIER"
        acceptAction.title = "Accept"
        acceptAction.activationMode = .background
        acceptAction.isDestructive = false
        acceptAction.isAuthenticationRequired = false
        
        let declineAction = UIMutableUserNotificationAction.init()
        declineAction.identifier = "DECLINE_IDENTIFIER"
        declineAction.title = "Decline"
        declineAction.activationMode = .background
        declineAction.isDestructive = true
        declineAction.isAuthenticationRequired = false
        
        let maybeAction = UIMutableUserNotificationAction.init()
        maybeAction.identifier = "MAYBE_IDENTIFIER"
        maybeAction.title = "May Be"
        maybeAction.activationMode = .background
        maybeAction.isDestructive = false
        maybeAction.isAuthenticationRequired = false
        
        let inviteCategory = MONotificationCategory.init()
        inviteCategory.identifier = "INVITE_CATEGORY"
        inviteCategory.setActions([maybeAction,declineAction,acceptAction], for: .default)
        inviteCategory.setActions([declineAction,acceptAction], for: .minimal)
        
        let categoriesSet = Set.init([inviteCategory])
        return categoriesSet;
    }
}

As you can see in the example Accept, Decline and May Be actions are grouped to a category i.e,"INVITE_CATEGORY". For declaring categories for iOS8 and iOS9 use MONotificationCategory from our SDK.

Notification Categories

Its recommended not to change the actions grouped in a category across the app versions, as it will lead to user seeing different actions for same category across different app versions.

Tracking User Actions

FOR iOS10 and Above :

As mentioned earlier, use the delegate methods of UNUserNotificationCenter to handle actions of notifications and also call userNotificationCenter:didreceivenotificationresponse: of the SDK, to track the actions performed on notifications.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler{  
	[[MoEngage sharedInstance] userNotificationCenter:center didReceiveNotificationResponse:response];
	//---   
	completionHandler();  
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
	MoEngage.sharedInstance().userNotificationCenter(center, didReceive: response)
  //---
	completionHandler()
}

FOR iOS8 and iOS9

To track the action performed by the user on actionable notifications call handleActionWithIdentifier:forremotenotification: in application:handleactionwithidentifier:forRemoteNotification:completionhandler: as shown below :

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler:(void (^)()) completionHandler {
    ----
    [[MoEngage sharedInstance] handleActionWithIdentifier:identifier forRemoteNotification:notification]
    completionHandler();
}
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
//---
	if let identifier = identifier {
		MoEngage.sharedInstance().handleAction(withIdentifier: identifier, forRemoteNotification: userInfo)
	}
}