人人做人人干-人人做人人看-人人做人人爽国产视-人人做人人爽人人爱-色屋视频-色屋网

歡迎您光臨深圳塔燈網(wǎng)絡(luò)科技有限公司!
電話圖標(biāo) 余先生:13699882642

網(wǎng)站百科

為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴

Flutter注冊(cè)iOS推送

發(fā)表日期:2018-11 文章編輯:小燈 瀏覽次數(shù):3176

在進(jìn)行iOS上開(kāi)發(fā),發(fā)現(xiàn)Flutter創(chuàng)建的項(xiàng)目不走didRegisterForRemoteNotificationsWithDeviceToken,起初以為是沒(méi)有設(shè)置UNUserNotificationCenterDelegate,后來(lái)發(fā)現(xiàn)AppDelegate是繼承于FlutterAppDelegate的,


也就是將原生的UIApplicationDelegate的方法都會(huì)被FlutterAppDelegate攔截,即使我們不實(shí)現(xiàn)didRegisterForRemoteNotificationsWithDeviceToken,我覺(jué)得應(yīng)該有兩種方法可以實(shí)現(xiàn):第一種是需要重寫(xiě)父類(lèi)的推送方法。第二種就是在dart文件中監(jiān)聽(tīng)系統(tǒng)代理,通過(guò)通道回調(diào)appdelegate來(lái)實(shí)現(xiàn),

下面是百度云推送,重寫(xiě)父類(lèi)代理的實(shí)現(xiàn):

在didFinishLaunchingWithOptions launchOptions: 中注冊(cè)原生交互channel


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

? ? ? ? //Flutter Plugin插件注冊(cè)

? ? ? ? GeneratedPluginRegistrant.register(with: self);

? ? ? ? //調(diào)用appdelegate 的 設(shè)置、注冊(cè)遠(yuǎn)程推送

? ? ? ? self.requestAuthorization(application: application);

? ? ? ? //Flutter原生交互通道

? ? ? ? self.BPushChannel();

? ? ? ? //注冊(cè)BPush通道

? ? ? ? BPush.registerChannel(launchOptions, apiKey: BPushKey, pushMode: BPushMode.development, withFirstAction: "打開(kāi)", withSecondAction: "關(guān)閉", withCategory: nil, useBehaviorTextInput: true, isDebug: true);

? ? ? ? //禁用地理位置信息推送

? ? ? ? BPush.disableLbs();

? ? ? ? return super.application(application, didFinishLaunchingWithOptions: launchOptions);

? ? }


? ? //MARK:注冊(cè)遠(yuǎn)程推送通知

? ? func requestAuthorization(application: UIApplication) {

? ? ? ? if #available(iOS 10.0, *) {

? ? ? ? ? ? UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate;

? ? ? ? ? ? UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in

? ? ? ? ? ? ? ? if granted == true {

? ? ? ? ? ? ? ? ? ? DispatchQueue.main.async {

? ? ? ? ? ? ? ? ? ? ? ? application.registerForRemoteNotifications()

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? } else if #available(iOS 8.0, *) {

? ? ? ? ? ? let types:UIUserNotificationType = [.badge , .alert , .sound]

? ? ? ? ? ? let settings:UIUserNotificationSettings = UIUserNotificationSettings(types: types, categories: nil)

? ? ? ? ? ? application.registerUserNotificationSettings(settings)

? ? ? ? } else {

? ? ? ? ? ? let types:UIRemoteNotificationType = [UIRemoteNotificationType.alert, UIRemoteNotificationType.badge, .sound]

? ? ? ? ? ? application.registerForRemoteNotifications(matching: types)

? ? ? ? }

? ? }


? ? //百度推送通道

? ? func BPushChannel() -> Void {

? ? ? ? //獲取系統(tǒng)的跟控制器

? ? ? ? let controller = self.window.rootViewController

? ? ? ? //建立rootViewController和Flutter的通信通道

? ? ? ? let pushChannel = FlutterMethodChannel.init(name: channelNameForPush, binaryMessenger:controller as! FlutterBinaryMessenger)

? ? ? ? //設(shè)置Method回調(diào)?FlutterMethodCall包含了method的Name,ID等信息,?FlutterResult是給Native和Flutter的通信回調(diào)

????????pushChannel.setMethodCallHandler { (FlutterMethodCall, FlutterResult) in

? ? ? ? ? ? print("pushChannel");

? ? ? ? }

? ? ? ? //綁定channelId到服務(wù)器

? ? ? ? let pushBind = FlutterMethodChannel.init(name:channelNameForPushBind, binaryMessenger: controller as! FlutterBinaryMessenger)

? ? ? ? pushBind.setMethodCallHandler { (FlutterMethodCall, FlutterResult) in

? ? ? ? ? ? //FlutterResult();結(jié)果回調(diào),回調(diào)的結(jié)果只能為string類(lèi)型

? ? ? ? ? ? if(self.channelId.isEmpty){

? ? ? ? ? ? ? ? FlutterResult(FlutterMethodNotImplemented);

? ? ? ? ? ? } else{

? ? ? ? ? ? ? ? print("channelId",self.channelId);

? ? ? ? ? ? ? ? let dic : Dictionary<String,String> = ["channelId":self.channelId];

? ? ? ? ? ? ? ? let data = try? JSONSerialization.data(withJSONObject: dic, options: [])

? ? ? ? ? ? ? ? let encodingStr = String(data: data!, encoding: String.Encoding.utf8)!

//將信息傳到Flutter,

? ? ? ? ? ? ? ? FlutterResult(encodingStr);

? ? ? ? ? ? }

? ? ? ? }

? ? }


