Skip to content

Commit 32f6638

Browse files
author
zhanq
committed
统一android/ios收到推送和点击推送的消息内容
1 parent 297a7f2 commit 32f6638

File tree

4 files changed

+107
-57
lines changed

4 files changed

+107
-57
lines changed

android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.facebook.react.modules.core.DeviceEventManagerModule;
3030

3131

32+
import org.json.JSONException;
3233
import org.json.JSONObject;
3334

3435
import java.lang.reflect.Field;
@@ -37,6 +38,7 @@
3738
import java.text.SimpleDateFormat;
3839
import java.util.Date;
3940
import java.util.HashSet;
41+
import java.util.Iterator;
4042
import java.util.LinkedHashSet;
4143
import java.util.List;
4244
import java.util.Set;
@@ -181,21 +183,36 @@ private static void sendEvent() {
181183
switch (mEvent) {
182184
case RECEIVE_CUSTOM_MESSAGE:
183185
WritableMap map = Arguments.createMap();
184-
map.putInt("id", mCachedBundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID));
185-
map.putString("message", mCachedBundle.getString(JPushInterface.EXTRA_MESSAGE));
186-
map.putString("content", mCachedBundle.getString(JPushInterface.EXTRA_MESSAGE));
187-
map.putString("content_type", mCachedBundle.getString(JPushInterface.EXTRA_CONTENT_TYPE));
188-
map.putString("title", mCachedBundle.getString(JPushInterface.EXTRA_TITLE));
186+
WritableMap extrasMap =Arguments.createMap();
189187
map.putString("extras", mCachedBundle.getString(JPushInterface.EXTRA_EXTRA));
188+
map.putString("content_type", mCachedBundle.getString(JPushInterface.EXTRA_CONTENT_TYPE));
189+
map.putString("content", mCachedBundle.getString(JPushInterface.EXTRA_MESSAGE));
190+
map.putString("title", mCachedBundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE));
191+
map.putString("id", mCachedBundle.getString(JPushInterface.EXTRA_MSG_ID));
190192
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
191193
.emit(mEvent, map);
192194
break;
193195
case RECEIVE_NOTIFICATION:
194196
case OPEN_NOTIFICATION:
195197
map = Arguments.createMap();
196-
map.putInt("id", mCachedBundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID));
197-
map.putString("alertContent", mCachedBundle.getString(JPushInterface.EXTRA_ALERT));
198-
map.putString("extras", mCachedBundle.getString(JPushInterface.EXTRA_EXTRA));
198+
extrasMap =Arguments.createMap();
199+
try {
200+
String string = mCachedBundle.getString(JPushInterface.EXTRA_EXTRA);
201+
JSONObject jsonObject = new JSONObject(string);
202+
Iterator it = jsonObject.keys();
203+
while (it.hasNext()) {
204+
String key = (String)it.next();
205+
String value = jsonObject.getString(key);
206+
extrasMap.putString(key,value);
207+
}
208+
} catch (JSONException e) {
209+
e.printStackTrace();
210+
}
211+
map.putMap("extras",extrasMap);
212+
map.putString("content_type", mCachedBundle.getString(JPushInterface.EXTRA_ALERT_TYPE));
213+
map.putString("content", mCachedBundle.getString(JPushInterface.EXTRA_ALERT));
214+
map.putString("title", mCachedBundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE));
215+
map.putString("id", mCachedBundle.getString(JPushInterface.EXTRA_MSG_ID));
199216
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
200217
.emit(mEvent, map);
201218
break;
@@ -599,12 +616,9 @@ public JPushReceiver() {
599616

600617
@Override
601618
public void onReceive(Context context, Intent data) {
602-
603619
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(data.getAction())) {
604620
mCachedBundle = data.getExtras();
605621
try {
606-
String message = data.getStringExtra(JPushInterface.EXTRA_MESSAGE);
607-
Logger.i(TAG, "收到自定义消息: " + message);
608622
mEvent = RECEIVE_CUSTOM_MESSAGE;
609623
if (mRAC != null) {
610624
sendEvent();
@@ -615,12 +629,6 @@ public void onReceive(Context context, Intent data) {
615629
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(data.getAction())) {
616630
mCachedBundle = data.getExtras();
617631
try {
618-
// 通知内容
619-
String alertContent = mCachedBundle.getString(JPushInterface.EXTRA_ALERT);
620-
// extra 字段的 json 字符串
621-
String extras = mCachedBundle.getString(JPushInterface.EXTRA_EXTRA);
622-
Logger.i(TAG, "收到推送下来的通知: " + alertContent);
623-
Logger.i(TAG, "extras: " + extras);
624632
mEvent = RECEIVE_NOTIFICATION;
625633
if (mRAC != null) {
626634
sendEvent();
@@ -631,15 +639,6 @@ public void onReceive(Context context, Intent data) {
631639
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(data.getAction())) {
632640
mCachedBundle = data.getExtras();
633641
try {
634-
Logger.d(TAG, "用户点击打开了通知");
635-
// 通知内容
636-
String alertContent = mCachedBundle.getString(JPushInterface.EXTRA_ALERT);
637-
// extra 字段的 json 字符串
638-
String extras = mCachedBundle.getString(JPushInterface.EXTRA_EXTRA);
639-
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
640-
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
641-
intent.putExtras(mCachedBundle);
642-
context.startActivity(intent);
643642
mEvent = OPEN_NOTIFICATION;
644643
if (mRAC != null) {
645644
sendEvent();

example/App.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,39 @@ export default class App extends Component {
234234
this.setState({
235235
pushMsg: map.content
236236
})
237-
console.log('extras: ' + map.extras)
237+
for (var key in map) {
238+
console.log('JS receiveCustomNotification key:'+key+",vaule:" +map[key]);
239+
}
238240
}
239-
240241
JPushModule.addReceiveCustomMsgListener(this.receiveCustomMsgListener)
242+
241243
this.receiveNotificationListener = map => {
242-
console.log('alertContent: ' + map.alertContent)
243-
console.log('extras: ' + map.extras)
244+
for (var key in map) {
245+
console.log('JS receiveNotification key:'+key+",vaule:" +map[key]);
246+
if(key==="extras"){
247+
var extrasMap = map["extras"];
248+
for(var extras in extrasMap){
249+
console.log('JS receiveNotification extras key:'+extras+",vaule:" +extrasMap[extras]);
250+
}
251+
}
252+
}
244253
}
245254
JPushModule.addReceiveNotificationListener(this.receiveNotificationListener)
246-
255+
256+
if(Platform.OS === "ios"){//监听应用启动
257+
this.openNotificationLaunchAppListener = map => {
258+
for (var key in map) {
259+
console.log('JS openNotificationLaunchApp key:'+key+",vaule:" +map[key]);
260+
}
261+
this.jumpSecondActivity()
262+
}
263+
JPushModule.addOpenNotificationLaunchAppListener(this.openNotificationLaunchAppListener)
264+
}
265+
247266
this.openNotificationListener = map => {
248-
console.log('Opening notification!')
249-
console.log('map.extra: ' + map.extras)
267+
for (var key in map) {
268+
console.log('JS openNotification key:'+key+",vaule:" +map[key]);
269+
}
250270
this.jumpSecondActivity()
251271
}
252272
JPushModule.addReceiveOpenNotificationListener(this.openNotificationListener)

index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ declare class JPush {
225225

226226
/**
227227
* 监听:接收推送事件
228+
*
229+
* any = {
230+
* id: string
231+
* title: string
232+
* content: string
233+
* extras = {string : string,string : string ....... }
234+
* content_type: string (android only)
235+
* appState: string (inactive/active/background ,ios only)
236+
* }
237+
*
228238
*/
229239
static addReceiveNotificationListener(cb: JSuccessCallback<any>): void
230240

@@ -235,6 +245,16 @@ declare class JPush {
235245

236246
/**
237247
* 监听:点击推送事件
248+
*
249+
* any = {
250+
* id: string
251+
* title: string
252+
* content: string
253+
* extras = {string : string,string : string ....... }
254+
* content_type: string (android only)
255+
* appState: string (inactive/active/background ,ios only)
256+
* }
257+
*
238258
*/
239259
static addReceiveOpenNotificationListener(cb: JSuccessCallback<any>): void
240260

ios/RCTJPushModule/RCTJPushModule.m

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -234,35 +234,46 @@ - (NSMutableDictionary *)jpushFormatLocalNotificationDic:(NSDictionary *)dic {
234234
}
235235

236236
- (NSMutableDictionary *)jpushFormatAPNSDic:(NSDictionary *)dic {
237-
NSMutableDictionary *extras = @{}.mutableCopy;
238-
for (NSString *key in dic) {
239-
if([key isEqualToString:@"_j_business"] ||
240-
[key isEqualToString:@"_j_msgid"] ||
241-
[key isEqualToString:@"_j_uid"] ||
242-
[key isEqualToString:@"actionIdentifier"] ||
243-
[key isEqualToString:@"aps"]) {
244-
continue;
237+
NSMutableDictionary *extras = @{}.mutableCopy;
238+
NSMutableDictionary *formatDic = @{}.mutableCopy;
239+
for (NSString *key in dic) {
240+
if([key isEqualToString:@"_j_business"] ||
241+
[key isEqualToString:@"_j_msgid"] ||
242+
[key isEqualToString:@"_j_uid"] ||
243+
[key isEqualToString:@"actionIdentifier"] ||
244+
[key isEqualToString:@"aps"]) {
245+
continue;
245246
}
246247
// 和 android 统一将 extras 字段移动到 extras 里面
247248
extras[key] = dic[key];
248249
}
249-
NSMutableDictionary *formatDic = dic.mutableCopy;
250-
formatDic[@"extras"] = extras;
251-
252-
// 新增 应用状态
253-
switch ([UIApplication sharedApplication].applicationState) {
254-
case UIApplicationStateInactive:
255-
formatDic[@"appState"] = @"inactive";
256-
break;
257-
case UIApplicationStateActive:
258-
formatDic[@"appState"] = @"active";
259-
break;
260-
case UIApplicationStateBackground:
261-
formatDic[@"appState"] = @"background";
262-
break;
263-
default:
264-
break;
250+
NSMutableDictionary *aps = dic[@"aps"];
251+
if(dic[@"_j_msgid"]){
252+
formatDic[@"id"] = dic[@"_j_msgid"];
253+
}
254+
if (aps[@"alert"]) {
255+
if([aps[@"alert"] isKindOfClass:[NSString class]]){
256+
formatDic[@"content"] = aps[@"alert"];
257+
}else{
258+
formatDic[@"content"] = aps[@"alert"][@"body"];
259+
formatDic[@"title"] = aps[@"alert"][@"title"];
260+
}
261+
}
262+
// 新增 应用状态
263+
switch ([UIApplication sharedApplication].applicationState) {
264+
case UIApplicationStateInactive:
265+
formatDic[@"appState"] = @"inactive";
266+
break;
267+
case UIApplicationStateActive:
268+
formatDic[@"appState"] = @"active";
269+
break;
270+
case UIApplicationStateBackground:
271+
formatDic[@"appState"] = @"background";
272+
break;
273+
default:
274+
break;
265275
}
276+
formatDic[@"extras"] = extras;
266277
return formatDic;
267278
}
268279

0 commit comments

Comments
 (0)