ios - 如何每天重复一个 UNUserNotification?

标签 ios swift ios10 unusernotificationcenter

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

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)
    }
})

非常感谢任何帮助

最佳答案

在您的情况下,更改此行:

let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: false)

let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: true)

来自文档:

UNCalendarNotificationTrigger:

Triggers a notification at the specified date and time. You use a UNCalendarNotificationTrigger object to specify the temporal information for the trigger condition of a notification. Calendar triggers can fire once or they can fire multiple times.

NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 8;
date.minute = 30; 
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
                     triggerWithDateMatchingComponents:date repeats:YES];

swift :

var date = DateComponents()
date.hour = 8
date.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)

关于ios - 如何每天重复一个 UNUserNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44313883/

相关文章:

ios - 自定义标题有零导出

ios - NSFetchedResultsController 返回重复项(相同的 objectID 和引用)

ios - 如何使用导航栏项目转到其他 View Controller ?

ios - 如何打开特定联系人的编辑联系人屏幕

ios - 如何使用 iOS Facebook SDK 迭代相册中的照片

ios - 在自定义 UITableViewCell 中访问 subview

ios - AVAudioPlayer应该在后台播放还是在主线程播放?

ios - UIScrollView 第一次创建在错误的位置

ios - 如果约束改变( swift )

ios - iOS10 上语音转文字?