ios – 如何在用户通知中设置重复频率

前端之家收集整理的这篇文章主要介绍了ios – 如何在用户通知中设置重复频率前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?3个
直到iOS 9,我们写这样的本地通知
  1. UILocalNotification* localNotification = [[UILocalNotification alloc] init];
  2. localNotification.fireDate = pickerDate;
  3. localNotification.alertBody = self.textField.text;
  4. localNotification.timeZone = [NSTimeZone defaultTimeZone];
  5. localNotification.repeatInterval = NSCalendarUnitMinute;
  6. localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
  7. [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

并且在本地通知中我们有repeatInterval,现在在WWDC2016中Apple公布了包含的用户通知

> UNTimeIntervalNotificationTrigger.
> UNCalendarNotificationTrigger.

  1. UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];

上面的代码会在每分钟后触发通知.但无法设定日期.

  1. NSDateComponents* date = [[NSDateComponents alloc] init];
  2. date.hour = 8;
  3. date.minute = 30;
  4.  
  5. UNCalendarNotificationTrigger* triggerC = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];

上面的代码可以设置,重复将在明天8:30触发,而不是在分钟后.

在iOS 10用户通知中,我如何设置重复频率的日期时间,就像我们可以在UILocalNotification中设置一样?

我希望明天晚上8:30安排用户通知,并在每分钟后继续重复,就像我在顶部指定的关于本地通知代码一样

解决方法

对于iOS 10,您可以这样使用:
  1. NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate];
  2. UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

它与iOS 9代码具有相同的效果.要重复,您只需使用要重复的组件即可.

猜你在找的iOS相关文章