这是我用来在我的应用程序上为 iOS 10 用户显示本地通知的 Swift 2.3 代码:
func showLocalNotif() {
if #available(iOS 10, *) {
let content = UNMutableNotificationContent()
content.title = "your title"
content.subtitle = "your subtitle"
content.body = "your message to the users"
content.categoryIdentifier = "message"
//Set the trigger of the notification -- here a timer.
let trig = UNTimeIntervalNotificationTrigger(
timeInterval: 100,
repeats: false)
//Set the request for the notification from the above
let request = UNNotificationRequest(
identifier: "100.second.message",
content: content,
trigger: trig
)
//Add the notification to the current notification center
UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, withCompletionHandler: addNotificationRequestHandler)
}}