问题描述
我正在尝试安排有关在应用程序中创建的提醒的通知。 每次创建新的提醒时,我都会运行此功能,以安排带有提醒信息的通知。
import Foundation
import NotificationCenter
func scheduleNotification(title: String,body: String,date: Date,notifUUID: UUID) {
// Notification center
let notifCenter = UNUserNotificationCenter.current()
// Ask for permission if necessary
notifCenter.getNotificationSettings(completionHandler: { settings in
if settings.authorizationStatus == .denied || settings.authorizationStatus == .notDetermined {
notifCenter.requestAuthorization(options: [.alert,.sound,.badge]) { granted,error in
if let error = error {
print(error)
}
}
}
})
// Create notification content
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.categoryIdentifier = "alarm"
content.sound = UNNotificationSound.default
// Specify delivery conditions
var dateComponents = DateComponents()
dateComponents = Calendar.current.dateComponents([.hour,.minute,.day,.month,.year],from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,repeats: true)
// Create the request from notification UUID,content and date trigger
let request = UNNotificationRequest(identifier: notifUUID.uuidString,content: content,trigger: trigger)
notifCenter.add(request) { (error) in
if error != nil {
print("Error while scheduling notification: \(error!)")
}
}
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print("NOTIFICA: ")
print(request)
}
})
}
我确定通知已安排好,因为在函数的最后6行中,我打印了每个计划的通知,并获得了以下信息:
<UNNotificationRequest: 0x600000593bd0; identifier: FFE7D4FE-8981-40B2-AB6B-F12C78D6522B,content: <UNNotificationContent: 0x60000307f810; title: <redacted>,subtitle: (null),body: (null),summaryArgument:,summaryArgumentCount: 0,categoryIdentifier: alarm,launchImageName:,threadIdentifier:,attachments: (
),badge: (null),sound: <UNNotificationSound: 0x6000020349a0>,realert: 0,trigger: <UNCalendarNotificationTrigger: 0x600000b88080; dateComponents: <NSDateComponents: 0x60000097d990> {
Calendar Year: 2020
Month: 8
Day: 26
Hour: 8
Minute: 26,repeats: YES>>
但是随后在模拟器中未发送通知,这是Xcode 12 beta 5的错误还是我做错了??
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)