為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(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è)贊唄!**
日期:2018-10 瀏覽次數(shù):7543
日期:2018-12 瀏覽次數(shù):4626
日期:2018-07 瀏覽次數(shù):5138
日期:2018-12 瀏覽次數(shù):4416
日期:2018-09 瀏覽次數(shù):5778
日期:2018-12 瀏覽次數(shù):10197
日期:2018-11 瀏覽次數(shù):5109
日期:2018-07 瀏覽次數(shù):4859
日期:2018-05 瀏覽次數(shù):5118
日期:2018-12 瀏覽次數(shù):4585
日期:2018-10 瀏覽次數(shù):5392
日期:2018-12 瀏覽次數(shù):6463
日期:2018-11 瀏覽次數(shù):4719
日期:2018-08 瀏覽次數(shù):4869
日期:2018-11 瀏覽次數(shù):12964
日期:2018-09 瀏覽次數(shù):5879
日期:2018-12 瀏覽次數(shù):5097
日期:2018-10 瀏覽次數(shù):4442
日期:2018-11 瀏覽次數(shù):4796
日期:2018-12 瀏覽次數(shù):6323
日期:2018-06 瀏覽次數(shù):4266
日期:2018-08 瀏覽次數(shù):5712
日期:2018-10 瀏覽次數(shù):4701
日期:2018-12 瀏覽次數(shù):4819
日期:2018-07 瀏覽次數(shù):4631
日期:2018-12 瀏覽次數(shù):4803
日期:2018-06 瀏覽次數(shù):4637
日期:2018-11 瀏覽次數(shù):4621
日期:2018-12 瀏覽次數(shù):4553
日期:2018-12 瀏覽次數(shù):5532
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.