Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple UNUserNotifications not firing

I'm setting multiple UNUsernotifications as below,

- (void)viewDidLoad {
    [super viewDidLoad];
    notifCount = 0;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
                              if (!error) {
                                  NSLog(@"request succeeded!");

                                  [self set10Notif];

                              }
                          }];
}

In the set10Notif method, I'm setting multiple(8 for testing) notifications with time 10 seconds from current time.

-(void) set10Notif
{
    notifCount = notifCount+1;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0") && notifCount < 10)
    {
        // create actions
      NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

      [calendar setTimeZone:[NSTimeZone localTimeZone]];

       NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];

       NSString *fireStr = [self returnStringFromDate:fireD withFormat:@"hh/mm/ss dd/MM/yyyy"];

       NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:fireD];

      UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
    objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notif!" arguments:nil];
    objNotificationContent.body = [NSString localizedUserNotificationStringForKey:fireStr
                                                                        arguments:nil];
    objNotificationContent.sound = [UNNotificationSound defaultSound];

      UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];

      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer"
                                                                          content:objNotificationContent trigger:trigger];
      UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter];
      [userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {

            NSLog(@"Local Notification succeeded");
        }
        else {
            NSLog(@"Local Notification failed");
        }
        [self set10Notif];
       }];
   #endif
    }
}

All the local notifications are set. But only one notification got fired in the device, that's the last one.

Why can't the multiple notifications are not firing? What did I do wrong?

like image 554
Nazik Avatar asked Dec 04 '22 23:12

Nazik


1 Answers

Give a different "requestWithIdentifier" and "time delay" for each notification and try, may it works for you.

like image 135
AshokPolu Avatar answered Dec 21 '22 19:12

AshokPolu