發表日期: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)
? ? }
**如果解決了你的問題,點個贊唄!**
日期:2018-10 瀏覽次數:7557
日期:2018-12 瀏覽次數:4643
日期:2018-07 瀏覽次數:5156
日期:2018-12 瀏覽次數:4431
日期:2018-09 瀏覽次數:5795
日期:2018-12 瀏覽次數:10216
日期:2018-11 瀏覽次數:5128
日期:2018-07 瀏覽次數:4870
日期:2018-05 瀏覽次數:5131
日期:2018-12 瀏覽次數:4600
日期:2018-10 瀏覽次數:5407
日期:2018-12 瀏覽次數:6475
日期:2018-11 瀏覽次數:4727
日期:2018-08 瀏覽次數:4885
日期:2018-11 瀏覽次數:13037
日期:2018-09 瀏覽次數:5896
日期:2018-12 瀏覽次數:5110
日期:2018-10 瀏覽次數:4456
日期:2018-11 瀏覽次數:4814
日期:2018-12 瀏覽次數:6336
日期:2018-06 瀏覽次數:4278
日期:2018-08 瀏覽次數:5724
日期:2018-10 瀏覽次數:4712
日期:2018-12 瀏覽次數:4835
日期:2018-07 瀏覽次數:4647
日期:2018-12 瀏覽次數:4816
日期:2018-06 瀏覽次數:4649
日期:2018-11 瀏覽次數:4631
日期:2018-12 瀏覽次數:4571
日期:2018-12 瀏覽次數:5548
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.