How to implement interactive remote notifications in iOS Objective-c

995 views Asked by At

I am new in Objective-c. I want to create a interactive remote notification which have two action, one is call 'OK' and another is called 'VIEW'. When user receive notification through APNS that time if user click 'OK' the notification have to be dismiss and if user click 'VIEW' that time open a particular page of my app. That's it.

I have go through many web documents and grab some concept of notification payload. But I am not able to implement this step wise. Can any one assist me, how can I implement this functionality. And please anyone don't mark as a duplicate question. Thanks

2

There are 2 answers

0
SachinVsSachin On

With the iOS 12 SDK, your app take advantage of Interactive Controls in Notifications

Notification content app extensions now support user interactivity in custom views. If the content of your app’s notifications needs to prompt user interaction, add controls like buttons and switches.

To enable user interactions:

Open your Notification Content Extension’s info.plist file.

Add the UNNotificationExtensionUserInteractionEnabled key to your extension attributes. Give it a Boolean value, set to YES. enter image description here

Here is reference like to know more

https://developer.apple.com/documentation/usernotificationsui/customizing_the_appearance_of_notifications

https://developer.apple.com/documentation/usernotificationsui/customizing_the_appearance_of_notifications

https://developer.apple.com/documentation/usernotificationsui

16
Prashant Tukadiya On

Note: I have use my own constants here for category like KNotificatoin_IDENTIFIER_CATEGORY_NEW_BID use your at that place

Register for push

- (void) registerPushNotification {

    UIUserNotificationType  type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |  UIUserNotificationTypeSound;
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:[NSSet setWithObjects:[self createActionNotificationsSettingForApproveBID],[self createActionNotificationsSettingForCancelingRequest ], nil]];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

}


- (UIMutableUserNotificationCategory *) createActionNotificationsSettingForApproveBID {

    UIMutableUserNotificationAction *actionApproveBID = [[UIMutableUserNotificationAction alloc]  init];
    [actionApproveBID setIdentifier:KNotificatoin_IDENTIFER_ACTION_APPROVEBID];
    actionApproveBID.activationMode = UIUserNotificationActivationModeForeground;
    actionApproveBID.title = @"Approve";
    actionApproveBID.authenticationRequired = true;
    [actionApproveBID setDestructive: false];


    UIMutableUserNotificationAction *actionCancelDialog = [[UIMutableUserNotificationAction alloc]  init];
    [actionCancelDialog setIdentifier:KNotificatoin_IDENTIFER_ACTION_DETAILS];
    actionCancelDialog.activationMode = UIUserNotificationActivationModeForeground;
    actionCancelDialog.title = @"Details";
    actionCancelDialog.authenticationRequired = true;
    [actionCancelDialog setDestructive: false];

    UIMutableUserNotificationCategory *cateogoryApproveBID = [[UIMutableUserNotificationCategory alloc] init];
    cateogoryApproveBID.identifier = KNotificatoin_IDENTIFIER_CATEGORY_NEW_BID;
    [cateogoryApproveBID setActions:@[actionApproveBID,actionCancelDialog] forContext:UIUserNotificationActionContextDefault];
    [cateogoryApproveBID setActions:@[actionApproveBID,actionCancelDialog] forContext:UIUserNotificationActionContextMinimal];

    return cateogoryApproveBID;
}

- (UIMutableUserNotificationCategory *) createActionNotificationsSettingForCancelingRequest {

    UIMutableUserNotificationAction *actionGetMoreBids = [[UIMutableUserNotificationAction alloc]  init];
    [actionGetMoreBids setIdentifier:KNotificatoin_IDENTIFER_ACTION_APPROVEBID];
    actionGetMoreBids.activationMode = UIUserNotificationActivationModeForeground;
    actionGetMoreBids.title = @"Get more bids";
    actionGetMoreBids.authenticationRequired = true;
    [actionGetMoreBids setDestructive: false];


    UIMutableUserNotificationAction *actionEditRequest = [[UIMutableUserNotificationAction alloc]  init];
    [actionEditRequest setIdentifier:KNotificatoin_IDENTIFER_ACTION_EDIT_REQUEST];
    actionEditRequest.activationMode = UIUserNotificationActivationModeForeground;
    actionEditRequest.title = @"Edit request";
    actionEditRequest.authenticationRequired = true;
    [actionEditRequest setDestructive: false];

    UIMutableUserNotificationCategory *categoryCancelRequest = [[UIMutableUserNotificationCategory alloc] init];
    categoryCancelRequest.identifier = KNotificatoin_IDENTIFER_ACTION_MORE_BIDS;
    [categoryCancelRequest setActions:@[actionGetMoreBids,actionEditRequest] forContext:UIUserNotificationActionContextDefault];
    [categoryCancelRequest setActions:@[actionGetMoreBids,actionEditRequest] forContext:UIUserNotificationActionContextMinimal];

    return categoryCancelRequest;
}

How you will handle actions ?

- (void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
    NSLog(@" APPLICATION STATUS %ld",(long)[UIApplication sharedApplication].applicationState);

    if ([[[userInfo objectForKey:@"aps"] objectForKey:kCategory] isEqualToString:KNotificatoin_IDENTIFIER_CATEGORY_NEW_BID]){
        if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_APPROVEBID]) {
            NSMutableDictionary *dictData = [NSMutableDictionary dictionaryWithDictionary:userInfo];
            [dictData setObject:@17 forKey:kType];

            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:dictData];
            } else {
                [self navigatateAsPerPush:dictData allowInActiveState:NO];
            }
        }
        else if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_DETAILS]) {
            NSLog(@"You chose action 2.");
            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:userInfo];
            } else {
                [self navigatateAsPerPush:userInfo allowInActiveState:NO];
            }
        }
    } else  if ([[[userInfo objectForKey:@"aps"] objectForKey:kCategory] isEqualToString:KNotificatoin_IDENTIFIER_NOTIFICATION_REQUEST]){

        NSMutableDictionary *dictData = [NSMutableDictionary dictionaryWithDictionary:userInfo];

        if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_EDIT_REQUEST]) {
            NSLog(@"You chose action 1.");
            [dictData setObject:@16 forKey:kType];
            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:dictData];
            } else {
                [self navigatateAsPerPush:dictData allowInActiveState:NO];
            }

        }
        else if ([identifier isEqualToString:KNotificatoin_IDENTIFER_ACTION_MORE_BIDS]) {
            NSLog(@"You chose action 2.");
            if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive || self.isApplicationIsInActiveState) {
                [self saveDictionaryForPushActiveState:dictData];
            } else {
                [self navigatateAsPerPush:dictData allowInActiveState:NO];
            }

        }

    }

    if (completionHandler) {
        completionHandler();
    }
}

Hope it is helpful to you