Skip to content

Commit cbbd71a

Browse files
committed
update ios add sendmessage api
1 parent d1a5bec commit cbbd71a

File tree

3 files changed

+195
-28
lines changed

3 files changed

+195
-28
lines changed

ios/RCTJMessageModule/JMessageHelper.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ -(void)initJMessage:(NSDictionary*)options{
3737

3838
if (options[@"appkey"]) {
3939
appkey = options[@"appkey"];
40+
self.JMessageAppKey = appkey;
4041
}
4142

4243
if (options[@"channel"]) {

ios/RCTJMessageModule/RCTJMessageModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define syncRoamingMessageEvent @"JMessage.SyncRoamingMessage" // 同步漫游消息事件
2525
#define messageRetractEvent @"JMessage.MessageRetract" // 消息撤回事件
2626
#define contactNotifyEvent @"JMessage.ContactNotify" // 收到好友请求消息事件
27+
#define uploadProgressEvent @"JMessage.UploadProgress" // 上传进度
2728

2829
@interface RCTJMessageModule : NSObject<RCTBridgeModule>
2930
@property(strong, nonatomic)NSDictionary *launchOptions;

ios/RCTJMessageModule/RCTJMessageModule.m

Lines changed: 193 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,45 +161,24 @@ -(void)didSendMessage:(NSNotification *)notification {
161161
}
162162
[self.SendMsgCallbackDic removeObjectForKey:msgDic[@"id"]];
163163
}
164-
// TODO: fix ==================>
165-
// - MARK: JMessage Event
166-
//- (void)conversationChanged:(NSNotification *)notification {
167-
//// [JMessagePlugin evalFuntionName:@"onConversationChanged" jsonParm:[notification.object toJsonString]];
168-
//}
169-
//
170-
//- (void)unreadChanged:(NSNotification *)notification{
171-
// [JMessagePlugin evalFuntionName:@"onUnreadChanged" jsonParm:[notification.object toJsonString]];
172-
//}
173-
//
174-
//- (void)groupInfoChanged:(NSNotification *)notification{
175-
// [JMessagePlugin evalFuntionName:@"onGroupInfoChanged" jsonParm:[notification.object toJsonString]];
176-
//}
177164

178165
- (void)loginStateChanged:(NSNotification *)notification{
179-
180166
[self.bridge.eventDispatcher sendAppEventWithName:loginStateChangedEvent body:notification.object];
181167
}
182168

183169
- (void)onContactNotify:(NSNotification *)notification{
184-
185170
[self.bridge.eventDispatcher sendAppEventWithName:contactNotifyEvent body:notification.object];
186171
}
187172

188173
- (void)didReceiveRetractMessage:(NSNotification *)notification{
189-
190174
[self.bridge.eventDispatcher sendAppEventWithName:messageRetractEvent body:notification.object];
191175
}
192176

193177
//didReceiveJMessageMessage change name
194178
- (void)didReceiveJMessageMessage:(NSNotification *)notification {
195-
196179
[self.bridge.eventDispatcher sendAppEventWithName:receiveMsgEvent body:notification.object];
197180
}
198181

199-
// TODO: fix <================
200-
201-
202-
203182
//#pragma mark IM - User
204183

205184

@@ -273,7 +252,7 @@ - (void)didReceiveJMessageMessage:(NSNotification *)notification {
273252
RCT_EXPORT_METHOD(updateMyPassword:(NSDictionary *)param
274253
successCallback:(RCTResponseSenderBlock)successCallback
275254
failCallback:(RCTResponseSenderBlock)failCallback) {
276-
255+
277256
if (param[@"oldPwd"] && param[@"newPwd"]) {
278257
[JMSGUser updateMyPasswordWithNewPassword:param[@"newPwd"] oldPassword:param[@"oldPwd"] completionHandler:^(id resultObject, NSError *error) {
279258
if (!error) {
@@ -862,7 +841,7 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic {
862841
RCT_EXPORT_METHOD(getHistoryMessages:(NSDictionary *)param
863842
successCallback:(RCTResponseSenderBlock)successCallback
864843
failCallback:(RCTResponseSenderBlock)failCallback) {
865-
844+
// NSDictionary * param = [command argumentAtIndex:0];
866845
if (param[@"type"] == nil ||
867846
param[@"from"] == nil ||
868847
param[@"limit"] == nil) {
@@ -949,7 +928,7 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic {
949928
RCT_EXPORT_METHOD(acceptInvitation:(NSDictionary *)param
950929
successCallback:(RCTResponseSenderBlock)successCallback
951930
failCallback:(RCTResponseSenderBlock)failCallback) {
952-
931+
// NSDictionary * param = [command argumentAtIndex:0];
953932
if (param[@"username"] == nil) {
954933
failCallback(@[[self getParamError]]);
955934
return;
@@ -2011,7 +1990,7 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic {
20111990
NSMutableArray *conversationDicList = @[].mutableCopy;
20121991

20131992
if (conversationList.count < 1) {
2014-
successCallback(@[]);
1993+
successCallback(@[@[]]);
20151994
} else {
20161995
for (JMSGConversation *conversation in conversationList) {
20171996
[conversationDicList addObject:[conversation conversationToDictionary]];
@@ -2157,9 +2136,9 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic {
21572136
}
21582137

21592138
RCT_EXPORT_METHOD(createSendMessage:(NSDictionary *)param
2160-
successCallback:(RCTResponseSenderBlock)successCallback) {
2139+
callback:(RCTResponseSenderBlock)callback) {
21612140
/**
2162-
* @param {object} params = {
2141+
* {object} params = {
21632142
* 'type': String, // 'single' / 'group'
21642143
* 'messageType': String, // 'text', 'image', 'voice', 'location', 'file', 'custom'
21652144
* 'groupId': String, // 当 type = group 时,groupId 不能为空
@@ -2176,7 +2155,193 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic {
21762155
* 'extras': Object, // Optional. 自定义键值对 = {'key1': 'value1'}
21772156
* }
21782157
*/
2179-
JMSGMessage
2158+
if (!param[@"type"]) {
2159+
callback(@[]);
2160+
return;
2161+
}
2162+
2163+
2164+
NSString *mediaPath = @"";
2165+
if ([param[@"messageType"] isEqualToString:@"image"] ||
2166+
[param[@"messageType"] isEqualToString:@"voice"] ||
2167+
[param[@"messageType"] isEqualToString:@"file"]) {
2168+
mediaPath = param[@"path"];
2169+
if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){
2170+
mediaPath = mediaPath;
2171+
} else {
2172+
callback(@[[self getMediafileError]]);//TODO: fix
2173+
}
2174+
}
2175+
2176+
JMSGAbstractContent *content = nil;
2177+
if ([param[@"messageType"] isEqualToString:@"text"]) {
2178+
content = [[JMSGTextContent alloc] initWithText:param[@"text"]];
2179+
}
2180+
2181+
if ([param[@"messageType"] isEqualToString:@"image"]) {
2182+
content = [[JMSGImageContent alloc] initWithImageData: [NSData dataWithContentsOfFile: mediaPath]];
2183+
}
2184+
2185+
if ([param[@"messageType"] isEqualToString:@"voice"]) {
2186+
double duration = 0;
2187+
if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]) {
2188+
mediaPath = mediaPath;
2189+
2190+
NSError *error = nil;
2191+
AVAudioPlayer *avAudioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:mediaPath] error: &error];
2192+
if (error) {
2193+
callback(@[[self getMediafileError]]);
2194+
return;
2195+
}
2196+
2197+
duration = avAudioPlayer.duration;
2198+
avAudioPlayer = nil;
2199+
2200+
} else {
2201+
callback(@[[self getMediafileError]]);
2202+
return;
2203+
}
2204+
2205+
content = [[JMSGVoiceContent alloc] initWithVoiceData:[NSData dataWithContentsOfFile: mediaPath] voiceDuration:@(duration)];
2206+
2207+
2208+
}
2209+
2210+
if ([param[@"messageType"] isEqualToString:@"location"]) {
2211+
2212+
2213+
content = [[JMSGLocationContent alloc] initWithLatitude:param[@"latitude"]
2214+
longitude:param[@"longitude"]
2215+
scale:param[@"scale"]
2216+
address:param[@"address"]];
2217+
2218+
2219+
}
2220+
2221+
if ([param[@"messageType"] isEqualToString:@"file"]) {
21802222

2223+
content = [[JMSGFileContent alloc] initWithFileData:[NSData dataWithContentsOfFile: mediaPath]
2224+
fileName: param[@"fileName"]];
2225+
2226+
}
2227+
2228+
if ([param[@"messageType"] isEqualToString:@"custom"]) {
2229+
content = [[JMSGCustomContent alloc] initWithCustomDictionary: param[@"customObject"]];
2230+
}
2231+
2232+
NSString *appKey = nil;
2233+
if (param[@"appKey"]) {
2234+
appKey = param[@"appKey"];
2235+
} else {
2236+
appKey = [JMessageHelper shareInstance].JMessageAppKey;
2237+
}
2238+
2239+
if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) {
2240+
[JMSGConversation createSingleConversationWithUsername:param[@"username"] appKey:appKey completionHandler:^(id resultObject, NSError *error) {
2241+
if (error) {
2242+
callback(@[[error errorToDictionary]]);
2243+
return;
2244+
}
2245+
JMSGConversation *conversation = resultObject;
2246+
JMSGMessage *message = [conversation createMessageWithContent:content];
2247+
2248+
callback(@[[message messageToDictionary]]);
2249+
}];
2250+
} else {
2251+
[JMSGConversation createGroupConversationWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) {
2252+
if (error) {
2253+
callback(@[[error errorToDictionary]]);
2254+
return;
2255+
}
2256+
JMSGConversation *conversation = resultObject;
2257+
JMSGMessage *message = [conversation createMessageWithContent:content];
2258+
if (param[@"extras"] && [param[@"extras"] isKindOfClass: [NSDictionary class]]) {
2259+
NSDictionary *extras = param[@"extras"];
2260+
for (NSString *key in extras.allKeys) {
2261+
[message updateMessageExtraValue: extras[key] forKey: key];
2262+
}
2263+
}
2264+
callback(@[[message messageToDictionary]]);
2265+
}];
2266+
}
2267+
}
2268+
2269+
RCT_EXPORT_METHOD(sendMessage:(NSDictionary *)param
2270+
successCallback:(RCTResponseSenderBlock)successCallback
2271+
failCallBack:(RCTResponseSenderBlock)failCallback) {
2272+
2273+
NSString *appKey = nil;
2274+
if (param[@"appKey"]) {
2275+
appKey = param[@"appKey"];
2276+
} else {
2277+
appKey = [JMessageHelper shareInstance].JMessageAppKey;
2278+
}
2279+
2280+
if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) {
2281+
[JMSGConversation createSingleConversationWithUsername:param[@"username"] appKey:appKey completionHandler:^(id resultObject, NSError *error) {
2282+
if (error) {
2283+
failCallback(@[[error errorToDictionary]]);
2284+
return;
2285+
}
2286+
2287+
JMSGConversation *conversation = resultObject;
2288+
JMSGMessage *message = [conversation messageWithMessageId: param[@"id"]];
2289+
2290+
if ([message.content isKindOfClass:[JMSGMediaAbstractContent class]]) {
2291+
JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *)message.content;
2292+
content.uploadHandler = ^(float percent, NSString *msgID) {
2293+
2294+
[self.bridge.eventDispatcher sendAppEventWithName:uploadProgressEvent body:@{@"messageId": msgID,
2295+
@"progress": @(percent)}];
2296+
};
2297+
}
2298+
2299+
JMSGOptionalContent *messageSendingOptions = nil;
2300+
if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) {
2301+
messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]];
2302+
}
2303+
2304+
self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback];
2305+
2306+
if (messageSendingOptions) {
2307+
[conversation sendMessage:message optionalContent:messageSendingOptions];
2308+
} else {
2309+
[conversation sendMessage:message];
2310+
}
2311+
}];
2312+
} else {
2313+
[JMSGConversation createGroupConversationWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) {
2314+
if (error) {
2315+
failCallback(@[[error errorToDictionary]]);
2316+
return;
2317+
}
2318+
2319+
JMSGConversation *conversation = resultObject;
2320+
JMSGMessage *message = [conversation messageWithMessageId: param[@"id"]];
2321+
2322+
if ([message.content isKindOfClass:[JMSGMediaAbstractContent class]]) {
2323+
JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *)message.content;
2324+
content.uploadHandler = ^(float percent, NSString *msgID) {
2325+
[self.bridge.eventDispatcher sendAppEventWithName:uploadProgressEvent body:@{@"messageId": msgID,
2326+
@"progress": @(percent)}];
2327+
};
2328+
}
2329+
2330+
JMSGOptionalContent *messageSendingOptions = nil;
2331+
if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) {
2332+
messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]];
2333+
}
2334+
2335+
self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback];
2336+
if (messageSendingOptions) {
2337+
[conversation sendMessage:message optionalContent:messageSendingOptions];
2338+
} else {
2339+
[conversation sendMessage:message];
2340+
}
2341+
2342+
}];
2343+
}
21812344
}
2345+
2346+
21822347
@end

0 commit comments

Comments
 (0)