3

多UNUserNotifications我如下設置多個UNUsernotifications,未擊發

- (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]; 

           } 
          }]; 
} 

set10Notif方法,我設置(用於測試8)的多個隨時間通知從當前時間起10秒。

-(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 
    } 
} 

設置了所有本地通知。但是隻有一個通知在設備中被觸發,這是最後一個。

爲什麼多個通知不能觸發? 我做錯了什麼?

+0

你是怎麼做的工作是爲多個通知?我被困在同一個問題上。請看看:https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –

+0

@JamshedAlam,在UNNotificationRequest中給出不同的requestWithIdentifier值 *請求= [UNNotificationRequest requestWithIdentifier:@「禱告」 內容:objNotificationContent觸發器:觸發器];'爲每個用戶通知... – NAZIK

+0

請看我的問題。我已經在那裏設置了不同的標識符。但是我只有第一個鬧鐘,對於另一個鬧鐘,當我改變我的MAC時間時,它不會觸發,重新運行第二個鬧鐘的項目。 https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –

回答

8

爲每個通知給出不同的「requestWithIdentifier」和「time delay」並嘗試,可能適用於您。

+3

給予不同的「requestWithIdentifier」的竅門。沒有時間延遲的需要... – NAZIK

+0

我正在嘗試做同樣的事情,並且想要觸發超過1次的位置通知。但我保留「requestWithIdentifier」。我保留了不同的「requestWithIdentifier」。它也爲我工作。 –

+0

感謝它解決了我的問題.... 給一個不同的「requestWithIdentifier」爲我工作... – vipulkumarmehta

0

多NSUSERNOTIFICATION火自定義日期和時間

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 *[email protected]"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"); 
     } 
    }]; 


}