如何在用户通知中设置重复频率[重复]
Posted
技术标签:
【中文标题】如何在用户通知中设置重复频率[重复]【英文标题】:How to set repeat frequency in User Notification [duplicate] 【发布时间】:2016-06-23 06:01:18 【问题描述】:直到ios 9
我们这样写local notifications
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 宣布User Notification
包含
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 User Notification
中,我如何像在 UILocalNotification
中设置一样设置重复频率的日期时间?
我想在明天晚上 8:30 安排User Notification
,并在每分钟之后重复一次,就像我在顶部指定的关于local notification
的代码一样
【问题讨论】:
大家好,我也有同样的问题。请让我们知道是否有人已经实现了此功能? 仅供参考: 问题是“如何为 iOS 10 中的本地通知设置重复间隔,例如每天、每月、每分钟等”。我在等。谢谢。 找到解决上述问题的方法了吗? 也很相关:UserNotification in 3 days then repeat every day/hour - iOS 10 【参考方案1】:对于 iOS 10,您可以这样使用:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate];
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
它与 iOS 9 代码的效果相同。要重复,您只需使用要重复的组件即可。
【讨论】:
【参考方案2】:受@Ramon Vasconcelos 的启发,我使用以下代码来设置间隔和fireDate 一起
switch (interval)
case NSCalendarUnitMinute:
unitFlags = NSCalendarUnitSecond;
break;
case NSCalendarUnitHour:
unitFlags = NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
case NSCalendarUnitDay:
unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
case NSCalendarUnitWeekOfYear:
unitFlags = NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
case NSCalendarUnitMonth:
unitFlags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
case NSCalendarUnitYear:
unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
default:
unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
break;
NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:fireDate];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:interval != 0];
【讨论】:
以上是关于如何在用户通知中设置重复频率[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何在每天不同的时间在android studio中设置每日重复通知?