【发布时间】:2021-10-27 23:19:08
【问题描述】:
我想在我的应用上安排每日通知。但问题是当应用程序打开时通知不会出现,只有当我退出应用程序时才会出现通知。
注意:我是ios开发的初学者
我的功能:
func sheduleDailyLocalNotification() {
let calendar = Calendar.current
let hour = calendar.component(.hour, from: currentDate)
let minute = calendar.component(.minute, from: currentDate)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
let content = UNMutableNotificationContent()
content.title = "iGrow Goals"
content.subtitle = "Don't forget to check your today's goal steps"
content.sound = UNNotificationSound.default
// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = hour
dateComponents.minute = minute
let trigger = UNCalendarNotificationTrigger(
dateMatching: dateComponents, repeats: false)
// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
// add our notification request
UNUserNotificationCenter.current().add(request)
}else if let error = error {
print(error.localizedDescription)
}
}
}
【问题讨论】: