SwiftFlutterPluginMipushPlugin.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import Flutter
  2. import UIKit
  3. public class SwiftFlutterPluginMipushPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,MiPushSDKDelegate{
  4. private var eventSink: FlutterEventSink?
  5. public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  6. self.eventSink = events;
  7. return nil
  8. }
  9. public func onCancel(withArguments arguments: Any?) -> FlutterError?{
  10. self.eventSink = nil
  11. return nil
  12. }
  13. public static func register(with registrar: FlutterPluginRegistrar){
  14. let channel = FlutterMethodChannel(name: "flutter_plugin_mipush", binaryMessenger: registrar.messenger())
  15. let event = FlutterEventChannel(name: "flutter_plugin_mipush_event", binaryMessenger: registrar.messenger())
  16. let instance = SwiftFlutterPluginMipushPlugin()
  17. registrar.addMethodCallDelegate(instance, channel: channel)
  18. registrar.addApplicationDelegate(instance)
  19. event.setStreamHandler(instance)
  20. }
  21. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  22. if (call.method == "init") {
  23. initPush(result: result)
  24. }else if call.method == "setAlias" {
  25. MiPushSDK.setAlias((call.arguments as? [String:String])?["userId"] ?? "")
  26. }else if call.method == "unsetAlias" {
  27. MiPushSDK.unsetAlias((call.arguments as? [String:String])?["userId"] ?? "")
  28. }
  29. }
  30. private func initPush(result: @escaping FlutterResult){
  31. MiPushSDK.registerMiPush(self, type: [], connect: true)
  32. let name = Bundle.main.infoDictionary?["CFBundleIdentifier"]
  33. NSLog("initPush \(String(describing: name))")
  34. result("success")
  35. }
  36. // MARK: UIApplicationDelegate
  37. public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
  38. let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
  39. NSLog("deviceToken = >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \(deviceTokenString)");
  40. MiPushSDK.bindDeviceToken(deviceToken)
  41. }
  42. public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
  43. MiPushSDK.handleReceiveRemoteNotification(userInfo)
  44. }
  45. public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) -> Bool {
  46. MiPushSDK.handleReceiveRemoteNotification(userInfo)
  47. NSLog("应用在后台收到消息");
  48. return true
  49. }
  50. @available(iOS 10.0, *)
  51. public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
  52. let userInfo: [AnyHashable : Any] = notification.request.content.userInfo;
  53. if notification.request.trigger is UNPushNotificationTrigger {
  54. MiPushSDK.handleReceiveRemoteNotification(userInfo)
  55. }
  56. NSLog("应用在前台收到消息")
  57. completionHandler(UNNotificationPresentationOptions.alert)
  58. }
  59. @available(iOS 10.0, *)
  60. public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void){
  61. let userInfo: [AnyHashable : Any] = response.notification.request.content.userInfo;
  62. if response.notification.request.trigger is UNPushNotificationTrigger {
  63. MiPushSDK.handleReceiveRemoteNotification(userInfo)
  64. NSLog("点击通知进入应用 \(userInfo)")
  65. let aps = userInfo["aps"] as? [AnyHashable:Any]
  66. // NSLog("aps = \(aps ?? [:])")
  67. let alert = aps?["alert"] as? [AnyHashable:Any]
  68. // NSLog("alert = \(alert ?? [:])")
  69. if let body = alert?["body"] as? String {
  70. NSLog("body = \(body)")
  71. self.eventSink?(body)
  72. }
  73. }
  74. completionHandler()
  75. }
  76. // MARK: MiPushSDKDelegate
  77. public func miPushRequestSucc(withSelector selector: String!, data: [AnyHashable : Any]!) {
  78. if selector == "bindDeviceToken:" {
  79. let regId = data["regid"]
  80. NSLog("regId = >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \(regId ?? "成功")");
  81. }
  82. MiPushSDK.setAlias("leontest")
  83. }
  84. public func miPushRequestErr(withSelector selector: String!, error: Int32, data: [AnyHashable : Any]!) {
  85. if selector == "bindDeviceToken:" {
  86. let regId = data["regid"]
  87. NSLog("regId = \(regId ?? "失败")")
  88. }
  89. }
  90. // 解析推送数据
  91. public func miPushReceiveNotification(_ data: [AnyHashable : Any]!) {
  92. NSLog("长连接收到的消息。消息格式跟APNs格式一样")
  93. let aps = data["aps"] as? [AnyHashable:Any]
  94. // NSLog("aps = \(aps ?? [:])")
  95. let alert = aps?["alert"] as? [AnyHashable:Any]
  96. // NSLog("alert = \(alert ?? [:])")
  97. if let body = alert?["body"] as? String {
  98. NSLog("body = \(body)")
  99. self.eventSink?(body)
  100. }
  101. }
  102. }