Swift 2 2 整合lean cloud Push推送

liangtongzhuo發表於2017-12-14
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // 註冊一個通知 通知換控制器 “新特性”
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(switchRootViewController), name: SwitchRootViewControllerKey, object: nil)
        
        // 註冊 Cloud
        setCloud(launchOptions)

        //建立window
        window = windowNew()
 
        //註冊 通知APNs
        registerForPushNotifications(application)
        
        return true
    }

    //註冊 通知APNs
    func registerForPushNotifications(application: UIApplication) {
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
    }
   
    
    //註冊遠端推送通知了
    func application(application: UIApplication,didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        if notificationSettings.types != .None {
            application.registerForRemoteNotifications()
        }
    }
    ///deviceToken推送伺服器
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        AVOSCloud.handleRemoteNotificationsWithDeviceToken(deviceToken)
    }
    //失敗
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print(error)
    }
    
複製程式碼

相關文章