Flutter注册iOS推送

作者: 划落永恒 | 来源:发表于2018-11-29 11:58 被阅读70次

在进行iOS上开发,发现Flutter创建的项目不走didRegisterForRemoteNotificationsWithDeviceToken,起初以为是没有设置UNUserNotificationCenterDelegate,后来发现AppDelegate是继承于FlutterAppDelegate的,

也就是将原生的UIApplicationDelegate的方法都会被FlutterPlugin拦截,即使我们不实现didRegisterForRemoteNotificationsWithDeviceToken,我觉得应该有两种方法可以实现:第一种是需要重写父类的推送方法。第二种就是在dart文件中监听系统代理,通过通道回调appdelegate来实现

下面是方法一的实现:

override func application(

        _ application: UIApplication,

        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?

        ) -> Bool {

        GeneratedPluginRegistrant.register(with: self);

        //设置、注册远程推送

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

        //

        let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]

        if userInfo != nil {

            BPush.handleNotification(userInfo as? [AnyHashable : Any])

        }

        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)

        pushChannel.setMethodCallHandler { (FlutterMethodCall, FlutterResult) in

            print("pushChannel");

        }

        //绑定channelId到服务器

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

        pushBind.setMethodCallHandler { (FlutterMethodCall, FlutterResult) in

            if(self.channelId.isEmpty){

                print("channelId为空");

                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)

    }

相关文章

  • Flutter注册iOS推送

    在进行iOS上开发,发现Flutter创建的项目不走didRegisterForRemoteNotificatio...

  • iOS 推送适配

    推送注册 iOS 8 以下推送注册UIRemoteNotificationType types = (UIRemo...

  • Flutter踩坑记录-iOS端 fluwx无法收到回调

    工程背景 iOS项目嵌套Flutter工程,iOS调用Flutter项目中的fluwx库进行WX登录 -> 注册 ...

  • ios 设置多个本地推送时间

    iOS的本地推送 1、先注册推送(注iOS8.0之前和iOS8.0之后注册的方法不一样)在AppDelegate....

  • iOS推送注册相关知识点

    兼容iOS7 iOS8的注册方式 判断用户是否注册推送 option 相关 假如用户勾选推送时显示badge和提示...

  • iOS 远程推送

    iOS远程推送主要流程为:注册推送的token,把token上传到服务器->接收到服务器的推送->处理推送。注册t...

  • Flutter iOS端 添加推送扩展 Notification

    Flutter iOS端添加推送扩展 Notification Service Extension 1、打开Flu...

  • iOS 推送总结

    注册推送 收到推送 iOS10以前 iOS10之后 关于推送的声音播放 苹果默认的推送声音就不说了,需要播放自定义...

  • iOS 推送步骤以及原理

    iOS推送 步骤: 1.应用程序向系统APNS注册消息推送。2.iOS系统从APNS Server获取device...

  • 推送

    设备token iOS10之后注册推送,应用在前台也能显示通知栏,但不能在后台调用 iOS8之后注册推送,应用在前...

网友评论

    本文标题:Flutter注册iOS推送

    本文链接:https://www.haomeiwen.com/subject/tzixcqtx.html