EMNotificationHelper.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // EMNotificationHelper.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/10.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMNotificationHelper.h"
  9. #import "EMMulticastDelegate.h"
  10. static NSString *kNotifications_Sender = @"sender";
  11. static NSString *kNotifications_Receiver = @"receiver";
  12. static NSString *kNotifications_GroupId = @"groupId";
  13. static NSString *kNotifications_Message = @"message";
  14. static NSString *kNotifications_Time = @"time";
  15. static NSString *kNotifications_Status = @"status";
  16. static NSString *kNotifications_Type = @"type";
  17. static NSString *kNotifications_IsRead = @"isRead";
  18. @implementation EMNotificationModel
  19. - (instancetype)init
  20. {
  21. self = [super init];
  22. if (self) {
  23. self.groupId = @"";
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  28. {
  29. if (self = [super init]) {
  30. self.sender = [aDecoder decodeObjectForKey:kNotifications_Sender];
  31. self.receiver = [aDecoder decodeObjectForKey:kNotifications_Receiver];
  32. self.groupId = [aDecoder decodeObjectForKey:kNotifications_GroupId];
  33. self.message = [aDecoder decodeObjectForKey:kNotifications_Message];
  34. self.time = [aDecoder decodeObjectForKey:kNotifications_Time];
  35. self.status = [aDecoder decodeIntegerForKey:kNotifications_Status];
  36. self.type = [aDecoder decodeIntegerForKey:kNotifications_Type];
  37. self.isRead = [aDecoder decodeBoolForKey:kNotifications_IsRead];
  38. }
  39. return self;
  40. }
  41. - (void)encodeWithCoder:(NSCoder *)aCoder
  42. {
  43. [aCoder encodeObject:self.sender forKey:kNotifications_Sender];
  44. [aCoder encodeObject:self.receiver forKey:kNotifications_Receiver];
  45. [aCoder encodeObject:self.groupId forKey:kNotifications_GroupId];
  46. [aCoder encodeObject:self.message forKey:kNotifications_Message];
  47. [aCoder encodeObject:self.time forKey:kNotifications_Time];
  48. [aCoder encodeInteger:self.status forKey:kNotifications_Status];
  49. [aCoder encodeInteger:self.type forKey:kNotifications_Type];
  50. [aCoder encodeBool:self.isRead forKey:kNotifications_IsRead];
  51. }
  52. @end
  53. static EMNotificationHelper *shared = nil;
  54. @interface EMNotificationHelper()
  55. @property (nonatomic, strong) EMMulticastDelegate<EMNotificationsDelegate> *delegates;
  56. @property (nonatomic, strong) NSDateFormatter *dateFormatter;
  57. @end
  58. @implementation EMNotificationHelper
  59. + (instancetype)shared
  60. {
  61. static dispatch_once_t onceToken;
  62. dispatch_once(&onceToken, ^{
  63. shared = [[EMNotificationHelper alloc] init];
  64. });
  65. return shared;
  66. }
  67. - (instancetype)init
  68. {
  69. self = [super init];
  70. if (self) {
  71. _fileName = [NSString stringWithFormat:@"emdemo_notifications_%@.data", [EMClient sharedClient].currentUsername];
  72. _notificationList = [[NSMutableArray alloc] init];
  73. _isCheckUnreadCount = YES;
  74. _dateFormatter = [[NSDateFormatter alloc] init];
  75. [_dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm"];
  76. _delegates = (EMMulticastDelegate<EMNotificationsDelegate> *)[[EMMulticastDelegate alloc] init];
  77. [[EMClient sharedClient] addMultiDevicesDelegate:self delegateQueue:nil];
  78. [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
  79. [[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];
  80. [self getNotificationsFromLocal];
  81. }
  82. return self;
  83. }
  84. - (void)dealloc
  85. {
  86. [self.delegates removeAllDelegates];
  87. [[EMClient sharedClient] removeMultiDevicesDelegate:self];
  88. [[EMClient sharedClient].contactManager removeDelegate:self];
  89. [[EMClient sharedClient].groupManager removeDelegate:self];
  90. }
  91. #pragma mark - Private
  92. - (void)getNotificationsFromLocal
  93. {
  94. NSString *file = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:self.fileName];
  95. NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
  96. [self.notificationList removeAllObjects];
  97. [self.notificationList addObjectsFromArray:array];
  98. _unreadCount = [self getUnreadCount];
  99. if (self.isCheckUnreadCount) {
  100. [self.delegates didNotificationsUnreadCountUpdate:_unreadCount];
  101. }
  102. }
  103. - (NSInteger)getUnreadCount
  104. {
  105. NSInteger ret = 0;
  106. for (EMNotificationModel *model in self.notificationList) {
  107. if (!model.isRead) {
  108. ++ret;
  109. }
  110. }
  111. return ret;
  112. }
  113. #pragma mark - Public
  114. - (void)addDelegate:(id<EMNotificationsDelegate>)aDelegate
  115. {
  116. [self.delegates addDelegate:aDelegate delegateQueue:dispatch_get_main_queue()];
  117. }
  118. - (void)removeDelegate:(id<EMNotificationsDelegate>)aDelegate
  119. {
  120. [self.delegates removeDelegate:aDelegate];
  121. }
  122. - (void)archive
  123. {
  124. NSString *file = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:self.fileName];
  125. [NSKeyedArchiver archiveRootObject:self.notificationList toFile:file];
  126. }
  127. - (void)markAllAsRead
  128. {
  129. BOOL isArchive = NO;
  130. for (EMNotificationModel *model in self.notificationList) {
  131. if (!model.isRead) {
  132. model.isRead = YES;
  133. isArchive = YES;
  134. }
  135. }
  136. if (isArchive) {
  137. [self archive];
  138. }
  139. if (self.unreadCount != 0) {
  140. _unreadCount = 0;
  141. if (self.isCheckUnreadCount) {
  142. [self.delegates didNotificationsUnreadCountUpdate:0];
  143. }
  144. }
  145. }
  146. - (void)insertModel:(EMNotificationModel *)aModel
  147. {
  148. NSString *time = [self.dateFormatter stringFromDate:[NSDate date]];
  149. for (EMNotificationModel *model in self.notificationList) {
  150. if (model.type == aModel.type && model.status == aModel.status && [model.sender isEqualToString:aModel.sender] && [model.groupId isEqualToString:aModel.groupId]) {
  151. [self.notificationList removeObject:model];
  152. break;
  153. }
  154. }
  155. aModel.time = time;
  156. if ([aModel.message length] == 0) {
  157. if (aModel.type == EMNotificationModelTypeContact) {
  158. aModel.message = @"申请添加您为好友";
  159. } else if (aModel.type == EMNotificationModelTypeGroupInvite) {
  160. aModel.message = [NSString stringWithFormat:@"邀请您加入群组\"%@\"", aModel.groupId];
  161. }
  162. }
  163. if (!self.isCheckUnreadCount) {
  164. aModel.isRead = YES;
  165. } else {
  166. ++_unreadCount;
  167. [self.delegates didNotificationsUnreadCountUpdate:self.unreadCount];
  168. }
  169. [self.notificationList insertObject:aModel atIndex:0];
  170. [self archive];
  171. [self.delegates didNotificationsUpdate];
  172. }
  173. #pragma mark - EMMultiDevicesDelegate
  174. - (void)multiDevicesContactEventDidReceive:(EMMultiDevicesEvent)aEvent
  175. username:(NSString *)aTarget
  176. ext:(NSString *)aExt
  177. {
  178. if (aEvent == EMMultiDevicesEventContactAccept || aEvent == EMMultiDevicesEventContactDecline) {
  179. for (EMNotificationModel *model in self.notificationList) {
  180. if (model.type == EMNotificationModelTypeContact && [model.sender isEqualToString:aTarget]) {
  181. if (!model.isRead && self.unreadCount > 0) {
  182. model.isRead = YES;
  183. --_unreadCount;
  184. if (self.isCheckUnreadCount) {
  185. [self.delegates didNotificationsUnreadCountUpdate:_unreadCount];
  186. }
  187. }
  188. model.status = aEvent == EMMultiDevicesEventContactAccept ? EMNotificationModelStatusAgreed : EMNotificationModelStatusDeclined;
  189. [self archive];
  190. [self.delegates didNotificationsUpdate];
  191. break;
  192. }
  193. }
  194. }
  195. }
  196. - (void)multiDevicesGroupEventDidReceive:(EMMultiDevicesEvent)aEvent
  197. groupId:(NSString *)aGroupId
  198. ext:(id)aExt
  199. {
  200. if (aEvent == EMMultiDevicesEventGroupInviteDecline || aEvent == EMMultiDevicesEventGroupInviteAccept || aEvent == EMMultiDevicesEventGroupApplyAccept || aEvent == EMMultiDevicesEventGroupApplyDecline) {
  201. EMNotificationModelType type = EMNotificationModelTypeGroupInvite;
  202. if (aEvent == EMMultiDevicesEventGroupApplyAccept || aEvent == EMMultiDevicesEventGroupApplyDecline) {
  203. type = EMNotificationModelTypeGroupJoin;
  204. }
  205. EMNotificationModelStatus status = EMNotificationModelStatusAgreed;
  206. if (aEvent == EMMultiDevicesEventGroupInviteDecline || aEvent == EMMultiDevicesEventGroupApplyDecline) {
  207. status = EMNotificationModelStatusDeclined;
  208. }
  209. for (EMNotificationModel *model in self.notificationList) {
  210. if (model.type == EMNotificationModelTypeGroupJoin && [model.groupId isEqualToString:aGroupId]) {
  211. if (!model.isRead && self.unreadCount > 0) {
  212. model.isRead = YES;
  213. --_unreadCount;
  214. if (self.isCheckUnreadCount) {
  215. [self.delegates didNotificationsUnreadCountUpdate:_unreadCount];
  216. }
  217. }
  218. model.status = status;
  219. [self archive];
  220. [self.delegates didNotificationsUpdate];
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. #pragma mark - EMContactManagerDelegate
  227. - (void)friendRequestDidReceiveFromUser:(NSString *)aUsername
  228. message:(NSString *)aMessage
  229. {
  230. if ([aUsername length] == 0) {
  231. return;
  232. }
  233. if ([aMessage length] == 0) {
  234. aMessage = @"申请添加您为好友";
  235. }
  236. EMNotificationModel *model = [[EMNotificationModel alloc] init];
  237. model.sender = aUsername;
  238. model.message = aMessage;
  239. model.type = EMNotificationModelTypeContact;
  240. [self insertModel:model];
  241. }
  242. #pragma mark - EMGroupManagerDelegate
  243. - (void)groupInvitationDidReceive:(NSString *)aGroupId
  244. inviter:(NSString *)aInviter
  245. message:(NSString *)aMessage
  246. {
  247. if ([aGroupId length] == 0 || [aInviter length] == 0) {
  248. return;
  249. }
  250. EMNotificationModel *model = [[EMNotificationModel alloc] init];
  251. model.sender = aInviter;
  252. model.groupId = aGroupId;
  253. model.type = EMNotificationModelTypeGroupInvite;
  254. model.message = aMessage;
  255. [[EMNotificationHelper shared] insertModel:model];
  256. }
  257. - (void)joinGroupRequestDidReceive:(EMGroup *)aGroup
  258. user:(NSString *)aUsername
  259. reason:(NSString *)aReason
  260. {
  261. if ([aGroup.groupId length] == 0 || [aUsername length] == 0) {
  262. return;
  263. }
  264. EMNotificationModel *model = [[EMNotificationModel alloc] init];
  265. model.sender = aUsername;
  266. model.groupId = aGroup.groupId;
  267. model.type = EMNotificationModelTypeGroupJoin;
  268. model.message = [NSString stringWithFormat:@"申请加入群组\"%@\"", aGroup.groupId];;
  269. [[EMNotificationHelper shared] insertModel:model];
  270. }
  271. @end