2017-05-23 21 views
0

我已经实现了iOS 10本地通知,它适用于第一次闹钟,但不适用于其他闹钟。我已经导入了iOS 10库,实现了委托,在委托中第一次接收它,但它不会稍后触发。iOS本地通知不是第二次触发,而是在取消通知请求中显示

我的日期,甲酸是这样的:2017年5月28日14时04分07秒+0000

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    [calendar setTimeZone:[NSTimeZone localTimeZone]]; 
    //(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) 
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now]; 

    NSDate *todaySehri = [calendar dateFromComponents:components]; 


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

      objNotificationContent.title = [NSString localizedUserNotificationStringForKey:[NSString stringWithFormat:@"Local.."]] arguments:nil]; 


        objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Incoming Local Notification" arguments:nil]; 


      objNotificationContent.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@",soundFileBtn.titleLabel.text]]; // [UNNotificationSound defaultSound];//soundFileBtn.titleLabel.text;// [UNNotificationSound defaultSound]; 
      NSDictionary *dict = @{@"alarmID":self.myKey, 
                @"SOUND_TYPE":[NSString stringWithFormat:@"%li",(long)self.audio_segment.selectedSegmentIndex] 
                }; 
      NSArray *array = [NSArray arrayWithObjects:dict, nil]; 
      NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"]; 


      [objNotificationContent setUserInfo:data]; 

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

      NSCalendar *cal = [NSCalendar currentCalendar]; 

       NSDateComponents *compss= [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) 
          fromDate:todaySehri]; 

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

      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:[todaySehri description] 
                        content:objNotificationContent trigger:trigger]; 
      //schedule localNotification 
      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
      [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
       if (!error) { 
        NSLog(@"Local Notification succeeded"); 
       } 
       else { 
        NSLog(@"Local Notification failed"); 
       } 
      }]; 
+0

尝试打印您为每个触发器设置的'identifier',以检查它们是否不同。另外你如何测试通知?通过手动更改设备上的时间或等待时间? – bhakti123

+0

标识符在我的NSLog中不同。即使我附加一个额外的随机字符串到我的标识符。我正在模拟器上检查报警。我设置了3个独立的警报。我设置我的MacBook时间接近第一次报警。当它工作,然后我再次改变我的MacBook时间接近第二次报警并运行我的项目。但它不适用于第二次或其他警报。 @ bhakti123 –

+1

检查是否所有的警报都是预定的。他们的下一个触发日期是什么。然后尝试根据该触发日期更改时间并查看它是否有效。 – bhakti123

回答

0

如何设置你的触发YES重复?另外,检查了这一点。回答为repeating local notifications

+0

不! 重复(YES)表示您希望每天或任何特定时间后查看。但是我在这里分别设置通知单独的一天。假设,我在3个不同的时间创建3个通知3天。 –

+0

在这种情况下,你应该保留三个标识符来分隔出不同的通知 –

+0

是的,我的标识符是requestWithIdentifier-> [todaySehri描述],它是每个触发器中不同的字符串。那我该怎么办? :( –