| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- /************************************************************
- * * Hyphenate CONFIDENTIAL
- * __________________
- * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
- *
- * NOTICE: All information contained herein is, and remains
- * the property of Hyphenate Inc.
- * Dissemination of this information or reproduction of this material
- * is strictly forbidden unless prior written permission is obtained
- * from Hyphenate Inc.
- */
- #import <UserNotifications/UserNotifications.h>
- #import "EaseSDKHelper.h"
- #import "EaseConvertToCommonEmoticonsHelper.h"
- //@interface EMChatImageOptions : NSObject<IChatImageOptions>
- //
- //@property (assign, nonatomic) CGFloat compressionQuality;
- //
- //@end
- static EaseSDKHelper *helper = nil;
- @implementation EaseSDKHelper
- @synthesize isShowingimagePicker = _isShowingimagePicker;
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- [self commonInit];
- }
-
- return self;
- }
- +(instancetype)shareHelper
- {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- helper = [[EaseSDKHelper alloc] init];
- });
-
- return helper;
- }
- #pragma mark - private
- - (void)commonInit
- {
-
- }
- #pragma mark - app delegate notifications
- /** @brief 注册App切入后台和进入前台的通知 */
- - (void)_setupAppDelegateNotifications
- {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(appDidEnterBackgroundNotif:)
- name:UIApplicationDidEnterBackgroundNotification
- object:nil];
-
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(appWillEnterForeground:)
- name:UIApplicationWillEnterForegroundNotification
- object:nil];
- }
- - (void)appDidEnterBackgroundNotif:(NSNotification*)notif
- {
- [[EMClient sharedClient] applicationDidEnterBackground:notif.object];
- }
- - (void)appWillEnterForeground:(NSNotification*)notif
- {
- [[EMClient sharedClient] applicationWillEnterForeground:notif.object];
- }
- #pragma mark - register apns
- /** @brief 注册远程通知 */
- - (void)_registerRemoteNotification
- {
- UIApplication *application = [UIApplication sharedApplication];
- application.applicationIconBadgeNumber = 0;
- if (NSClassFromString(@"UNUserNotificationCenter")) {
- [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError *error) {
- if (granted) {
- #if !TARGET_IPHONE_SIMULATOR
- [application registerForRemoteNotifications];
- #endif
- }
- }];
- return;
- }
- if([application respondsToSelector:@selector(registerUserNotificationSettings:)])
- {
- UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
- UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
- [application registerUserNotificationSettings:settings];
- }
-
- #if !TARGET_IPHONE_SIMULATOR
- if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
- [application registerForRemoteNotifications];
- }else{
- UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
- UIRemoteNotificationTypeSound |
- UIRemoteNotificationTypeAlert;
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
- }
- #endif
- }
- #pragma mark - init Hyphenate
- - (void)hyphenateApplication:(UIApplication *)application
- didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- appkey:(NSString *)appkey
- apnsCertName:(NSString *)apnsCertName
- otherConfig:(NSDictionary *)otherConfig
- {
- [self _setupAppDelegateNotifications];
- [self _registerRemoteNotification];
-
- EMOptions *options = [EMOptions optionsWithAppkey:appkey];
- options.apnsCertName = apnsCertName;
- options.isAutoAcceptGroupInvitation = NO;
- if ([otherConfig objectForKey:kSDKConfigEnableConsoleLogger]) {
- options.enableConsoleLog = YES;
- }
-
- BOOL isHttpsOnly = NO;
- if ([otherConfig objectForKey:@"httpsOnly"]) {
- isHttpsOnly = [[otherConfig objectForKey:@"httpsOnly"] boolValue];
- }
- options.usingHttpsOnly = isHttpsOnly;
-
- BOOL sandBox = [otherConfig objectForKey:@"easeSandBox"] && [[otherConfig objectForKey:@"easeSandBox"] boolValue];
- if (!sandBox) {
- [[EMClient sharedClient] initializeSDKWithOptions:options];
- }
- }
- - (void)hyphenateApplication:(UIApplication *)application
- didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- [[EMClient sharedClient] application:application didReceiveRemoteNotification:userInfo];
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - send message
- + (EMMessage *)sendTextMessage:(NSString *)text
- to:(NSString *)toUser
- messageType:(EMChatType)messageType
- messageExt:(NSDictionary *)messageExt
- {
- NSString *willSendText = [EaseConvertToCommonEmoticonsHelper convertToCommonEmoticons:text];
- EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:willSendText];
- NSString *from = [[EMClient sharedClient] currentUsername];
- EMMessage *message = [[EMMessage alloc] initWithConversationID:toUser from:from to:toUser body:body ext:messageExt];
- message.chatType = messageType;
-
- return message;
- }
- + (EMMessage *)sendCmdMessage:(NSString *)action
- to:(NSString *)to
- messageType:(EMChatType)messageType
- messageExt:(NSDictionary *)messageExt
- cmdParams:(NSArray *)params
- {
- EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:action];
- if (params) {
- body.params = params;
- }
- NSString *from = [[EMClient sharedClient] currentUsername];
- EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
- message.chatType = messageType;
-
- return message;
- }
- + (EMMessage *)sendLocationMessageWithLatitude:(double)latitude
- longitude:(double)longitude
- address:(NSString *)address
- to:(NSString *)to
- messageType:(EMChatType)messageType
- messageExt:(NSDictionary *)messageExt
- {
- EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:latitude longitude:longitude address:address];
- NSString *from = [[EMClient sharedClient] currentUsername];
- EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
- message.chatType = messageType;
-
- return message;
- }
- + (EMMessage *)sendImageMessageWithImageData:(NSData *)imageData
- to:(NSString *)to
- messageType:(EMChatType)messageType
- messageExt:(NSDictionary *)messageExt
- {
- EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:imageData displayName:@"image"];
- NSString *from = [[EMClient sharedClient] currentUsername];
- EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
- message.chatType = messageType;
-
- return message;
- }
- + (EMMessage *)sendImageMessageWithImage:(UIImage *)image
- to:(NSString *)to
- messageType:(EMChatType)messageType
- messageExt:(NSDictionary *)messageExt
- {
- NSData *data = UIImageJPEGRepresentation(image, 1);
-
- return [self sendImageMessageWithImageData:data to:to messageType:messageType messageExt:messageExt];
- }
- + (EMMessage *)sendVoiceMessageWithLocalPath:(NSString *)localPath
- duration:(NSInteger)duration
- to:(NSString *)to
- messageType:(EMChatType)messageType
- messageExt:(NSDictionary *)messageExt
- {
- EMVoiceMessageBody *body = [[EMVoiceMessageBody alloc] initWithLocalPath:localPath displayName:@"audio"];
- body.duration = (int)duration;
- NSString *from = [[EMClient sharedClient] currentUsername];
- EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
- message.chatType = messageType;
-
- return message;
- }
- + (EMMessage *)sendVideoMessageWithURL:(NSURL *)url
- to:(NSString *)to
- messageType:(EMChatType)messageType
- messageExt:(NSDictionary *)messageExt
- {
- EMVideoMessageBody *body = [[EMVideoMessageBody alloc] initWithLocalPath:[url path] displayName:@"video.mp4"];
- NSString *from = [[EMClient sharedClient] currentUsername];
- EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt];
- message.chatType = messageType;
-
- return message;
- }
- @end
|