美文网首页
APNs原生推送接入

APNs原生推送接入

作者: GiveMF | 来源:发表于2020-07-23 18:02 被阅读0次

    注:Xcode11.6 Swift5.1 最低版本:iOS 10.0

    1.在做下面接入之前需要把APNs的开发证书与发布证书搞定。
    2.同时需要后端同学接入APNs的时候需要把p12证书也给他们(证书最好是带密码的,无密码好像会报错。)


    推送证书.png
    Xcode项目配置.png

    Coding:

    // MARK: - 推送配置方法
    extension AppDelegate{
         
        // MARK: - 0.通知注册(只有在真机中起作用)
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
           
            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.alert,.sound,.badge]) { (accepted, error) in
                if !accepted {
                    YLog("用户不允许消息通知。")
                }else{
                    YLog("用户Accepted通知。")
                }
            }
            //获取DeviceToken注册
            UIApplication.shared.registerForRemoteNotifications()
        }
    
        // MARK: - 1.程序在前台时收到通知会触发
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      
            //可以根据业务需求发一些通知
        }
        
        // MARK: - 2.通知点击调用(只是在程序处于运行状态和前台状态调用)
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            guard let res = response.notification.request.trigger else {
                return
            }
            if res.isKind(of: UNPushNotificationTrigger.self) {
                //收到远程通知
            }else{
                // 收到本地通知
            }
                
            completionHandler()//系统要求需实现
        }
        
        // MARK: - 3.通知点击调用(APP被杀死状态下点击通知调用)
        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
           if application.applicationState == UIApplication.State.active {
                // 从前台接受消息app
                YLog("fetchCompletionHandler 前台接受消息  active \n\(userInfo) ")
            }else{
                // 从后台接受消息后进入app
                YLog("fetchCompletionHandler 后台接受消息 inactive|background \n\(userInfo) ")
            }
            completionHandler(.newData)
        }
        
        
        // MARK: - 4.实现注册 APNs 接口deviceToken&registrationID
        func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
                   
             YLog("APNs注册 deviceToken  -----> \(deviceToken.map { String(format: "%02hhx", $0) }.joined())")
    
        }
        
        // MARK: - 5.实现注册 APNs 失败接口(可选)
        func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
          
            YLog("注册远程通知&DeviceToken获取失败  \(error) ")
        }
    }
    

    注:APNs Tool (ANPs测试工具)
    Done.

    相关文章

      网友评论

          本文标题:APNs原生推送接入

          本文链接:https://www.haomeiwen.com/subject/zvvhlktx.html