EaseSDKHelper.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. //@interface EMChatImageOptions : NSObject<IChatImageOptions>
  15. //
  16. //@property (assign, nonatomic) CGFloat compressionQuality;
  17. //
  18. //@end
  19. static EaseSDKHelper *helper = nil;
  20. @implementation EaseSDKHelper
  21. @synthesize isShowingimagePicker = _isShowingimagePicker;
  22. - (instancetype)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. [self commonInit];
  27. }
  28. return self;
  29. }
  30. +(instancetype)shareHelper
  31. {
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. helper = [[EaseSDKHelper alloc] init];
  35. });
  36. return helper;
  37. }
  38. #pragma mark - private
  39. - (void)commonInit
  40. {
  41. }
  42. #pragma mark - app delegate notifications
  43. /** @brief 注册App切入后台和进入前台的通知 */
  44. - (void)_setupAppDelegateNotifications
  45. {
  46. [[NSNotificationCenter defaultCenter] addObserver:self
  47. selector:@selector(appDidEnterBackgroundNotif:)
  48. name:UIApplicationDidEnterBackgroundNotification
  49. object:nil];
  50. [[NSNotificationCenter defaultCenter] addObserver:self
  51. selector:@selector(appWillEnterForeground:)
  52. name:UIApplicationWillEnterForegroundNotification
  53. object:nil];
  54. }
  55. - (void)appDidEnterBackgroundNotif:(NSNotification*)notif
  56. {
  57. [[EMClient sharedClient] applicationDidEnterBackground:notif.object];
  58. }
  59. - (void)appWillEnterForeground:(NSNotification*)notif
  60. {
  61. [[EMClient sharedClient] applicationWillEnterForeground:notif.object];
  62. }
  63. #pragma mark - register apns
  64. /** @brief 注册远程通知 */
  65. - (void)_registerRemoteNotification
  66. {
  67. UIApplication *application = [UIApplication sharedApplication];
  68. application.applicationIconBadgeNumber = 0;
  69. if (NSClassFromString(@"UNUserNotificationCenter")) {
  70. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError *error) {
  71. if (granted) {
  72. #if !TARGET_IPHONE_SIMULATOR
  73. dispatch_sync(dispatch_get_main_queue(), ^{
  74. [application registerForRemoteNotifications];
  75. });
  76. #endif
  77. }
  78. }];
  79. return;
  80. }
  81. if([application respondsToSelector:@selector(registerUserNotificationSettings:)])
  82. {
  83. UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
  84. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
  85. [application registerUserNotificationSettings:settings];
  86. }
  87. #if !TARGET_IPHONE_SIMULATOR
  88. if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
  89. [application registerForRemoteNotifications];
  90. }else{
  91. UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
  92. UIRemoteNotificationTypeSound |
  93. UIRemoteNotificationTypeAlert;
  94. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
  95. }
  96. #endif
  97. }
  98. #pragma mark - init Hyphenate
  99. - (void)hyphenateApplication:(UIApplication *)application
  100. appkey:(NSString *)appkey
  101. apnsCertName:(NSString *)apnsCertName
  102. {
  103. [self _setupAppDelegateNotifications];
  104. [self _registerRemoteNotification];
  105. EMOptions *options = [EMOptions optionsWithAppkey:appkey];
  106. options.apnsCertName = apnsCertName;
  107. options.isAutoAcceptGroupInvitation = YES;
  108. options.enableConsoleLog = NO;
  109. options.usingHttpsOnly = NO;
  110. [[EMClient sharedClient] initializeSDKWithOptions:options];
  111. }
  112. - (void)hyphenateApplication:(UIApplication *)application
  113. didReceiveRemoteNotification:(NSDictionary *)userInfo
  114. {
  115. [[EMClient sharedClient] application:application didReceiveRemoteNotification:userInfo];
  116. }
  117. // 将得到的deviceToken传给SDK
  118. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
  119. [[EMClient sharedClient] bindDeviceToken:deviceToken];
  120. }
  121. - (void)dealloc
  122. {
  123. [[NSNotificationCenter defaultCenter] removeObserver:self];
  124. }
  125. #pragma mark - send message
  126. + (EMMessage *)sendTextMessage:(NSString *)text
  127. to:(NSString *)toUser
  128. messageType:(EMChatType)messageType
  129. messageExt:(NSDictionary *)messageExt
  130. {
  131. EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:text];
  132. NSString *from = [[EMClient sharedClient] currentUsername];
  133. EMMessage *message = [[EMMessage alloc] initWithConversationID:toUser from:from to:toUser body:body ext:messageExt];
  134. message.chatType = messageType;
  135. return message;
  136. }
  137. + (EMMessage *)sendCmdMessage:(NSString *)action
  138. to:(NSString *)to
  139. messageType:(EMChatType)messageType
  140. messageExt:(NSDictionary *)messageExt
  141. cmdParams:(NSArray *)params
  142. {
  143. EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:action];
  144. if (params) {
  145. body.params = params;
  146. }
  147. NSString *from = [[EMClient sharedClient] currentUsername];
  148. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  149. message.chatType = messageType;
  150. return message;
  151. }
  152. + (EMMessage *)sendLocationMessageWithLatitude:(double)latitude
  153. longitude:(double)longitude
  154. address:(NSString *)address
  155. to:(NSString *)to
  156. messageType:(EMChatType)messageType
  157. messageExt:(NSDictionary *)messageExt
  158. {
  159. EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:latitude longitude:longitude address:address];
  160. NSString *from = [[EMClient sharedClient] currentUsername];
  161. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  162. message.chatType = messageType;
  163. return message;
  164. }
  165. + (EMMessage *)sendImageMessageWithImageData:(NSData *)imageData
  166. to:(NSString *)to
  167. messageType:(EMChatType)messageType
  168. messageExt:(NSDictionary *)messageExt
  169. {
  170. EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:imageData displayName:@"image"];
  171. NSString *from = [[EMClient sharedClient] currentUsername];
  172. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  173. message.chatType = messageType;
  174. return message;
  175. }
  176. + (EMMessage *)sendImageMessageWithImage:(UIImage *)image
  177. to:(NSString *)to
  178. messageType:(EMChatType)messageType
  179. messageExt:(NSDictionary *)messageExt
  180. {
  181. NSData *data = UIImageJPEGRepresentation(image, 1);
  182. return [self sendImageMessageWithImageData:data to:to messageType:messageType messageExt:messageExt];
  183. }
  184. + (EMMessage *)sendVoiceMessageWithLocalPath:(NSString *)localPath
  185. duration:(NSInteger)duration
  186. to:(NSString *)to
  187. messageType:(EMChatType)messageType
  188. messageExt:(NSDictionary *)messageExt
  189. {
  190. EMVoiceMessageBody *body = [[EMVoiceMessageBody alloc] initWithLocalPath:localPath displayName:@"audio"];
  191. body.duration = (int)duration;
  192. NSString *from = [[EMClient sharedClient] currentUsername];
  193. EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
  194. message.chatType = messageType;
  195. return message;
  196. }
  197. //点击登陆后的操作 - 登录环信
  198. - (void)loginEaseWithUsername:(NSString *)username
  199. password:(NSString *)password
  200. {
  201. [[EMClient sharedClient] loginWithUsername:username password:password completion:^(NSString *aUsername, EMError *aError) {
  202. EMError *error = aError;
  203. dispatch_async(dispatch_get_main_queue(), ^{
  204. if (!error) {
  205. //设置是否自动登录
  206. [[EMClient sharedClient].options setIsAutoLogin:YES];
  207. NSLog(@"emclient login");
  208. } else {
  209. NSLog(@"emclient login error");
  210. switch (error.code)
  211. {
  212. case EMErrorUserNotFound:
  213. [self againRegisterAccout];
  214. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title1"));
  215. break;
  216. case EMErrorNetworkUnavailable:
  217. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title2"));
  218. break;
  219. case EMErrorServerNotReachable:
  220. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title3"));
  221. break;
  222. case EMErrorUserAuthenticationFailed:
  223. // NSLog(@"huanxin errorDescription ----------- %@", error.errorDescription);
  224. break;
  225. case EMErrorServerTimeout:
  226. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title4"));
  227. break;
  228. case EMErrorServerServingForbidden:
  229. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title5"));
  230. break;
  231. case EMErrorUserLoginTooManyDevices:
  232. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title6"));
  233. break;
  234. case EMErrorUserNotLogin:
  235. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title6"));
  236. break;
  237. default:
  238. // NSLog(@"huanxin errorDescription ----------- %@", LanguageStr(@"MessageListVC_title7"));
  239. break;
  240. }
  241. }
  242. });
  243. }];
  244. }
  245. - (void)againRegisterAccout{
  246. //==注册环信账号==//
  247. // NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  248. // [[BaseManagersFactory getHttpsManager] haya_registerEaseAccount:params callback:^(EaseModel *object, NSError *error) {//
  249. // if (!error) {
  250. // if (object.error_code==0) {
  251. // NSString *username = [[NSString alloc] initWithFormat:@"%d",object.id];
  252. // [self haya_loginEaseWithUsername:username password:object.im_password];
  253. // }
  254. // }
  255. // }];
  256. }
  257. #pragma mark - 退出环信
  258. - (void)easeLogout {
  259. [[EMClient sharedClient] logout:YES completion:^(EMError *aError) {
  260. if (aError) {
  261. NSLog(@"haya_easeLogout = %@", aError.errorDescription);
  262. }
  263. }];
  264. }
  265. - (NSArray *)getAllConversations{
  266. NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
  267. return conversations;
  268. }
  269. /// 发送消息
  270. /// @param aMessage <#aMessage description#>
  271. /// @param aProgressBlock <#aProgressBlock description#>
  272. /// @param aCompletionBlock <#aCompletionBlock description#>
  273. + (void)sendMessage:(EMMessage *)aMessage
  274. progress:(void (^)(int progress))aProgressBlock
  275. completion:(void (^)(EMMessage *message, EMError *error))aCompletionBlock{
  276. [[EMClient sharedClient].chatManager sendMessage:aMessage progress:aProgressBlock completion:aCompletionBlock];
  277. }
  278. @end