参见英文答案 >
How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?3个
直到iOS 9,我们写这样的本地通知
直到iOS 9,我们写这样的本地通知
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = pickerDate; localNotification.alertBody = self.textField.text; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitMinute; localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
并且在本地通知中我们有repeatInterval,现在在WWDC2016中Apple公布了包含的用户通知
> UNTimeIntervalNotificationTrigger.
> UNCalendarNotificationTrigger.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
NSDateComponents* date = [[NSDateComponents alloc] init]; date.hour = 8; date.minute = 30; UNCalendarNotificationTrigger* triggerC = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
上面的代码可以设置,重复将在明天8:30触发,而不是在分钟后.
在iOS 10用户通知中,我如何设置重复频率的日期时间,就像我们可以在UILocalNotification中设置一样?
解决方法
对于iOS 10,您可以这样使用:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate]; UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];