美文网首页
iOS 友盟集成推送通知 部分代码记录

iOS 友盟集成推送通知 部分代码记录

作者: 犯色戒的和尚 | 来源:发表于2018-03-08 09:56 被阅读0次

程序启动之后在下边的方法中

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        setupNotification(with: Preferences.notificationConfig, launchOptions: launchOptions)
}
  func setupNotification(with config: Preferences.NotificationConfig, launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
        UMessage.start(withAppkey: config.appKey, launchOptions: launchOptions)

        UIApplication.shared.registerForRemoteNotifications()

        registerRemoteNotification()

        UMessage.setLogEnabled(true)
    }

    func registerRemoteNotification() {

        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in
                if granted && error != nil {
                    log.debug("注册成功")

                }
            })
            center.getNotificationSettings(completionHandler: { setting in
                log.debugExec {
                    log.debug(setting
                    )
                }
            })

        } else {

            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil))

        }

    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        log.debug(notification.request.content.userInfo)
        if notification.request.trigger is UNPushNotificationTrigger {
            UMessage.setAutoAlert(false)
            UMessage.didReceiveRemoteNotification(notification.request.content.userInfo)

            if let userInfo: [String:Any] = notification.request.content.userInfo as? [String : Any] {
                var businessData: String = userInfo["business_data"] as? String ?? ""
                var type: String = userInfo["type"] as? String ?? ""
                var d: String = userInfo["d"] as? String ?? ""
                var p: String = userInfo["p"] as? String ?? ""
                var messageClass: String = userInfo["message_class"] as? String ?? ""
                //页面跳转

                let messageController = WKWebViewController("\(webURL)/#/assNotice?messageclass=\(messageClass)")

                Authorization.authorized {
                    WKTabBarControllerConfig.homeNavigationController.pushViewController(messageController, animated: true)
                }

            }

        } else {
            //应用处于前台时的本地推送接收

        }

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

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

        let userInfo = response.notification.request.content.userInfo
        log.debug(userInfo)
        if response.notification.request.trigger is UNPushNotificationTrigger {
            //应用处于后台时的远程推送接受
            UMessage.didReceiveRemoteNotification(response.notification.request.content.userInfo)
        } else {
            //应用处于后台时的本地推送接受
        }
        completionHandler()
    }

application 代理的实现

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        log.debugExec {
            let data = NSData(data: deviceToken)
            var token = data.description.trimmingCharacters(in: CharacterSet(charactersIn: "<>"))
            token = token.replacingOccurrences(of: " ", with: "")
            log.info("get remote notification device token:\(token)")
        }

        UMessage.registerDeviceToken(deviceToken)

        if Account.shareAccount.phone.count > 0 {
            UMessage.setAlias(Authorization.Account.username, type: "account", response: { (responseObject, _) in
                log.debug(responseObject)
            })
        }
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        UMessage.didReceiveRemoteNotification(userInfo)
        completionHandler(UIBackgroundFetchResult.newData)

    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        log.error("register remote notification error: \(error.localizedDescription)")
    }

相关文章

网友评论

      本文标题:iOS 友盟集成推送通知 部分代码记录

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