Skip to content

Instantly share code, notes, and snippets.

@chengyang1380
Last active March 30, 2018 15:06
Show Gist options
  • Save chengyang1380/f68f499ba555d1d13092c3c5546f7d6f to your computer and use it in GitHub Desktop.
Save chengyang1380/f68f499ba555d1d13092c3c5546f7d6f to your computer and use it in GitHub Desktop.
// 1.創建通知內容
let content = UNMutableNotificationContent()
content.title = "我是標題"
content.subtitle = "我是副標題"
content.body = "我是內容"
// 2.創建觸發條件,這裡是設定5秒後發出通知且不重複,注意這裡不能設定60秒內重複,會crash
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
// 3.請求標示符
let requestIdentifier = "easyNotification"
// 4.創建一個發送請求,將我們前面三個步驟所創的東西,丟進來就完成一個發送請求了
let request = UNNotificationRequest(identifier: requestIdentifier,
content: content,
trigger: trigger)
// 5.將請求添加發送中心
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
// 假如有錯誤就彈跳出警告視窗
UIAlertController.showConfirmAlert(message: error.localizedDescription, in: self)
} else {
print("成功發出一個簡單的通知")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment