123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import Flutter
- import UIKit
- public class SwiftFlutterPluginMipushPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,MiPushSDKDelegate{
-
- private var eventSink: FlutterEventSink?
-
- public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
- self.eventSink = events;
- return nil
- }
-
- public func onCancel(withArguments arguments: Any?) -> FlutterError?{
- self.eventSink = nil
- return nil
- }
- public static func register(with registrar: FlutterPluginRegistrar){
- let channel = FlutterMethodChannel(name: "flutter_plugin_mipush", binaryMessenger: registrar.messenger())
- let event = FlutterEventChannel(name: "flutter_plugin_mipush_event", binaryMessenger: registrar.messenger())
- let instance = SwiftFlutterPluginMipushPlugin()
- registrar.addMethodCallDelegate(instance, channel: channel)
- registrar.addApplicationDelegate(instance)
- event.setStreamHandler(instance)
-
- }
-
- public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
- if (call.method == "init") {
- initPush(result: result)
- }else if call.method == "setAlias" {
- MiPushSDK.setAlias((call.arguments as? [String:String])?["userId"] ?? "")
- }else if call.method == "unsetAlias" {
- MiPushSDK.unsetAlias((call.arguments as? [String:String])?["userId"] ?? "")
- }
- }
-
- private func initPush(result: @escaping FlutterResult){
- MiPushSDK.registerMiPush(self, type: [], connect: true)
- let name = Bundle.main.infoDictionary?["CFBundleIdentifier"]
- NSLog("initPush \(String(describing: name))")
- result("success")
- }
- // MARK: UIApplicationDelegate
- public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
- let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
- NSLog("deviceToken = >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \(deviceTokenString)");
- MiPushSDK.bindDeviceToken(deviceToken)
- }
- public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
- MiPushSDK.handleReceiveRemoteNotification(userInfo)
- }
- public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) -> Bool {
- MiPushSDK.handleReceiveRemoteNotification(userInfo)
- NSLog("应用在后台收到消息");
- return true
- }
- @available(iOS 10.0, *)
- public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
- let userInfo: [AnyHashable : Any] = notification.request.content.userInfo;
- if notification.request.trigger is UNPushNotificationTrigger {
- MiPushSDK.handleReceiveRemoteNotification(userInfo)
- }
- NSLog("应用在前台收到消息")
- completionHandler(UNNotificationPresentationOptions.alert)
- }
- @available(iOS 10.0, *)
- public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void){
- let userInfo: [AnyHashable : Any] = response.notification.request.content.userInfo;
- if response.notification.request.trigger is UNPushNotificationTrigger {
- MiPushSDK.handleReceiveRemoteNotification(userInfo)
-
- NSLog("点击通知进入应用 \(userInfo)")
-
- let aps = userInfo["aps"] as? [AnyHashable:Any]
-
- // NSLog("aps = \(aps ?? [:])")
- let alert = aps?["alert"] as? [AnyHashable:Any]
- // NSLog("alert = \(alert ?? [:])")
-
- if let body = alert?["body"] as? String {
-
- NSLog("body = \(body)")
-
- self.eventSink?(body)
-
- }
-
- }
- completionHandler()
- }
- // MARK: MiPushSDKDelegate
- public func miPushRequestSucc(withSelector selector: String!, data: [AnyHashable : Any]!) {
- if selector == "bindDeviceToken:" {
- let regId = data["regid"]
- NSLog("regId = >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \(regId ?? "成功")");
- }
- MiPushSDK.setAlias("leontest")
- }
- public func miPushRequestErr(withSelector selector: String!, error: Int32, data: [AnyHashable : Any]!) {
- if selector == "bindDeviceToken:" {
- let regId = data["regid"]
- NSLog("regId = \(regId ?? "失败")")
- }
- }
- // 解析推送数据
- public func miPushReceiveNotification(_ data: [AnyHashable : Any]!) {
-
- NSLog("长连接收到的消息。消息格式跟APNs格式一样")
- let aps = data["aps"] as? [AnyHashable:Any]
-
- // NSLog("aps = \(aps ?? [:])")
- let alert = aps?["alert"] as? [AnyHashable:Any]
- // NSLog("alert = \(alert ?? [:])")
-
- if let body = alert?["body"] as? String {
-
- NSLog("body = \(body)")
-
- self.eventSink?(body)
-
- }
-
- }
- }
|