• <ul id="cgeq2"></ul>
  • 歡迎您光臨深圳塔燈網絡科技有限公司!
    電話圖標 余先生:13699882642

    網站百科

    為您解碼網站建設的點點滴滴

    Flutter注冊iOS推送

    發表日期:2018-11 文章編輯:小燈 瀏覽次數:3183

    在進行iOS上開發,發現Flutter創建的項目不走didRegisterForRemoteNotificationsWithDeviceToken,起初以為是沒有設置UNUserNotificationCenterDelegate,后來發現AppDelegate是繼承于FlutterAppDelegate的,


    也就是將原生的UIApplicationDelegate的方法都會被FlutterAppDelegate攔截,即使我們不實現didRegisterForRemoteNotificationsWithDeviceToken,我覺得應該有兩種方法可以實現:第一種是需要重寫父類的推送方法。第二種就是在dart文件中監聽系統代理,通過通道回調appdelegate來實現,

    下面是百度云推送,重寫父類代理的實現:

    在didFinishLaunchingWithOptions launchOptions: 中注冊原生交互channel


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

    ? ? ? ? //Flutter Plugin插件注冊

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

    ? ? ? ? //調用appdelegate 的 設置、注冊遠程推送

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

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

    ? ? ? ? self.BPushChannel();

    ? ? ? ? //注冊BPush通道

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

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

    ? ? ? ? BPush.disableLbs();

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

    ? ? }


    ? ? //MARK:注冊遠程推送通知

    ? ? 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 {

    ? ? ? ? //獲取系統的跟控制器

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

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

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

    ? ? ? ? //設置Method回調?FlutterMethodCall包含了method的Name,ID等信息,?FlutterResult是給Native和Flutter的通信回調

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

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

    ? ? ? ? }

    ? ? ? ? //綁定channelId到服務器

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

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

    ? ? ? ? ? ? //FlutterResult();結果回調,回調的結果只能為string類型

    ? ? ? ? ? ? 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);

    ? ? ? ? ? ? }

    ? ? ? ? }

    ? ? }


    ? ? // 重寫遠程推送通知 注冊成功

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

    ? ? ? ? //? 向云推送注冊 device token

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

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

    ? ? ? ? // 綁定channel.將會在回調中看獲得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)

    ? ? }


    // 重寫注冊失敗

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

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

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

    ? ? }


    **如果解決了你的問題,點個贊唄!**


    本頁內容由塔燈網絡科技有限公司通過網絡收集編輯所得,所有資料僅供用戶學習參考,本站不擁有所有權,如您認為本網頁中由涉嫌抄襲的內容,請及時與我們聯系,并提供相關證據,工作人員會在5工作日內聯系您,一經查實,本站立刻刪除侵權內容。本文鏈接:http://www.juherenli.com/17649.html
    相關APP開發
     八年  行業經驗

    多一份參考,總有益處

    聯系深圳網站公司塔燈網絡,免費獲得網站建設方案及報價

    咨詢相關問題或預約面談,可以通過以下方式與我們聯系

    業務熱線:余經理:13699882642

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

    国产亚洲精品激情都市| 久久国产亚洲精品| 一区二区三区免费精品视频| 国产2021精品视频免费播放| 久久久国产精品网站| 亚洲国产精品日韩| 狠狠入ady亚洲精品| 99在线精品视频观看免费| 久久精品aⅴ无码中文字字幕| 国产精品偷窥熟女精品视频| 国产精品深爱在线| 中文字幕精品三区无码亚洲| 久久久久99精品成人片直播| 精品久久久久久久| 久久青青草原精品国产不卡| 国产成人综合久久精品| 久久久一本精品99久久精品66直播| 久久久久久亚洲Av无码精品专口| 国内精品手机在线观看视频| 亚洲精品tv久久久久久久久久| 日韩精品久久一区二区三区| 国产精品1024| 国产精品一久久香蕉国产线看| 亚洲综合一区二区精品导航| 久久91精品国产91久久户| 中文乱码精品一区二区三区| 国产精品超碰12396| 视频一区二区精品的福利| 国产精品美女久久久久久久| 国产成人午夜精品一区二区三区| 国产精品自在在线午夜蜜芽tv在线| 91精品国产综合久久香蕉| 2021国产精品久久| 91精品在线国产| 亚洲国产精品无码久久久| 91精品国产91热久久久久福利| 91亚洲精品第一综合不卡播放| 久久九九精品99国产精品| 亚洲麻豆精品果冻传媒| 99视频精品国在线视频艾草| 精品香蕉一区二区三区|