UNUserNotificationCenter. 本地推送

1.获取权限

       UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge ,.sound]) { (b:Bool, error:Error?) in
                 
                }

.alert: 弹窗
.badge: 角标
.sound: 通知音效
第一次会有系统授权弹出

2.授权状态

  let current = UNUserNotificationCenter.current()
  current.getNotificationSettings { (settings:UNNotificationSettings) in
   }

3.创建

let notificationContent:UNMutableNotificationContent = UNMutableNotificationContent()                
notificationContent.sound = UNNotificationSound.default                
notificationContent.title = "It's time to take care of your mind.".localized+""
notificationContent.userInfo = ["type":"mindful"]
notificationContent.badge = -1
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let identifier = "mindful_localNoti_\(index)"
let request = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

userinfo: 额外带的参数, 拿到通知可以处理
badge: >0 设置数字 =0 维持原来的数字。<0 隐藏
设置identifier 方便删除特定的推送

4.删除

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ids)

你可能感兴趣的:(UNUserNotificationCenter. 本地推送)