0

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

Swarup
  • 11
  • 6

2 Answers2

1

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

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • @Swarup I don't have that one. You please refer apple documentations – Prashant Tukadiya Dec 05 '17 at 12:04
  • I don't understand how many action(Button) in you sample code. Can you please tell me and also the name? So it will very helpful for me. Thanks – Swarup Dec 05 '17 at 12:12
  • It is clear by it's name `createActionNotificationsSettingForApproveBID` two buttons approve and cancel , Please try that code by your self you will get understanding easily – Prashant Tukadiya Dec 05 '17 at 12:41
  • @Swarup welcome , If answer is helpful to you you can approve as accepted !! – Prashant Tukadiya Dec 05 '17 at 13:06
  • Yes I will do that after successful implement. – Swarup Dec 05 '17 at 13:09
  • hi @Prasant, when I implement interactive notification that time is need to declare 'didReceiveRemoteNotification' delegate method? Because I am getting crash when user click notification alert body(not click notification action button) – Swarup Dec 27 '17 at 07:25
  • @Swarup Don't know exactly but I think you need to declare – Prashant Tukadiya Dec 27 '17 at 12:54
  • I have implemented interactive notification which have two action button. If user click on notification body out of action buttons that time app is open and getting crash. how to solve this issue? I am not able to understand this. Thanks – Swarup Dec 27 '17 at 13:54
  • @Swarup Yes user might not click on any of buttons You have to handle this thing. Try to debug where is your app is crashing – Prashant Tukadiya Dec 28 '17 at 04:40
  • Hi @Prashant yes have to handle this thing. Can I disable user interaction when user click notification body(apart from 2buttons click)? – Swarup Dec 28 '17 at 05:55
  • @Swarup Nope !! You can't , If use don't click on any button you should handle this response as per either button 1 or button 2 – Prashant Tukadiya Dec 28 '17 at 05:57
  • @Swarup For you it is calling or not ? – Prashant Tukadiya Dec 28 '17 at 06:27
  • tell me one thing, when click notification body that time 'didReceiveRemoteNotification' delegate method is call or not? Because when User click interactive notification's button that time call 'handleActionWithIdentifier' this delegate method. right? please cleare this topic. I go through many documents but did.t find anything. Thanks – Swarup Dec 28 '17 at 06:28
  • @Swarup If user tap on button handleActionWithIdentifier will call and if user tap on notification then didReceiveRemoteNotification will call – Prashant Tukadiya Dec 28 '17 at 06:31
  • hi @prashant I have not enough reputation to mark this answers as accepted. can you vote this question? Thanks – Swarup Jan 02 '18 at 09:39
  • @Swarup Okay no problem :) – Prashant Tukadiya Jan 02 '18 at 09:42
0

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

SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39