0

I am trying to generate a local notification with 2 actions. However, even though the notifications are popping up. I am not able to see the actions.

Is this because of something new in iOS 10

my viewcontroller,h has code:

-(void)createNotifications: (int)seconds{
        UILocalNotification *local = [[UILocalNotification alloc]init];
        local.fireDate = [[NSDate date]dateByAddingTimeInterval:seconds];
        local.timeZone = nil;
        local.alertBody = @"Alert body";
        local.alertTitle = @"Alert Title";
        local.alertAction = @"Okay";
        local.soundName = UILocalNotificationDefaultSoundName;
        local.applicationIconBadgeNumber = 4127;
        local.category = @"MAIN_CATEGORY";


        [[UIApplication sharedApplication] scheduleLocalNotification:local];
    }

    -(void)requestPermissionToNotify{

        UIMutableUserNotificationAction *action  = [[UIMutableUserNotificationAction alloc]init];
        action.identifier = @"FLOAT_ACTION";
        action.title = @"float";
        action.activationMode = UIUserNotificationActivationModeBackground;
        action.destructive = YES;
        action.authenticationRequired = NO;

        UIMutableUserNotificationAction *stingAction  = [[UIMutableUserNotificationAction alloc]init];
        stingAction.identifier = @"STING_ACTION";
        stingAction.title = @"sting";
        stingAction.activationMode = UIUserNotificationActivationModeForeground;
        stingAction.destructive = NO;
        stingAction.authenticationRequired = NO;

        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
        category.identifier = @"MAIN_CATEGORY";

        NSSet *categories = [NSSet setWithObjects:category, nil];


        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [category setActions:@[action,stingAction] forContext:UIUserNotificationActionContextDefault];
    }

my appdelegate.m has callback handlers:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    application.applicationIconBadgeNumber = 0;
    UILocalNotification *localnotif = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if(localnotif){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while launch" message:localnotif.alertBody preferredStyle:UIAlertViewStyleDefault];
        UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
        [alert addAction:aa];

        dispatch_async(dispatch_get_main_queue(), ^{
            [application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        });


    }
    return YES;
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    application.applicationIconBadgeNumber = 0;

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while running" message:notification.alertBody preferredStyle:UIAlertViewStyleDefault];
    UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
    [alert addAction:aa];

    dispatch_async(dispatch_get_main_queue(), ^{
        [application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    });
}

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while action" message:identifier preferredStyle:UIAlertViewStyleDefault];
        UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
        [alert addAction:aa];

        dispatch_async(dispatch_get_main_queue(), ^{
            [application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        });

    completionHandler();



}
Ackman
  • 1,562
  • 6
  • 31
  • 54
  • Possible duplicate of [UILocalNotification is deprecated in iOS10](http://stackoverflow.com/questions/37938771/uilocalnotification-is-deprecated-in-ios10) – mfaani Apr 28 '17 at 20:02
  • Thank you I am looking into it :) – Ackman Apr 28 '17 at 21:36
  • check this link : https://jamesrochabrunsite.wordpress.com/2016/11/09/local-notifications-in-ios-10/ for clear understanding – NAVEEN KUMAR Apr 29 '17 at 08:24

1 Answers1

0

You might be going in the wrong direction for adding action in the local notification. You need to add category along with action identifier while asking for permission. Please check below code for adding action and handling action tap in notification

// Ask for permission to allow Notification
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")){
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

        UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"ActionIdentifier" title:@"NewAction" options:UNNotificationActionOptionNone];
        NSArray *notificationActions = @[action];

        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"Notification" actions:notificationActions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
        NSSet *categories = [NSSet setWithObject:category];

        [center setNotificationCategories:categories];
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
            if(!granted) {

            }
        }];
    }
    else {
        UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
        action.identifier = @"ActionIdentifier";
        action.title = @"NewAction";
        action.activationMode = UIUserNotificationActivationModeBackground;
        action.destructive = NO;
        action.authenticationRequired = NO;

        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
        category.identifier = @"ActionIdentifier";
        [category setActions:@[action] forContext:UIUserNotificationActionContextDefault];
        [category setActions:@[action] forContext:UIUserNotificationActionContextMinimal];

        NSSet *categories = [NSSet setWithObjects:category, nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:categories]];
    }

// Handle Notification for iOS 10
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    // Handle Notification here
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
    NSString *actionIdentifier = response.actionIdentifier;
    BOOL newActionTapped = [actionIdentifier isEqualToString:@"NewAction"];

    if(snooze) {
            // Handle NewAction action button here
    }
    else {
        // Handle Notification here
    }
    completionHandler();
}

// Handle Notification for iOS 9 or lower
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    // Handle Notification here
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {
    // This method will be called when user tap on Action
    // Handle NewAction action button here
    if (completionHandler) {
        completionHandler();
    }
}
Patrick R
  • 6,621
  • 1
  • 24
  • 27