0

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....

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
  • You need to use a different identifier if you want to schedule multiple notifications otherwise you will be replacing the existing notification with a new trigger time – Paulw11 Oct 26 '16 at 08:50
  • @Paulw11: Thanks for ur quick reply. Can i have some pinch of code. Please can u help me. ? – Soumya Ranjan Oct 26 '16 at 08:53
  • Just change the string in `requestWithIdentifier`. Perhaps add a sequence number – Paulw11 Oct 26 '16 at 08:54
  • 1
    Check this http://stackoverflow.com/questions/40216915/multiple-unusernotifications-not-firing – kb920 Oct 26 '16 at 08:55
  • @Paulw11 : Thanks buddy, Its working. And how can I get the particular notification click in notification bar. Can u help me for this ? – Soumya Ranjan Oct 26 '16 at 09:03
  • It is delivered to your Notification Center delegate http://cleanswifter.com/ios-10-local-notifications/ – Paulw11 Oct 26 '16 at 09:14

0 Answers0