如何在用户通知中设置重复频率 [英] How to set repeat frequency in User Notification
问题描述
直到 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宣布用户通知
其中包含
and in local notification we have repeatInterval
, Now in WWDC2016
Apple announced User Notification
which contains
- UNTimeIntervalNotificationTrigger。
-
UNCalendarNotificationTrigger。
- UNTimeIntervalNotificationTrigger.
UNCalendarNotificationTrigger.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
上面的代码会在每分钟后触发通知。但无法设置日期。
the code above will trigger notification after every minute. But can't set the date.
NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 8;
date.minute = 30;
UNCalendarNotificationTrigger* triggerC = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
上面的代码可以设置,重复将在明天8:30触发,而不是在分钟后。
the code above date can be set and repeat will trigger tomorrow at 8:30 not not after minute.
在iOS 10中用户通知
我如何设置重复频率的日期时间,就像我们可以设置 UILocalNotification
?
In iOS 10 User Notification
how I can set date time with repeat frequency just like we can set in UILocalNotification
?
我想明天在8点安排用户通知
:下午30点,每分钟后继续重复,就像我在顶部指定的关于本地通知的代码
I want to schedule User Notification
tomorrow at 8:30pm and keep repeating after every minute just like the code I specified at the top regarding local notification
推荐答案
受@Ramon Vasconcelos启发,我使用以下代码设置间隔和fireDate
inspired by @Ramon Vasconcelos, I'm using the following code for setting up intervals and fireDate together
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];
这篇关于如何在用户通知中设置重复频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!