首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何每天重复一次UNUserNotification?

如何每天重复一次UNUserNotification?
EN

Stack Overflow用户
提问于 2017-06-01 17:37:42
回答 2查看 4.1K关注 0票数 3

我试图让用户每天在特定的时间安排通知,打开应用程序。到目前为止,我已经能够通过计算从现在到用户选择的时间来调度第一个通知,并在X秒内调度通知。但是,我是否可以将通知设置为每天重复呢?以下是我代码的一部分,以防您感到困惑:

代码语言:javascript
复制
let newTime: Double = Double(totalDifference)
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: false)
let request = UNNotificationRequest(identifier: "openApp", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
    if error != nil {
        print(error!)
        completion(false)
    } else {
        completion(true)
    }
})

任何帮助都非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-01 17:57:44

在您的例子中,更改这一行:

代码语言:javascript
复制
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: false)

代码语言:javascript
复制
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: true)

从文件中:

UNCalendarNotificationTrigger

在指定的日期和时间触发通知。使用UNCalendarNotificationTrigger对象为通知的触发条件指定时间信息。日历触发器可以触发一次,也可以多次触发。

代码语言:javascript
复制
NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 8;
date.minute = 30; 
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
                     triggerWithDateMatchingComponents:date repeats:YES];

Swift:

代码语言:javascript
复制
var date = DateComponents()
date.hour = 8
date.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
票数 7
EN

Stack Overflow用户

发布于 2017-06-01 18:25:51

FOR SWIFT3

代码语言:javascript
复制
    let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44313883

复制
相关文章

相似问题

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