? ? // 重寫(xiě)遠(yuǎn)程推送通知 注冊(cè)成功

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

? ? ? ? //? 向云推送注冊(cè) device token

? ? ? ? print("deviceToken = %@", deviceToken);

? ? ? ? BPush.registerDeviceToken(deviceToken as Data)

? ? ? ? // 綁定channel.將會(huì)在回調(diào)中看獲得channnelid appid userid 等

? ? ? ? BPush.bindChannel(completeHandler: { (result, error) -> Void in

? ? ? ? ? ? if ((result) != nil){

? ? ? ? ? ? ? ? self.channelId = BPush.getChannelId();

? ? ? ? ? ? ? ? BPush.setTag("MyTag", withCompleteHandler: { (result, error) -> Void in

? ? ? ? ? ? ? ? ? ? if ((result) != nil){

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? })

? ? ? ? ? ? }

? ? ? ? })

? ? ? ? super.application(application, didRegisterForRemoteNotificationsWithDeviceToken:deviceToken)

? ? }


// 重寫(xiě)注冊(cè)失敗

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

? ? ? ? print("deviceToken = %@", error)

? ? ? ? super.application(application, didFailToRegisterForRemoteNotificationsWithError: error)

? ? }


**如果解決了你的問(wèn)題,點(diǎn)個(gè)贊唄!**


本頁(yè)內(nèi)容由塔燈網(wǎng)絡(luò)科技有限公司通過(guò)網(wǎng)絡(luò)收集編輯所得,所有資料僅供用戶(hù)學(xué)習(xí)參考,本站不擁有所有權(quán),如您認(rèn)為本網(wǎng)頁(yè)中由涉嫌抄襲的內(nèi)容,請(qǐng)及時(shí)與我們聯(lián)系,并提供相關(guān)證據(jù),工作人員會(huì)在5工作日內(nèi)聯(lián)系您,一經(jīng)查實(shí),本站立刻刪除侵權(quán)內(nèi)容。本文鏈接:http://www.kwpm.com.cn/17649.html
相關(guān)APP開(kāi)發(fā)
 八年  行業(yè)經(jīng)驗(yàn)

多一份參考,總有益處

聯(lián)系深圳網(wǎng)站公司塔燈網(wǎng)絡(luò),免費(fèi)獲得網(wǎng)站建設(shè)方案及報(bào)價(jià)

咨詢(xún)相關(guān)問(wèn)題或預(yù)約面談,可以通過(guò)以下方式與我們聯(lián)系

業(yè)務(wù)熱線:余經(jīng)理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.    

主站蜘蛛池模板: 天天天天射 | 国产成人综合洲欧美在线 | 免费大片黄在线观看 | 毛片无遮挡高清免费观看 | aⅴ在线免费观看 | 手机亚洲第1页 | 欧美羞羞 | 色天天综合久久久久综合片 | 黄色一级毛片免费 | 九九热亚洲精品综合视频 | 在线视频日韩精品 | 日韩综合图区 | 香蕉视频一区二区 | 国产90后美女露脸在线观看 | 波多野结衣中出在线 | 日本a级特黄三级三级三级 日本a一级片 | 成人毛片免费观看视频在线 | 丁香网五月 | 国产一级特黄aa大片在线 | 国产欧美日韩不卡在线播放在线 | 欧美视频手机在线 | 国产亚洲欧美日本一二三本道 | 天天综合天天看夜夜添狠狠玩 | 黄色三级视频在线观看 | 色综久久天天综合绕视看 | 天天摸夜夜 | 欧美在线观看日韩欧美在线观看 | 波多野结衣中文字幕一区二区三区 | 亚洲一级毛片在线观 | 九九影视理论片在线播放 | 免费在线视频成人 | 人人澡 人人澡 人人看欧美 | 91精品一区二区三区在线观看 | 性欧美xxx 不卡视频 | 1769国产精品一区2区 | 国产欧美日韩精品第一区 | 亚洲视频 中文字幕 | 深夜福利免费 | 天堂va欧美ⅴa亚洲va一国产 | 99视频免费播放 | 久草8|