How to scheduled multiple local notification. In iOS 10 UILocalNotification
is changed to UNUserNotificationCenter
.
But I am not getting how to add multiple notification according to date/time. And how can I handle that notification on clicking on particular notification in notification bar.
Below is my code snippets :
// This is my App delegate method -------
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()<UNUserNotificationCenterDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
}
}];
return YES;
}
// This is my View Controoller -------
// Local Notification -------
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!" arguments:nil];
content.userInfo = [content userInfo];
content.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
NSDateComponents* date = [[NSDateComponents alloc] init];
date.day = 26;
date.month = 10;
date.year = 2016;
date.hour = 14;
date.minute = time; // Here time is different different time I want to schedule notification.
NSLog(@"date : %@",date);
// Deliver the notification in five seconds.
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
I am not getting how to add multiple notification and how to handle. I am stuck from last 2 days.
Please anyone know, help me....