gpt4 book ai didi

swift - 计划通知不适用于闹钟应用程序

转载 作者:行者123 更新时间:2023-11-30 10:50:54 28 4
gpt4 key购买 nike

您好,我正在尝试制作alram应用程序,我正在使用这篇文章https://stackoverflow.com/a/53902489使用计划通知我已经设置了时间但什么也没得到,我想设置时间并提醒一些事情并在特定时间响铃

    scheduleNotification(at: createDate(date : 6, month : 2, hour: 17, minute: 15, year: 2019), identifierUnic: stringUUID(), body: "Notification day", titles: "Notification titles1")


func createDate(date: Int, month : Int, hour: Int, minute: Int, year: Int)->Date {

var components = DateComponents()
components.hour = hour
components.minute = minute
components.year = year
components.day = date
components.month = month

components.timeZone = .current

let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: components)!
}


func scheduleNotification(at date: Date, identifierUnic : String, body: String, titles:String) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in }

var anniversary = DateComponents()
anniversary.day = 6
anniversary.month = 2
anniversary.hour = 5
anniversary.minute = 44
anniversary.second = 0

let trigger = UNCalendarNotificationTrigger(dateMatching: anniversary, repeats: true)
let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default
content.categoryIdentifier = identifierUnic

let request = UNNotificationRequest(identifier: identifierUnic, content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print(" We had an error: \(error)")
}}
}


func registerLocal() {
let center = UNUserNotificationCenter.current()

center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Yay!")
} else {
print("D'oh")
}
}
}

最佳答案

您可以尝试为您的请求创建一个唯一的 ID,并用它添加您的通知吗?

这是我的通知,对我来说效果很好

  1. 创建中心并检查授权

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) {
    (granted, error) in
    }
  2. 创建内容

    let content = UNMutableNotificationContent()
    content.title = "Title"
    content.subtitle = "Subtitle."
    content.body = "Body"
  3. 创建日期组件

    var anniversary = DateComponents()
    anniversary.day = 1
    anniversary.month = 1
    anniversary.hour = 12
    anniversary.minute = 0
    anniversary.second = 0
  4. 创建触发器

    let trigger = UNCalendarNotificationTrigger(dateMatching: anniversary, repeats: true)
  5. 创建唯一 ID 和请求

    let uuid = UUID().uuidString
    let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
  6. 将您的请求添加到中心并处理您的错误

    center.add(request) {
    (error) in
    //Write your handle function here..
    //print(error) for example
    }

希望对你有帮助...

关于swift - 计划通知不适用于闹钟应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com