Skip to content

Commit 4236d69

Browse files
guangyaoguangyao
authored andcommitted
2 parents 396a1e1 + c2dff24 commit 4236d69

File tree

8 files changed

+194
-46
lines changed

8 files changed

+194
-46
lines changed

README.md

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ public class MainActivity extends ReactActivity {
5757
5858
......
5959
60+
@Override
61+
protected void onCreate(Bundle savedInstanceState) {
62+
super.onCreate(savedInstanceState);
63+
if(ReceiverMsgParser.checkOpen(getIntent())){//在后台时处理点击推送消息
64+
ReactCache.emit(ReactCache.observeLaunchPushEvent, ReceiverMsgParser.getWritableMap(getIntent()));
65+
RNNeteaseImModule.launch = getIntent();
66+
}
67+
}
68+
6069
@Override
6170
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
6271
MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
@@ -134,9 +143,18 @@ Run `pod install`
134143
...
135144
[self setupNIMSDK];
136145
[self registerAPNs];
146+
if (launchOptions) {//未启动时,点击推送消息
147+
NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
148+
if (remoteNotification) {
149+
[self performSelector:@selector(clickSendObserve:) withObject:remoteNotification afterDelay:0.5];
150+
}
151+
}
137152
...
138153
return YES;
139154
}
155+
- (void)clickSendObserve:(NSDictionary *)dict{
156+
[[NSNotificationCenter defaultCenter]postNotificationName:@"ObservePushNotification" object:@{@"dict":dict,@"type":@"launch"}];
157+
}
140158
- (void)setupNIMSDK
141159
{
142160
//在注册 NIMSDK appKey 之前先进行配置信息的注册,如是否使用新路径,是否要忽略某些通知,是否需要多端同步未读数
@@ -159,20 +177,20 @@ UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTy
159177
160178
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
161179
{
162-
[[NIMSDK sharedSDK] updateApnsToken:deviceToken];
180+
[[NIMSDK sharedSDK] updateApnsToken:deviceToken];
163181
}
164-
182+
//在后台时处理点击推送消息
165183
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
166-
NSLog(@"receive remote notification: %@", userInfo);
167-
}
168184
185+
[[NSNotificationCenter defaultCenter]postNotificationName:@"ObservePushNotification" object:@{@"dict":userInfo,@"type":@"background"}];
186+
}
169187
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
170188
{
171-
NSLog(@"fail to get apns token :%@",error);
189+
NSLog(@"fail to get apns token :%@",error);
172190
}
173-
174-
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
175-
return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
191+
- (void)applicationDidEnterBackground:(UIApplication *)application {
192+
NSInteger count = [[[NIMSDK sharedSDK] conversationManager] allUnreadCount];
193+
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
176194
}
177195
```
178196

@@ -225,7 +243,28 @@ import NIM from 'react-native-netease-im';
225243
#### 监听会话
226244
```
227245
NativeAppEventEmitter.addListener("observeRecentContact",(data)=>{
228-
 console.log(data); //返回内容android和ios有区别
246+
 console.log(data); //返回会话列表和未读数
229247
});
230248
```
249+
#### 推送
250+
```
251+
//程序运行时获取的推送点击事件
252+
NativeAppEventEmitter.addListener("observeLaunchPushEvent",(data)=>{
253+
 console.log(data);
254+
});
255+
//程序后台时获取的推送点击事件
256+
NativeAppEventEmitter.addListener("observeBackgroundPushEvent",(data)=>{
257+
 console.log(data);
258+
});
259+
//推送数据格式
260+
{
261+
...
262+
   sessionBody:{
263+
sessionId:"",
264+
sessionType:"",
265+
sessionName:""
266+
}
267+
}
268+
269+
```
231270

android/src/main/java/com/netease/im/RNNeteaseImModule.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
import java.util.Map;
8989
import java.util.Set;
9090

91+
import static com.netease.im.ReceiverMsgParser.getIntent;
92+
9193

9294
public class RNNeteaseImModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener {
9395

@@ -1938,14 +1940,26 @@ public void onNewIntent(Intent intent) {
19381940

19391941
LogUtil.w(TAG, "onNewIntent:" + intent.getExtras());
19401942
// ReceiverMsgParser.openIntent(intent);
1941-
if (reactContext.getCurrentActivity() != null && ReceiverMsgParser.checkOpen(ReceiverMsgParser.getIntent())) {
1942-
intent.putExtras(ReceiverMsgParser.getIntent());
1943+
if (reactContext.getCurrentActivity() != null && ReceiverMsgParser.checkOpen(intent)) {
1944+
intent.putExtras(getIntent());
19431945
reactContext.getCurrentActivity().setIntent(intent);
1946+
ReactCache.emit(ReactCache.observeBackgroundPushEvent, ReceiverMsgParser.getWritableMap(intent));
19441947
}
19451948

19461949
}
19471950

19481951
public static String status = "";
1952+
public static Intent launch = null;
1953+
1954+
@ReactMethod
1955+
public void getLaunch(Promise promise) {
1956+
if (launch == null) {
1957+
promise.resolve(null);
1958+
} else {
1959+
promise.resolve(ReceiverMsgParser.getWritableMap(launch));
1960+
launch = null;
1961+
}
1962+
}
19491963

19501964
@Override
19511965
public void onHostResume() {

android/src/main/java/com/netease/im/ReactCache.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public class ReactCache {
8282
public final static String observeAttachmentProgress = "observeAttachmentProgress";//'上传下载进度'
8383
public final static String observeOnKick = "observeOnKick";//'被踢出'
8484
public final static String observeAccountNotice = "observeAccountNotice";//'账户变动通知'
85+
public final static String observeLaunchPushEvent = "observeLaunchPushEvent";//''
86+
public final static String observeBackgroundPushEvent = "observeBackgroundPushEvent";//''
8587

8688
final static String TAG = "ReactCache";
8789
private static ReactContext reactContext;
@@ -454,6 +456,7 @@ public static Object createUserInfo(NimUserInfo userInfo) {
454456
if (userInfo != null) {
455457

456458
writableMap.putString("isMyFriend", boolean2String(FriendDataCache.getInstance().isMyFriend(userInfo.getAccount())));
459+
// writableMap.putString("isMyFriend", boolean2String(NIMClient.getService(FriendService.class).isMyFriend(userInfo.getAccount())));
457460
writableMap.putString("isMe", boolean2String(userInfo.getAccount() != null && userInfo.getAccount().equals(LoginService.getInstance().getAccount())));
458461
writableMap.putString("isInBlackList", boolean2String(NIMClient.getService(FriendService.class).isInBlackList(userInfo.getAccount())));
459462
writableMap.putString("mute", boolean2String(NIMClient.getService(FriendService.class).isNeedMessageNotify(userInfo.getAccount())));

android/src/main/java/com/netease/im/ReceiverMsgParser.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.facebook.react.bridge.Arguments;
88
import com.facebook.react.bridge.WritableMap;
99
import com.netease.im.common.push.Extras;
10+
import com.netease.im.session.SessionUtil;
1011
import com.netease.im.uikit.common.util.log.LogUtil;
12+
import com.netease.nimlib.sdk.NIMClient;
1113
import com.netease.nimlib.sdk.NimIntent;
1214
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
1315
import com.netease.nimlib.sdk.msg.model.IMMessage;
@@ -55,6 +57,7 @@ public static Bundle openIntent(Intent intent) {
5557
result.putString("type", "session");
5658
result.putString("sessionType", Integer.toString(message.getSessionType().getValue()));
5759
result.putString("sessionId", message.getSessionId());
60+
result.putString("sessionName", message.getSessionId());
5861
}
5962
} else if (intent.hasExtra(Extras.EXTRA_JUMP_P2P)) {
6063
Intent data = intent.getParcelableExtra(Extras.EXTRA_DATA);
@@ -63,6 +66,7 @@ public static Bundle openIntent(Intent intent) {
6366
result.putString("type", "session");
6467
result.putString("sessionType", Integer.toString(SessionTypeEnum.P2P.getValue()));
6568
result.putString("sessionId", account);
69+
result.putString("sessionName", account);
6670
}
6771
}
6872

@@ -74,40 +78,52 @@ public static Bundle openIntent(Intent intent) {
7478
}
7579

7680
public static WritableMap getWritableMap(Intent intent) {
77-
WritableMap r = Arguments.createMap();
81+
WritableMap rr = Arguments.createMap();
7882
if (intent != null && canAutoLogin()) {
83+
WritableMap r = Arguments.createMap();
7984
if (intent.hasExtra(NimIntent.EXTRA_NOTIFY_CONTENT)) {
8085
ArrayList<IMMessage> messages = (ArrayList<IMMessage>) intent.getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
8186
if (messages == null || messages.size() > 1) {
82-
r.putString("type", "sessionList");
87+
rr.putString("type", "sessionList");
8388
} else {
8489
IMMessage message = messages.get(0);
85-
r.putString("type", "session");
90+
rr.putString("type", "session");
8691
r.putString("sessionType", Integer.toString(message.getSessionType().getValue()));
8792
r.putString("sessionId", message.getSessionId());
93+
r.putString("sessionName", SessionUtil.getSessionName(message.getSessionId(), message.getSessionType(), false));
8894
}
8995
} else if (intent.hasExtra(Extras.EXTRA_JUMP_P2P)) {
9096
Intent data = intent.getParcelableExtra(Extras.EXTRA_DATA);
9197
String account = data.getStringExtra(Extras.EXTRA_ACCOUNT);
9298
if (!TextUtils.isEmpty(account)) {
93-
r.putString("type", "session");
99+
rr.putString("type", "session");
94100
r.putString("sessionType", Integer.toString(SessionTypeEnum.P2P.getValue()));
95101
r.putString("sessionId", account);
102+
r.putString("sessionName", SessionUtil.getSessionName(account, SessionTypeEnum.P2P, false));
96103
}
97104
}
98-
99-
LogUtil.w("ReceiverMsgParser", intent + "");
105+
rr.putMap("sessionBody", r);
106+
printIntent(intent);
100107
}
101108

102-
LogUtil.w("ReceiverMsgParser", result + "");
103-
return r;
109+
LogUtil.w("ReceiverMsgParser", rr + "");
110+
return rr;
111+
}
112+
113+
static void printIntent(Intent intent) {
114+
LogUtil.w("ReceiverMsgParser", intent + "");
115+
Bundle extra = intent.getExtras();
116+
for (String key : extra.keySet()) {
117+
LogUtil.w("ReceiverMsgParser", "key:" + key);
118+
LogUtil.w("ReceiverMsgParser", "v:" + extra.get(key));
119+
}
104120
}
105121

106122
/**
107123
* 已经登陆过,自动登陆
108124
*/
109125
private static boolean canAutoLogin() {
110-
111-
return true;//!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token);
126+
return !NIMClient.getStatus().wontAutoLogin();
127+
// return true;//!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token);
112128
}
113129
}

0 commit comments

Comments
 (0)