首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UNNotificationRequest中设置重复间隔和火灾日期

如何在UNNotificationRequest中设置重复间隔和火灾日期
EN

Stack Overflow用户
提问于 2017-11-06 11:03:33
回答 1查看 2.9K关注 0票数 4

以前,我在我的应用程序中使用UILocalNotification作为提醒用途。

但是如前所述,API在iOS中被否决了,现在,10.0需要使用UserNotifications框架的UNNotificationRequest

使用UILocalNotification,我可以设置点火日期和重复间隔,如下所示:

代码语言:javascript
复制
UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];
    localNotification.fireDate = tomorrow;


    if(repeatUnit!=0){
          localNotification.repeatInterval = NSWeekCalendarUnit;
    }

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

在上面的代码中,我可以设置明天通知将被触发的日期,而且我还设置了重复间隔1周,这将从火灾日期开始。

如何使用UserNotifications框架的UNNotificationRequest实现这一点。

因为在新的api中,我们可以使用触发器UNCalendarNotificationTriggerUNTimeintervalNotificationTrigger来启动UNNotificationRequest

是否有任何可用的方法可以像UILocalNotification一样设置这两种方式?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-11 11:23:34

您仍然可以使用火日期和重复间隔,但它并不像以前那么明显。初始化通知触发器时,可以将其重复参数设置为YES。重复间隔由通过dateMatching参数传递给触发器的date组件定义:

每日重复:

只需传递日期组件hourminutesecond

⟶触发器在那个时候每天都会开火

每周重复:

还传递所需的工作日组件:工作日hourminutesecond

⟶触发器将在那个工作日每周发射一次。

下面是一个例子,它在明天同一时间发出通知,并每周重复一次:

Objective-C:

代码语言:javascript
复制
UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];

// find out what weekday is tomorrow
NSDate *sameTimeTomorrow = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
NSInteger weekdayTomorrow = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate: sameTimeTomorrow];

// set the current time (without weekday)
unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute |  NSCalendarUnitSecond;
NSDateComponents *firstFireDateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:sameTimeTomorrow];

// add tomorrow's weekday
firstFireDateComponents.weekday = weekdayTomorrow;

// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
UNCalendarNotificationTrigger *notificationTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:firstFireDateComponents repeats:YES];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification" content:notification trigger:notificationTrigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler: nil];

Swift:

代码语言:javascript
复制
let notification = UNMutableNotificationContent()

// find out what weekday is tomorrow
let weekDayTomorrow = Calendar.current.component(.weekday, from: Date(timeIntervalSinceNow: 60*60*24))

// set the current time (without weekday)
var firstFireDateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())

// add tomorrow's weekday
firstFireDateComponents.weekday = weekDayTomorrow

// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: firstFireDateComponents, repeats: true)

let request = UNNotificationRequest(identifier: "notification", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47135461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档