Swift3.0系统自带的推送

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

    {

        Thread.sleep(forTimeInterval:1.0)

        //Push Notification

        registerAppNotificationSettings(launchOptions:launchOptions)

        window =UIWindow(frame: UIScreen.main.bounds);

        window?.rootViewController   =LoginViewController()

        window?.makeKeyAndVisible()

        returntrue

    }

    //Registered remote push notifications

    privatefunc registerAppNotificationSettings(launchOptions:[UIApplicationLaunchOptionsKey:Any]?) {

        if#available(iOS10.0, *) {

            let notifiCenter =UNUserNotificationCenter.current()

            notifiCenter.delegate =self

            let types =UNAuthorizationOptions(arrayLiteral: [.alert, .badge, .sound])

            notifiCenter.requestAuthorization(options: types) { (flag, error)in

                if flag {

                    print("iOS request notification success")

                }else{

                    print(" iOS 10 request notification fail")

                }

            }

        } else {//iOS8,iOS9registration

            let setting =UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)

            UIApplication.shared.registerUserNotificationSettings(setting)

        }


        DispatchQueue.main.async {

            UIApplication.shared.registerForRemoteNotifications()

            let userSettings =UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)

            UIApplication.shared.registerUserNotificationSettings(userSettings)

        }

    }

    func application(_ application:UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data) {

        

        var token =""

        for iin 0..<deviceToken.count {

            token = token +String(format: "%02.2hhx", arguments: [deviceToken[i]])

        }

        

        print("\n>>>[DeviceToken Success]:%@\n\n",token);

        

        UserManager.sharedInstance.saveToken(pushToken: token)

    }

    func application(_ application:UIApplication, didFailToRegisterForRemoteNotificationsWithError error:Error) {

        

        UserManager.sharedInstance.saveToken(pushToken:"")

        print("\n>>>[DeviceToken Error]:%@\n\n",error);

    }

    //iOS10 Feature: the front desk agent notified method processing

    @available(iOS10.0, *)

    privatefunc userNotificationCenter(center:UNUserNotificationCenter, willPresentNotification notification:UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) ->Void){

        let userInfo = notification.request.content.userInfo

        print("userInfo10:\(userInfo)")

        completionHandler([.sound,.alert])


    }


    //iOS10 Feature: proxy method of dealing with the backstage, click on the notification

    @available(iOS10.0, *)

    privatefunc userNotificationCenter(center:UNUserNotificationCenter, didReceiveNotificationResponse response:UNNotificationResponse, withCompletionHandler completionHandler: () ->Void){

        let userInfo = response.notification.request.content.userInfo

        print("userInfo10:\(userInfo)")

        completionHandler()

    }

    //iOS9

    privatefunc application(application:UIApplication, didReceiveRemoteNotification userInfo: [NSObject :AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) ->Void) {

        print("收到新消息Active\(userInfo)")

        if application.applicationState ==UIApplicationState.active {

            // The front desk to accept the message app

        }else{

            // The background after accepting the message into the app

            UIApplication.shared.applicationIconBadgeNumber = 0

        }

        

        showNotificationAlert(userInfo: userInfo)

        completionHandler(.newData)

        

    }

    @nonobjcfunc application(_ application:UIApplication, didReceiveRemoteNotification userInfo: [NSObject :AnyObject]){

        print(userInfo)

    }

    

    @available(iOS10.0, *)

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

         print("前台收到新消息Active\(notification.request.content.userInfo)")

        

        completionHandler([.sound,.alert,.badge])

    }

    @available(iOS10.0, *)

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

        

        let userInfo = response.notification.request.content.userInfo

        print("后台收到新消息Active\(userInfo)")

        

        if(UserManager.isLogin()){

            

            let notification = Notification(name: Notification.Name(rawValue:kNOTIFICATION_PUSH), object:nil, userInfo: userInfo)

            NotificationCenter.default.post(notification)


        }

        

        completionHandler()

    }


    

    func showNotificationAlert(userInfo:[NSObject :AnyObject]){

        

    }

    

    func applicationWillResignActive(_ application:UIApplication) {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

        application.applicationIconBadgeNumber =0

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值