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 – 如何在用户通知中设置重复频率全部内容,希望文章能够帮你解决ios – 如何在用户通知中设置重复频率所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。