UserNotifications
-
你必須匯入 UserNotifications
@import UserNotifications;
2.請求 localNotification 的授權
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
-
現在我們將更新應用程式圖示徽章編號
@IBAction func triggerNotification(){ let content = UNMutableNotificationContent() content.title = NSString.localizedUserNotificationString(forKey: "Tom said:", arguments: nil) content.body = NSString.localizedUserNotificationString(forKey: "Hello Mike!Let's go.", arguments: nil) content.sound = UNNotificationSound.default() content.badge = UIApplication.shared().applicationIconBadgeNumber + 1; content.categoryIdentifier = "com.mike.localNotification" //Deliver the notification in two seconds. let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1.0, repeats: true) let request = UNNotificationRequest.init(identifier: "TwoSecond", content: content, trigger: trigger) //Schedule the notification. let center = UNUserNotificationCenter.current() center.add(request) } @IBAction func stopNotification(_ sender: AnyObject) { let center = UNUserNotificationCenter.current() center.removeAllPendingNotificationRequests() }