EMRemindManager.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // EMRemindManager.m
  3. // EMiOSDemo
  4. //
  5. // Created by 杜洁鹏 on 2019/8/21.
  6. // Copyright © 2019 杜洁鹏. All rights reserved.
  7. //
  8. #import "EMRemindManager.h"
  9. #import <AudioToolbox/AudioToolbox.h>
  10. #import <AVFoundation/AVFoundation.h>
  11. #import <UserNotifications/UserNotifications.h>
  12. // 提示音时间间隔
  13. static const CGFloat kDefaultPlaySoundInterval = 3.0;
  14. SystemSoundID soundID = 1007;
  15. @interface EMRemindManager ()
  16. @property (strong, nonatomic) NSDate *lastPlaySoundDate; // 最后一次提醒的时间
  17. @end
  18. @implementation EMRemindManager
  19. + (void)remindMessage:(EMMessage *)aMessage {
  20. [[EMRemindManager shared] remindMessage:aMessage];
  21. }
  22. + (void)updateApplicationIconBadgeNumber:(NSInteger)aBadgeNumber {
  23. [[EMRemindManager shared] updateApplicationIconBadgeNumber:aBadgeNumber];
  24. }
  25. #pragma - mark private
  26. + (EMRemindManager *)shared {
  27. static EMRemindManager *remindManager_;
  28. static dispatch_once_t onceToken;
  29. dispatch_once(&onceToken, ^{
  30. remindManager_ = [[EMRemindManager alloc] init];
  31. });
  32. return remindManager_;
  33. }
  34. - (void)updateApplicationIconBadgeNumber:(NSInteger)aBadgeNumber {
  35. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:aBadgeNumber];
  36. }
  37. - (void)remindMessage:(EMMessage *)aMessage {
  38. if ([aMessage.from isEqualToString:EMClient.sharedClient.currentUsername]) {
  39. return;
  40. }
  41. // 小于最小间隔时间
  42. NSTimeInterval timeInterval = [[NSDate date]
  43. timeIntervalSinceDate:self.lastPlaySoundDate];
  44. if (timeInterval < kDefaultPlaySoundInterval) {
  45. return;
  46. }
  47. // 是否是群免打扰的消息
  48. if (aMessage.chatType != EMChatTypeChat) {
  49. if (![self _needRemind:aMessage.conversationId]) {
  50. return;
  51. }
  52. }
  53. BOOL isBackground = NO;
  54. UIApplicationState state = [[UIApplication sharedApplication] applicationState];
  55. if (state == UIApplicationStateBackground) {
  56. isBackground = YES;
  57. }
  58. // App 是否在后台
  59. if (isBackground) {
  60. [self _localNotification:aMessage
  61. needInfo:EMClient.sharedClient.pushOptions.displayStyle != EMPushDisplayStyleSimpleBanner];
  62. }else {
  63. AudioServicesPlaySystemSound(soundID);
  64. }
  65. }
  66. // 本地通知 needInfo: 是否显示通知详情
  67. - (void)_localNotification:(EMMessage *)message
  68. needInfo:(BOOL)isNeed {
  69. NSString *alertBody = nil;
  70. if (isNeed) {
  71. EMMessageBody *messageBody = message.body;
  72. NSString *messageStr = nil;
  73. switch (messageBody.type) {
  74. case EMMessageBodyTypeText:
  75. {
  76. messageStr = ((EMTextMessageBody *)messageBody).text;
  77. }
  78. break;
  79. case EMMessageBodyTypeImage:
  80. {
  81. messageStr = @"图片";
  82. }
  83. break;
  84. case EMMessageBodyTypeLocation:
  85. {
  86. messageStr = @"位置";
  87. }
  88. break;
  89. case EMMessageBodyTypeVoice:
  90. {
  91. messageStr = @"音频";
  92. }
  93. break;
  94. case EMMessageBodyTypeVideo:{
  95. messageStr = @"视频";
  96. }
  97. break;
  98. case EMMessageBodyTypeFile:{
  99. messageStr = @"文件";
  100. }
  101. break;
  102. default:
  103. break;
  104. }
  105. if (message.chatType == EMChatTypeChat) {
  106. alertBody = [NSString stringWithFormat:@"%@:%@", message.from, messageStr];
  107. }else {
  108. alertBody = [NSString stringWithFormat:@"%@(%@):%@", message.conversationId, message.from, messageStr];
  109. }
  110. }
  111. else{
  112. alertBody = @"您有一条新消息";
  113. }
  114. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:self.lastPlaySoundDate];
  115. BOOL playSound = NO;
  116. if (!self.lastPlaySoundDate || timeInterval >= kDefaultPlaySoundInterval) {
  117. self.lastPlaySoundDate = [NSDate date];
  118. playSound = YES;
  119. }
  120. //发送本地推送
  121. if (NSClassFromString(@"UNUserNotificationCenter")) {
  122. UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];
  123. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  124. if (playSound) {
  125. content.sound = [UNNotificationSound defaultSound];
  126. }
  127. content.body = alertBody;
  128. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:message.messageId content:content trigger:trigger];
  129. [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];
  130. }
  131. else {
  132. UILocalNotification *notification = [[UILocalNotification alloc] init];
  133. notification.fireDate = [NSDate date]; //触发通知的时间
  134. notification.alertBody = alertBody;
  135. notification.alertAction = @"打开";
  136. notification.timeZone = [NSTimeZone defaultTimeZone];
  137. if (playSound) {
  138. notification.soundName = UILocalNotificationDefaultSoundName;
  139. }
  140. //发送通知
  141. [[UIApplication sharedApplication] scheduleLocalNotification:notification];
  142. }
  143. }
  144. - (BOOL)_needRemind:(NSString *)fromChatter
  145. {
  146. BOOL ret = NO;
  147. do {
  148. NSArray *igGroupIds = [[EMClient sharedClient].groupManager getGroupsWithoutPushNotification:nil];
  149. for (NSString *str in igGroupIds) {
  150. if ([str isEqualToString:fromChatter]) {
  151. return NO;
  152. }
  153. }
  154. ret = YES;
  155. } while (0);
  156. return ret;
  157. }
  158. @end