EaseSDKHelper.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import <UserNotifications/UserNotifications.h>
  13. #import "EaseSDKHelper.h"
  14. #import "EaseConvertToCommonEmoticonsHelper.h"
  15. //@interface EMChatImageOptions : NSObject<IChatImageOptions>
  16. //
  17. //@property (assign, nonatomic) CGFloat compressionQuality;
  18. //
  19. //@end
  20. static EaseSDKHelper *helper = nil;
  21. @implementation EaseSDKHelper
  22. @synthesize isShowingimagePicker = _isShowingimagePicker;
  23. - (instancetype)init
  24. {
  25. self = [super init];
  26. if (self) {
  27. [self commonInit];
  28. }
  29. return self;
  30. }
  31. +(instancetype)shareHelper
  32. {
  33. static dispatch_once_t onceToken;
  34. dispatch_once(&onceToken, ^{
  35. helper = [[EaseSDKHelper alloc] init];
  36. });
  37. return helper;
  38. }
  39. #pragma mark - private
  40. - (void)commonInit
  41. {
  42. }
  43. #pragma mark - app delegate notifications
  44. /** @brief 注册App切入后台和进入前台的通知 */
  45. - (void)_setupAppDelegateNotifications
  46. {
  47. [[NSNotificationCenter defaultCenter] addObserver:self
  48. selector:@selector(appDidEnterBackgroundNotif:)
  49. name:UIApplicationDidEnterBackgroundNotification
  50. object:nil];
  51. [[NSNotificationCenter defaultCenter] addObserver:self
  52. selector:@selector(appWillEnterForeground:)
  53. name:UIApplicationWillEnterForegroundNotification
  54. object:nil];
  55. }
  56. - (void)appDidEnterBackgroundNotif:(NSNotification*)notif
  57. {
  58. [[EMClient sharedClient] applicationDidEnterBackground:notif.object];
  59. }
  60. - (void)appWillEnterForeground:(NSNotification*)notif
  61. {
  62. [[EMClient sharedClient] applicationWillEnterForeground:notif.object];
  63. }
  64. #pragma mark - register apns
  65. /** @brief 注册远程通知 */
  66. - (void)_registerRemoteNotification
  67. {
  68. UIApplication *application = [UIApplication sharedApplication];
  69. application.applicationIconBadgeNumber = 0;
  70. if (NSClassFromString(@"UNUserNotificationCenter")) {
  71. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError *error) {
  72. if (granted) {
  73. #if !TARGET_IPHONE_SIMULATOR
  74. [application registerForRemoteNotifications];
  75. #endif
  76. }
  77. }];
  78. return;
  79. }
  80. if([application respondsToSelector:@selector(registerUserNotificationSettings:)])
  81. {
  82. UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
  83. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
  84. [application registerUserNotificationSettings:settings];
  85. }
  86. #if !TARGET_IPHONE_SIMULATOR
  87. if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
  88. [application registerForRemoteNotifications];
  89. }else{
  90. UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
  91. UIRemoteNotificationTypeSound |
  92. UIRemoteNotificationTypeAlert;
  93. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
  94. }
  95. #endif
  96. }
  97. #pragma mark - init Hyphenate
  98. - (void)hyphenateApplication:(UIApplication *)application
  99. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  100. appkey:(NSString *)appkey
  101. apnsCertName:(NSString *)apnsCertName
  102. otherConfig:(NSDictionary *)otherConfig
  103. {
  104. [self _setupAppDelegateNotifications];
  105. [self _registerRemoteNotification];
  106. EMOptions *options = [EMOptions optionsWithAppkey:appkey];
  107. options.apnsCertName = apnsCertName;
  108. options.isAutoAcceptGroupInvitation = NO;
  109. if ([otherConfig objectForKey:kSDKConfigEnableConsoleLogger]) {
  110. options.enableConsoleLog = YES;
  111. }
  112. BOOL isHttpsOnly = NO;
  113. if ([otherConfig objectForKey:@"httpsOnly"]) {
  114. isHttpsOnly = [[otherConfig objectForKey:@"httpsOnly"] boolValue];
  115. }
  116. options.usingHttpsOnly = isHttpsOnly;
  117. BOOL sandBox = [otherConfig objectForKey:@"easeSandBox"] && [[otherConfig objectForKey:@"easeSandBox"] boolValue];
  118. if (!sandBox) {
  119. [[EMClient sharedClient] initializeSDKWithOptions:options];
  120. }
  121. }
  122. - (void)hyphenateApplication:(UIApplication *)application
  123. didReceiveRemoteNotification:(NSDictionary *)userInfo
  124. {
  125. [[EMClient sharedClient] application:application didReceiveRemoteNotification:userInfo];
  126. }
  127. - (void)dealloc
  128. {
  129. [[NSNotificationCenter defaultCenter] removeObserver:self];
  130. }
  131. #pragma mark - send message
  132. + (EMMessage *)sendTextMessage:(NSString *)text
  133. to:(NSString *)toUser
  134. messageType:(EMChatType)messageType
  135. messageExt:(NSDictionary *)messageExt
  136. {
  137. NSString *willSendText = [EaseConvertToCommonEmoticonsHelper convertToCommonEmoticons:text];
  138. EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:willSendText];
  139. NSString *from = [[EMClient sharedClient] currentUsername];
  140. EMMessage *message = [[EMMessage alloc] initWithConversationID:toUser from:from to:toUser body:body ext:messageExt];
  141. message.chatType = messageType;
  142. return message;
  143. }
  144. + (EMMessage *)sendCmdMessage:(NSString *)action
  145. to:(NSString *)to
  146. messageType:(EMChatType)messageType
  147. messageExt:(NSDictionary *)messageExt
  148. cmdParams:(NSArray *)params
  149. {
  150. EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:action];
  151. if (params) {
  152. body.params = params;
  153. }
  154. NSString *from = [[EMClient sharedClient] currentUsername];
  155. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  156. message.chatType = messageType;
  157. return message;
  158. }
  159. + (EMMessage *)sendLocationMessageWithLatitude:(double)latitude
  160. longitude:(double)longitude
  161. address:(NSString *)address
  162. to:(NSString *)to
  163. messageType:(EMChatType)messageType
  164. messageExt:(NSDictionary *)messageExt
  165. {
  166. EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:latitude longitude:longitude address:address];
  167. NSString *from = [[EMClient sharedClient] currentUsername];
  168. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  169. message.chatType = messageType;
  170. return message;
  171. }
  172. + (EMMessage *)sendImageMessageWithImageData:(NSData *)imageData
  173. to:(NSString *)to
  174. messageType:(EMChatType)messageType
  175. messageExt:(NSDictionary *)messageExt
  176. {
  177. EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:imageData displayName:@"image"];
  178. NSString *from = [[EMClient sharedClient] currentUsername];
  179. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  180. message.chatType = messageType;
  181. return message;
  182. }
  183. + (EMMessage *)sendImageMessageWithImage:(UIImage *)image
  184. to:(NSString *)to
  185. messageType:(EMChatType)messageType
  186. messageExt:(NSDictionary *)messageExt
  187. {
  188. NSData *data = UIImageJPEGRepresentation(image, 1);
  189. return [self sendImageMessageWithImageData:data to:to messageType:messageType messageExt:messageExt];
  190. }
  191. + (EMMessage *)sendVoiceMessageWithLocalPath:(NSString *)localPath
  192. duration:(NSInteger)duration
  193. to:(NSString *)to
  194. messageType:(EMChatType)messageType
  195. messageExt:(NSDictionary *)messageExt
  196. {
  197. EMVoiceMessageBody *body = [[EMVoiceMessageBody alloc] initWithLocalPath:localPath displayName:@"audio"];
  198. body.duration = (int)duration;
  199. NSString *from = [[EMClient sharedClient] currentUsername];
  200. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  201. message.chatType = messageType;
  202. return message;
  203. }
  204. + (EMMessage *)sendVideoMessageWithURL:(NSURL *)url
  205. to:(NSString *)to
  206. messageType:(EMChatType)messageType
  207. messageExt:(NSDictionary *)messageExt
  208. {
  209. EMVideoMessageBody *body = [[EMVideoMessageBody alloc] initWithLocalPath:[url path] displayName:@"video.mp4"];
  210. NSString *from = [[EMClient sharedClient] currentUsername];
  211. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  212. message.chatType = messageType;
  213. return message;
  214. }
  215. @end