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)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容