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 ?? ""
                //頁面跳轉(zhuǎn)

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

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

            }

        } else {
            //應(yīng)用處于前臺時的本地推送接收

        }

        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 {
            //應(yīng)用處于后臺時的遠程推送接受
            UMessage.didReceiveRemoteNotification(response.notification.request.content.userInfo)
        } else {
            //應(yīng)用處于后臺時的本地推送接受
        }
        completionHandler()
    }

application 代理的實現(xiàn)

 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)")
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末漱病,一起剝皮案震驚了整個濱河市马澈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機鳍鸵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尉间,“玉大人偿乖,你說我怎么就攤上這事≌艹埃” “怎么了贪薪?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長眠副。 經(jīng)常有香客問我画切,道長,這世上最難降的妖魔是什么囱怕? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任霍弹,我火速辦了婚禮,結(jié)果婚禮上娃弓,老公的妹妹穿的比我還像新娘典格。我一直安慰自己,他們只是感情好台丛,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布耍缴。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪防嗡。 梳的紋絲不亂的頭發(fā)上变汪,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機與錄音本鸣,去河邊找鬼。 笑死硅蹦,一個胖子當著我的面吹牛荣德,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播童芹,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涮瞻,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了假褪?” 一聲冷哼從身側(cè)響起署咽,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎生音,沒想到半個月后宁否,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡缀遍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年慕匠,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片域醇。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡台谊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出譬挚,到底是詐尸還是另有隱情锅铅,我是刑警寧澤,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布减宣,位于F島的核電站盐须,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏漆腌。R本人自食惡果不足惜丰歌,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望屉凯。 院中可真熱鬧立帖,春花似錦、人聲如沸悠砚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绑咱,卻和暖如春绰筛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背描融。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工铝噩, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窿克。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓骏庸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親年叮。 傳聞我的和親對象是個殘疾皇子具被,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)只损,斷路器一姿,智...
    卡卡羅2017閱讀 134,652評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,083評論 25 707
  • 極光推送: 1.JPush當前版本是1.8.2,其SDK的開發(fā)除了正常的功能完善和擴展外也緊隨蘋果官方的步伐跃惫,SD...
    Isspace閱讀 6,719評論 10 16
  • 少兒賀歲片《熊出沒?變形記》極受歡迎爆存。有家長不明白:《熊出沒》衬横?不是電視上成天演嘛! 是的终蒂,電視長年累月播映著《熊...
    就這也行閱讀 1,204評論 3 11
  • 其實蜂林,剛開始我沒有意識要記錄下自己在事務(wù)所實習一個月的經(jīng)歷,但是拇泣,不久前在簡書上看到有書友寫下了自己在銀行實習的經(jīng)...
    go孫行者110閱讀 3,866評論 0 16