每天在设定的时间间隔之间触发一次本地通知。 (例如,上午10:00至下午6.00,每天间隔5分钟)

时间:2019-11-10 06:35:16

标签: ios swift uilocalnotification unnotificationrequest usernotificationsui

我正在创建一个应用程序来设置本地通知,该通知可以在设置的时间之间触发,间隔可以由我选择,以分钟为单位。我设法运行了本地通知,但是不知道如何在一天的特定时间之间进行设置。

//型号

struct ReminderModal:Codable{
    var Title:String
    var Description:String
    var Frequency:TimeFrequencyType
    var Id:String
    var StartTime:Date?
    var EndTime:Date?
}
struct TimeFrequencyType:Codable {
    let id: Int
    let name: String
}

let frequencyTypeArr = [
    TimeFrequencyType(id: 1, name: "1 Minute"),
    TimeFrequencyType(id: 5, name: "5 Minutes"),
    TimeFrequencyType(id: 10, name: "10 Minutes"),
    TimeFrequencyType(id: 15, name: "15 Minutes"),
    TimeFrequencyType(id: 20, name: "20 Minutes"),
    TimeFrequencyType(id: 39, name: "30 Minutes"),
    TimeFrequencyType(id: 60, name: "60 Minutes")
]

//通知代码

let name = obj.Title
let description = obj.Description
let id =  obj.Id
let fid = obj.Frequency.id


// build notification
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "\(name)", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "\(description)", arguments: nil)
content.sound = UNNotificationSound.default
content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber;
content.categoryIdentifier = "com.qtechsoftware.ReminderApp"

// Deliver the notification
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: TimeInterval(fid*60), repeats: true)
let request = UNNotificationRequest.init(identifier: "\(id)", content: content, trigger: trigger)

// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)

源代码的Git路径-ReminderApp

1 个答案:

答案 0 :(得分:0)

您使用了错误的语法来触发它。 https://developer.apple.com/documentation/usernotifications/scheduling_a_notification_locally_from_your_app 使用本指南可以精确地创建所需的内容。

tl; dr:


let trigger = UNCalendarNotificationTrigger(
         dateMatching: dateComponents, repeats: true)