Multiple UNUserNotifications not firing
Asked Answered
H

2

4

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?

Hoar answered 24/10, 2016 at 11:9 Comment(3)
how did you make worked it for multiple notification ? i am stuck on same issue. Please have a look: #44133379Cryolite
@JamshedAlam, Give different requestWithIdentifier value in UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer" content:objNotificationContent trigger:trigger]; for each usernotification...Hoar
please see my question. I already have set different identifier there. But i am getting first alarm only, for the other alarm, it is not firing when i change my mac time, re run project for second alarm. #44133379Cryolite
S
15

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

Sherly answered 24/10, 2016 at 14:26 Comment(4)
Giving a different "requestWithIdentifier" does the trick. no need for time delay...Hoar
I was trying to do the same and want to fire location notification more than 1 times. But I was keeping "the requestWithIdentifier". I kept different "requestWithIdentifier". It also worked for me.Determinant
thanks its solved my problem .... Give a different "requestWithIdentifier" worked for me ...Scrobiculate
@Scrobiculate : Can you share the code it will be great helpQuirinal
P
1

MULTIPLE NSUSERNOTIFICATION FIRE ON CUSTOM DATE AND TIME

UNUserNotificationCenter *center=[UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:( UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                      completionHandler:^(BOOL granted, NSError * _Nullable error) {
                          if (!error) {
                              NSLog(@"request authorization succeeded!");
                              // [self showAlert];
                          }
                      }];

UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];

objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"શ્રી હરિ જયંતિ!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"જય સ્વામિનારાયણ \nઆવતીકાલે એકાદશી છે!"
                                                                    arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"nirnay" withExtension:@"jpg"];
NSError *error;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"nirnay" URL:fileURL options:nil error:&error];
objNotificationContent.attachments = @[attachment];

/// 4. update application icon badge number
// objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);



for (int i=0; i<5; i++)
{
    int hours=11;
    int mint=46+i;

    NSString *myDateAsAStringValue=@"2017-03-22";
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    // or @"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM


    NSDate *myDate = [dateFormatter dateFromString: myDateAsAStringValue];
    NSDate *pickerDate = myDate;
    NSLog(@"pickerDate - %@",pickerDate);
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
    //  [dateFormatter setDateFormat:DT_FORMATE_BIRTHDATE];
    [dateFormatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter2 setTimeZone:gmt];
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *timeComponents = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute|NSCalendarUnitSecond)
                                                   fromDate:pickerDate];
    [timeComponents setHour:hours];
    [timeComponents setMinute:mint];
    [timeComponents setSecond:0];
    NSDate *dtFinal = [calendar dateFromComponents:timeComponents];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
    NSString *fierDate = [formatter stringFromDate:dtFinal];

    NSLog(@"fierDate - %@",fierDate);
    UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:timeComponents
                                                                                                       repeats:NO];



    UNNotificationRequest *request3 = [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:@"ten%d",i]
                                                                           content:objNotificationContent trigger:trigger3];

    /// 3. schedule localNotification
    [center addNotificationRequest:request3 withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Local Notification succeeded By calender");
        }
        else {
            NSLog(@"Local Notification failed");
        }
    }];


}
Pudendum answered 22/3, 2017 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.