SwiftBroadcastPlugin.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Flutter
  2. import UIKit
  3. import CoreFoundation
  4. public class SwiftBroadcastPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
  5. public static func register(with registrar: FlutterPluginRegistrar) {
  6. let channel = FlutterMethodChannel(name: "broadcast", binaryMessenger: registrar.messenger())
  7. let instance = SwiftBroadcastPlugin()
  8. registrar.addMethodCallDelegate(instance, channel: channel)
  9. let event = FlutterEventChannel(name: "broadcast_event", binaryMessenger: registrar.messenger())
  10. event.setStreamHandler(instance)
  11. //发送游戏返回前台的通知
  12. let observer = UnsafeRawPointer(Unmanaged.passUnretained(instance).toOpaque())
  13. CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
  14. observer,
  15. { (_, observer, name, _, _) -> Void in
  16. if let observer = observer{
  17. let mySelf = Unmanaged<SwiftBroadcastPlugin>.fromOpaque(observer).takeUnretainedValue()
  18. mySelf.gameEnterForeground()
  19. }
  20. },
  21. "GAME_ENTERFOREGROUND" as CFString,
  22. nil,
  23. .deliverImmediately)
  24. }
  25. public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
  26. sink = events
  27. return nil
  28. }
  29. public func onCancel(withArguments arguments: Any?) -> FlutterError? {
  30. sink = nil
  31. return nil
  32. }
  33. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  34. switch call.method {
  35. case "broadcast":
  36. if let arguments = call.arguments as? [String: Any]{
  37. let action = arguments["action"] as? String
  38. print("call action \(action)")
  39. switch action {
  40. case "SHOE.SDK.BLUE_DISCONNECT":
  41. postGroupCFNotificaiton()
  42. case "SHOE.SDK.GAME_INVITE":
  43. if let args = arguments["args"] as? [String: Any]{
  44. print("call args \(args)")
  45. sendInviteNotificaiton(msg: dicValueString(dic: args))
  46. }
  47. default:
  48. result(true)
  49. }
  50. }
  51. default:
  52. result(true)
  53. }
  54. result(true)
  55. }
  56. func postGroupCFNotificaiton(){
  57. let notificaitonName = CFNotificationName.init("DISCONNECT_BLE" as CFString)
  58. let notification = CFNotificationCenterGetDarwinNotifyCenter();
  59. CFNotificationCenterPostNotification(notification, notificaitonName, nil, nil, true)
  60. }
  61. //发送好友邀请广播
  62. func sendInviteNotificaiton(msg: String?){
  63. //将邀请信息加入group沙盒 ==>> 开发者帐号已设置好的groupID : group.com.Oujia.AppAndGame
  64. let userDefault = UserDefaults(suiteName: "group.com.Oujia.AppAndGame")
  65. userDefault?.set(msg, forKey: "invite")
  66. userDefault?.synchronize()
  67. //之后发送通知
  68. let notificaitonName = "INVITE_NOTIFICATION"
  69. let strRef = notificaitonName as CFString
  70. let notification = CFNotificationCenterGetDarwinNotifyCenter()
  71. CFNotificationCenterPostNotification(notification, CFNotificationName(strRef), nil, nil, true)
  72. }
  73. func dicValueString(dic:[String:Any]) ->String?{
  74. let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
  75. let str = String(data:data!, encoding: String.Encoding.utf8)
  76. return str
  77. }
  78. func gameEnterForeground(){
  79. sink?()
  80. }
  81. }