swift 本地通知的应用 UNUserNotificationCenter推送

这篇文章讲的是 ios 10.0 之后的试用方法

文章结构

1 注册通知
2 接下来就是发送本地通知
3 关于通知的四种触发器
4关于通知的事件

1 注册通知

在下面这个方法中注册通知

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

这个是注册部分的代码以及注册的回调

        //-- 注册推送
        let center = UNUserNotificationCenter.current()
        center.delegate = self as UNUserNotificationCenterDelegate
        center.getNotificationSettings { (setting) in
            if setting.authorizationStatus == .notDetermined {
                // 未注册
                center.requestAuthorization(options: [.badge,.sound,.alert]) { (result, error) in
                    print("显示内容:\(result) error:\(String(describing: error))")
                    if(result){
                        if !(error != nil){
                            print("注册成功了!")
                            application.registerForRemoteNotifications()
                        }
                    } else{
                        print("用户不允许推送")
                    }
                }
            } else if (setting.authorizationStatus == .denied){
                //用户已经拒绝推送通知
                //-- 弹出页面提示用户去显示
                
            }else if (setting.authorizationStatus == .authorized){
                //已注册 已授权 --注册同志获取 token
                // 请求授权时异步进行的,这里需要在主线程进行通知的注册
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
                
            }else{
                
            }
        }

2 接下来就是发送本地通知

        //设置推送内容
        let content = UNMutableNotificationContent()
        content.title = "aaa.com"
        content.body = "哈哈哈"
        content.userInfo = ["id": "id66", "articleId": 999]
        content.sound = UNNotificationSound.default
        
        // 定义触发的时间组合
        var matchingDate = DateComponents()
//        matchingDate.hour = 14
//        matchingDate.minute = 24
        matchingDate.second = 0
        //设置通知触发器
        let trigger =  UNCalendarNotificationTrigger.init(dateMatching: matchingDate, repeats: true)
         
        //设置请求标识符
        let requestIdentifier = "com.WL.Test"
         
        //设置一个通知请求
        let request = UNNotificationRequest(identifier: requestIdentifier,
                                            content: content, trigger: trigger)
         
        //将通知请求添加到发送中心
        UNUserNotificationCenter.current().add(request) { error in
            if error == nil {
                VVLog("Time Interval Notification scheduled: \(requestIdentifier)")
            } else {
                VVLog("通知添加成功")
            }
        }

3 关于通知的四种触发器

UNTimeIntervalNotificationTrigger	延时多少秒后触发
UNCalendarNotificationTrigger	符合相应的时间模板触发
UNLocationNotificationTrigger	基于位置触发
UNPushNotificationTrigger	推送触发


UNTimeIntervalNotificationTrigger
public convenience init(timeInterval: TimeInterval, repeats: Bool)
TimeInterval 延迟多少秒后触发  
repeats 是否重复触发

UNCalendarNotificationTrigger
public convenience init(dateMatching dateComponents: DateComponents, repeats: Bool)
DateComponents 固定某一个时间点触发
repeats 是否重复

 比如每天的 14:24:0触发
 var matchingDate = DateComponents()
 matchingDate.hour = 14
 matchingDate.minute = 24
 matchingDate.second = 0

UNLocationNotificationTrigger

public convenience init(region: CLRegion, repeats: Bool)
CLRegion  是一个区域类型的类
repeats 是否重复

4关于通知的事件

Appdelegate这个代理 UNUserNotificationCenterDelegate并且实现代理方法

接收到推送的方法 app在后台or被kiil了都会走这个方法

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {  completionHandler()  }

当app在前台是推送触发走的回调

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    //下面这段代码调用后 会执行上面的方法 
    completionHandler([.alert, .sound])
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值