Search code examples
objective-cios9actionuilocalnotificationlocalnotification

Schedule UILocalNotification


I want actionable buttons upon receiving a notification. For handling remote push notification, I added following code -

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIApplication *application = [UIApplication sharedApplication];

if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) ) {
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeForeground];
    [action1 setTitle:@"action1Text"];
    [action1 setIdentifier:@"action1id"];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:kMTNRemindLaterCategory];
    [actionCategory setActions:@[action1]
                    forContext:UIUserNotificationActionContextDefault];

    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

}
}

It works perfectly. I am able to see the button and handle the action. Now, upon clicking the button and based on some more logic, I want to schedule a local notification with the same actionable button as defined above. I added the code (NOT in AppDelegate file):

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"title 4";
localNotification.hasAction = YES;
localNotification.alertAction = @"action";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.category = kRemindLaterCategory;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

The local notification is scheduled and I receive the notification. But I don't see any actionable button.

I read apple documentation where

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

seems useful. But this is triggered upon clicking the notification. Other posts I referred: https://www.shinobicontrols.com/blog/ios8-day-by-day-day-25-notification-actions actions and categories don't show in UILocalNotification in iOS How can I add action buttons/actions to UILocalNotification alert?

Can anyone please point me in the right direction?


Solution

  • kRemindLaterCategory value and kMTNRemindLaterCategory should be same, Then You can see the actionable button with local notification also, If both are not same, actionable button can't be visible because assigned category with local notification has not been registered.