Skip to main content

本地通知

申请通知权限​

UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]){success, error in
if(success){
print("All Set!")
}else if let error = error{
print(error.localizedDescription)
}
}

设置通知内容​

let content = UNMutableNotificationContent()
content.title = "Feed the cat"
content.subtitle = "It looks hungry"
content.sound = UNNotificationSound.default

设置通知触发时间​

// show this notification five seconds from now
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

创建推送请求类​

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

将推送请求加入通知中心​

UNUserNotificationCenter.current().add(request)