SKPushMessage.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // SKPushMessage.m
  3. // Overseas Watch
  4. //
  5. // Created by 刘振兴 on 2024/1/8.
  6. // Copyright © 2024 BaH Cy. All rights reserved.
  7. //
  8. #import "SKPushMessage.h"
  9. #import "PopupView.h"
  10. #import "SKWebViewViewController.h"
  11. #import "SKWebSocket+queue.h"
  12. @implementation SKPushMessage
  13. + (void)didReceiveMessage:(NSString*)message {
  14. SocketModel* socketModel = [SocketModel mj_objectWithKeyValues:message];
  15. if (!socketModel)
  16. return;
  17. if (socketModel.msgType == 2) {
  18. NSDictionary* dic = socketModel.content;
  19. [[SKWebSocket share] sendAckId:[dic[@"ackId"] integerValue]];
  20. }
  21. switch (socketModel.msgType) {
  22. case -1: //服务器返回错误
  23. break;
  24. case 1: //Ping/Pong
  25. [[SKWebSocket share] removeQueueFromSocketModel:socketModel status:-1];
  26. break;
  27. case 20:
  28. case 21:
  29. case 22:
  30. case 23:
  31. case 24:
  32. //手表发的新语聊消息
  33. [self receiveChatMessages];
  34. break;
  35. case 31: { //定位点位变动消息,手表上报完成位置后会通过推送的方式下发位置到App
  36. SocketCoordinatesModel* model = [SocketCoordinatesModel mj_objectWithKeyValues:socketModel.content];
  37. [self receiveLocation:model];
  38. break;
  39. }
  40. case 30: //设备绑定状态变更消息,设备绑定状态变更的消息,一般由 App 发起绑定或解绑设备时出现
  41. case 32: //添加好友加过通知消息,两只手表互相通过好友 Code 加上好友后,相加的两人会收到这条消息
  42. case 33: //联系人变更通知消息,当小孩联系人数据变动时,这条消息会发送给手表设备和 App
  43. case 34: //闹钟变更通知消息,当小孩闹钟消息数据变动时,这条消息只会发送给手表设备
  44. case 35: //上课禁用变更通知消息,当小孩的上课禁用数据变动时,这条消息只会发送给手表设备
  45. case 36: //手表开机通知消息,手表开机时,这条消息会推中给 App
  46. case 37: //手表关机通知消息,手表关机时,这条消息会推中给 App
  47. case 38: //拒接陌生人变动通知消息,当小孩的拒接陌生人了开关数据变动时,这条消息只会发送给手表设备
  48. case 39: //消息中心变动通知消息,当监护人消息中心数据变动时,这条消息只会发送给 App
  49. [DataManager shared].isNewNotification = YES;
  50. for (MutableDelegateObject* object in [DataManager shared].delegateObjects) {
  51. if (object.delegate && [object.delegate respondsToSelector:@selector(onReceiveNewNotification:)]) {
  52. [object.delegate onReceiveNewNotification:YES];
  53. }
  54. }
  55. break;
  56. case 40: {//视频来电通知消息
  57. [[VideoManager shared] actionFromSocket:socketModel dial:1];
  58. break;
  59. }
  60. case 41: {//视频挂断通知消息
  61. [[VideoManager shared] actionFromSocket:socketModel dial:0];
  62. break;
  63. }
  64. case 50:
  65. //请求手表上报点位,App 通过 Ask 接口调用,发送通知消息到手表执行定位逻辑
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. + (void)postSocketStatusChange {
  72. for (MutableDelegateObject* object in [DataManager shared].delegateObjects) {
  73. if (object.delegate && [object.delegate respondsToSelector:@selector(onSocketStatusChange)]) {
  74. [object.delegate onSocketStatusChange];
  75. }
  76. }
  77. }
  78. + (void)postChatMessage:(SessionMessageModel*)model {
  79. for (MutableDelegateObject* object in [DataManager shared].delegateObjects) {
  80. if (object.delegate && [object.delegate respondsToSelector:@selector(onReceiveUpdateChat:)]) {
  81. [object.delegate onReceiveUpdateChat:model];
  82. }
  83. }
  84. }
  85. + (void)receiveChatMessages {
  86. NSInteger state = [UIApplication sharedApplication].applicationState;
  87. if (state == UIApplicationStateInactive) {
  88. //TODO:跳转到tabbar语聊页
  89. if ([[DataManager shared] getChildCount] > 0) {
  90. UIViewController* controller = [UIApplication sharedApplication].keyWindow.rootViewController;
  91. UITabBarController* tabbar = (UITabBarController*)controller;
  92. if (tabbar) {
  93. [tabbar setSelectedIndex:1];
  94. }
  95. } else {
  96. [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"TABBAR_SELECT_CHAT"];
  97. [[NSUserDefaults standardUserDefaults] synchronize];
  98. }
  99. } else {
  100. [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshChatCountFromAppDelegate" object:@{}];
  101. }
  102. for (MutableDelegateObject* object in [DataManager shared].delegateObjects) {
  103. if (object.delegate && [object.delegate respondsToSelector:@selector(onReceiveNewChat)]) {
  104. [object.delegate onReceiveNewChat];
  105. }
  106. }
  107. }
  108. + (void)receiveLocation:(SocketCoordinatesModel*)model {
  109. for (ChildModel* childModel in [DataManager shared].mChildList) {
  110. if ([childModel.cid isEqualToString:model.cid]) {
  111. childModel.lastLocation = model;
  112. childModel.lastLocation.latitude = model.lat;
  113. childModel.lastLocation.longitude = model.lon;
  114. }
  115. }
  116. for (MutableDelegateObject* object in [DataManager shared].delegateObjects) {
  117. if (object.delegate && [object.delegate respondsToSelector:@selector(onReceiveLocation:)]) {
  118. [object.delegate onReceiveLocation:model];
  119. }
  120. }
  121. }
  122. + (void)receiveWeather:(NSDictionary*)pushDic {
  123. //天气预警推送
  124. NSData *briefData = [pushDic[@"brief"] dataUsingEncoding:NSUTF8StringEncoding];
  125. NSMutableArray* array = [NSJSONSerialization JSONObjectWithData:briefData options:0 error:nil];
  126. NSMutableDictionary* data = [[NSMutableDictionary alloc] initWithDictionary:array[0]];
  127. NSMutableDictionary *dic = [NSMutableDictionary new];
  128. dic[@"WarnLevel"] = data[@"DefLevel"];
  129. dic[@"WarnType"] = data[@"DefCategory"];
  130. dic[@"WarnDetail"] = data[@"Content"];
  131. dic[@"WarnSummary"] = [NSString stringWithFormat:@"%@%@%@%@%@", data[@"Province"], @"气象局发布", data[@"Category"], data[@"Level"], NSLocalizedString(@"WWD.Warn", nil)];
  132. PopupView *view = [EUtil getBundleView:@"WeatherWarnView"];
  133. [view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  134. view.mViewController = nil;
  135. view.mPushDic = dic;
  136. [[UIApplication sharedApplication].keyWindow addSubview:view];
  137. }
  138. + (void)receiveMagic:(NSDictionary*)pushDic {
  139. //魔法学院推送
  140. NSData *briefData = [pushDic[@"brief"] dataUsingEncoding:NSUTF8StringEncoding];
  141. NSMutableDictionary* data = [NSJSONSerialization JSONObjectWithData:briefData options:0 error:nil];
  142. ChildModel* mode = [[DataManager shared]getChild:pushDic[@"cid"]];
  143. NSMutableDictionary *dic = [NSMutableDictionary new];
  144. dic[@"MagicFinish"] = data[@"Total"];
  145. dic[@"MagicRight"] = data[@"Right"];
  146. NSString* typeKey = [NSString stringWithFormat:@"%@.%@",@"MA.Subject", data[@"Type"]];
  147. NSString* levelKey = [NSString stringWithFormat:@"%@.%@.%@",@"MA.Subject", data[@"Type"], data[@"Level"]];
  148. dic[@"MagicContent"] = [NSString stringWithFormat:@"%@%@%@%@%@", mode.name, NSLocalizedString(@"MA.Temp.1", nil), NSLocalizedString(typeKey, nil), NSLocalizedString(levelKey, nil), NSLocalizedString(@"MA.Temp.2", nil)];
  149. PopupView *view = [EUtil getBundleView:@"MagicReportView"];
  150. [view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  151. view.mViewController = nil;
  152. view.mPushDic = dic;
  153. [[UIApplication sharedApplication].keyWindow addSubview:view];
  154. }
  155. + (void)receiveKidoMessage:(NSDictionary*)pushDic {
  156. //富文本推送
  157. BOOL isFinishLaunching = YES;
  158. if (isFinishLaunching) {
  159. //如果是刚启动,直接进入web
  160. NSString* url = pushDic[@"url"];
  161. if (url != nil && ![url isEqualToString:@""]) {
  162. NSString *title = pushDic[@"title"];
  163. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  164. SKWebViewViewController *webVc = [storyboard instantiateViewControllerWithIdentifier:@"WebViewVC"];
  165. webVc.title = title;
  166. webVc.urlToOpen = url;
  167. webVc.mIsShare = YES;
  168. webVc.mIsOpenApp = YES;
  169. webVc.mType = 16;
  170. webVc.mId = [pushDic[@"id"] integerValue];
  171. [EUtil pushViewController:webVc animated:YES];
  172. }
  173. } else {
  174. //如果App在运行中收到推送,弹框提示,再进入web
  175. [[NSNotificationCenter defaultCenter] postNotificationName:@"reciveKidoMessage" object:pushDic];
  176. }
  177. }
  178. + (void)receiveHighTemperature:(NSDictionary*)pushDic {
  179. //高温预警推送
  180. ChildModel* mode = [[DataManager shared]getChild:pushDic[@"cid"]];
  181. NSMutableDictionary *dic = [NSMutableDictionary new];
  182. dic[@"Content"] = [NSString stringWithFormat:@"%@%@", mode.name, NSLocalizedString(@"HT.Content", nil)];
  183. PopupView *view = [EUtil getBundleView:@"HighTemperatureView"];
  184. [view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  185. view.mViewController = nil;
  186. view.mPushDic = dic;
  187. [[UIApplication sharedApplication].keyWindow addSubview:view];
  188. }
  189. + (void)testPush {
  190. int i = 1;
  191. if (i == 0) {
  192. //天气预警推送
  193. NSMutableDictionary *dic = [NSMutableDictionary new];
  194. dic[@"WarnLevel"] = @(3);
  195. dic[@"WarnType"] = @(2);
  196. dic[@"WarnDetail"] = @"hahhahhaa";
  197. dic[@"WarnSummary"] = @"hehehheheh";
  198. PopupView *view = [EUtil getBundleView:@"WeatherWarnView"];
  199. [view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  200. view.mViewController = nil;
  201. view.mPushDic = dic;
  202. //view.layer.zPosition = INT8_MAX;
  203. [[UIApplication sharedApplication].keyWindow addSubview:view];
  204. }
  205. else if (i == 1) {
  206. NSMutableDictionary *dic = [NSMutableDictionary new];
  207. dic[@"MagicContent"] = @"测试数学";
  208. dic[@"MagicFinish"] = @"15";
  209. dic[@"MagicRight"] = @"12";
  210. PopupView *view = [EUtil getBundleView:@"MagicReportView"];
  211. [view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  212. view.mViewController = nil;
  213. view.mPushDic = dic;
  214. //view.layer.zPosition = INT8_MAX;
  215. [[UIApplication sharedApplication].keyWindow addSubview:view];
  216. }
  217. }
  218. @end