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

参见英文答案 > How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?3个
直到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中设置一样?

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

解决方法

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

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...