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

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

 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)")
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,273评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,180评论 25 709
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 11,732评论 10 16
  • 少儿贺岁片《熊出没•变形记》极受欢迎。有家长不明白:《熊出没》?不是电视上成天演嘛! 是的,电视长年累月播映着《熊...
    就这也行阅读 5,219评论 3 11
  • 其实,刚开始我没有意识要记录下自己在事务所实习一个月的经历,但是,不久前在简书上看到有书友写下了自己在银行实习的经...
    go孙行者110阅读 9,342评论 0 16

友情链接更多精彩内容