12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import Flutter
- import UIKit
- import CoreFoundation
- public class SwiftBroadcastPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
- public static func register(with registrar: FlutterPluginRegistrar) {
- let channel = FlutterMethodChannel(name: "broadcast", binaryMessenger: registrar.messenger())
- let instance = SwiftBroadcastPlugin()
- registrar.addMethodCallDelegate(instance, channel: channel)
- let event = FlutterEventChannel(name: "broadcast_event", binaryMessenger: registrar.messenger())
- event.setStreamHandler(instance)
- //发送游戏返回前台的通知
- let observer = UnsafeRawPointer(Unmanaged.passUnretained(instance).toOpaque())
- CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
- observer,
- { (_, observer, name, _, _) -> Void in
- if let observer = observer{
- let mySelf = Unmanaged<SwiftBroadcastPlugin>.fromOpaque(observer).takeUnretainedValue()
- mySelf.gameEnterForeground()
- }
- },
- "GAME_ENTERFOREGROUND" as CFString,
- nil,
- .deliverImmediately)
- }
- public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
- sink = events
- return nil
- }
- public func onCancel(withArguments arguments: Any?) -> FlutterError? {
- sink = nil
- return nil
- }
- public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
- switch call.method {
- case "broadcast":
-
- if let arguments = call.arguments as? [String: Any]{
- let action = arguments["action"] as? String
- print("call action \(action)")
- switch action {
- case "SHOE.SDK.BLUE_DISCONNECT":
- postGroupCFNotificaiton()
- case "SHOE.SDK.GAME_INVITE":
- if let args = arguments["args"] as? [String: Any]{
- print("call args \(args)")
- sendInviteNotificaiton(msg: dicValueString(dic: args))
- }
- default:
- result(true)
- }
- }
-
- default:
- result(true)
- }
- result(true)
- }
- func postGroupCFNotificaiton(){
- let notificaitonName = CFNotificationName.init("DISCONNECT_BLE" as CFString)
- let notification = CFNotificationCenterGetDarwinNotifyCenter();
- CFNotificationCenterPostNotification(notification, notificaitonName, nil, nil, true)
- }
- //发送好友邀请广播
- func sendInviteNotificaiton(msg: String?){
- //将邀请信息加入group沙盒 ==>> 开发者帐号已设置好的groupID : group.com.Oujia.AppAndGame
- let userDefault = UserDefaults(suiteName: "group.com.Oujia.AppAndGame")
- userDefault?.set(msg, forKey: "invite")
- userDefault?.synchronize()
- //之后发送通知
- let notificaitonName = "INVITE_NOTIFICATION"
- let strRef = notificaitonName as CFString
- let notification = CFNotificationCenterGetDarwinNotifyCenter()
- CFNotificationCenterPostNotification(notification, CFNotificationName(strRef), nil, nil, true)
- }
-
- func dicValueString(dic:[String:Any]) ->String?{
- let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
- let str = String(data:data!, encoding: String.Encoding.utf8)
- return str
- }
- func gameEnterForeground(){
- sink?()
- }
- }
|