@@ -31,6 +31,7 @@ public class JPushModule extends ReactContextBaseJavaModule {
3131 private Context mContext ;
3232 private static String mEvent ;
3333 private static Bundle mCachedBundle ;
34+ private static ReactApplicationContext mRAC ;
3435
3536 private final static String RECEIVE_NOTIFICATION = "receiveNotification" ;
3637 private final static String RECEIVE_CUSTOM_MESSAGE = "receivePushMsg" ;
@@ -105,6 +106,7 @@ public void resumePush() {
105106 public void notifyJSDidLoad () {
106107 // send cached event
107108 if (getReactApplicationContext ().hasActiveCatalystInstance ()) {
109+ mRAC = getReactApplicationContext ();
108110 if (mEvent != null ) {
109111 switch (mEvent ) {
110112 case RECEIVE_CUSTOM_MESSAGE :
@@ -457,17 +459,41 @@ public JPushReceiver() {
457459 public void onReceive (Context context , Intent data ) {
458460 mCachedBundle = data .getExtras ();
459461 if (JPushInterface .ACTION_MESSAGE_RECEIVED .equals (data .getAction ())) {
460- String message = data .getStringExtra (JPushInterface .EXTRA_MESSAGE );
461- Logger .i (TAG , "收到自定义消息: " + message );
462- mEvent = RECEIVE_CUSTOM_MESSAGE ;
462+ try {
463+ String message = data .getStringExtra (JPushInterface .EXTRA_MESSAGE );
464+ Logger .i (TAG , "收到自定义消息: " + message );
465+ if (mRAC != null ) {
466+ WritableMap map = Arguments .createMap ();
467+ map .putString ("message" , message );
468+ map .putString ("extras" , data .getExtras ().getString (JPushInterface .EXTRA_EXTRA ));
469+ mRAC .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
470+ .emit (RECEIVE_CUSTOM_MESSAGE , map );
471+ } else {
472+ mEvent = RECEIVE_CUSTOM_MESSAGE ;
473+ }
474+ } catch (Exception e ) {
475+ e .printStackTrace ();
476+ }
463477 } else if (JPushInterface .ACTION_NOTIFICATION_RECEIVED .equals (data .getAction ())) {
464- // 通知内容
465- String alertContent = mCachedBundle .getString (JPushInterface .EXTRA_ALERT );
466- // extra 字段的 json 字符串
467- String extras = mCachedBundle .getString (JPushInterface .EXTRA_EXTRA );
468- Logger .i (TAG , "收到推送下来的通知: " + alertContent );
469- //cache event
470- mEvent = RECEIVE_NOTIFICATION ;
478+ try {
479+ // 通知内容
480+ String alertContent = mCachedBundle .getString (JPushInterface .EXTRA_ALERT );
481+ // extra 字段的 json 字符串
482+ String extras = mCachedBundle .getString (JPushInterface .EXTRA_EXTRA );
483+ Logger .i (TAG , "收到推送下来的通知: " + alertContent );
484+ if (mRAC != null ) {
485+ WritableMap map = Arguments .createMap ();
486+ map .putString ("alertContent" , alertContent );
487+ map .putString ("extras" , extras );
488+ mRAC .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
489+ .emit (RECEIVE_NOTIFICATION , map );
490+ } else {
491+ //cache event
492+ mEvent = RECEIVE_NOTIFICATION ;
493+ }
494+ } catch (Exception e ) {
495+ e .printStackTrace ();
496+ }
471497 } else if (JPushInterface .ACTION_NOTIFICATION_OPENED .equals (data .getAction ())) {
472498 try {
473499 Logger .d (TAG , "用户点击打开了通知" );
@@ -480,8 +506,16 @@ public void onReceive(Context context, Intent data) {
480506 intent .putExtras (mCachedBundle );
481507 intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TOP );
482508 context .startActivity (intent );
483- // cache event
484- mEvent = OPEN_NOTIFICATION ;
509+ if (mRAC != null ) {
510+ WritableMap map = Arguments .createMap ();
511+ map .putString ("alertContent" , alertContent );
512+ map .putString ("extras" , extras );
513+ mRAC .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
514+ .emit (OPEN_NOTIFICATION , map );
515+ } else {
516+ // cache event
517+ mEvent = OPEN_NOTIFICATION ;
518+ }
485519 } catch (Exception e ) {
486520 e .printStackTrace ();
487521 Logger .i (TAG , "Shouldn't access here" );
@@ -490,7 +524,16 @@ public void onReceive(Context context, Intent data) {
490524 // After JPush finished registering, will send this broadcast, use JPushModule.addGetRegistrationIdListener
491525 // to get registrationId in the first instance.
492526 } else if (JPushInterface .ACTION_REGISTRATION_ID .equals (data .getAction ())) {
493- mEvent = RECEIVE_REGISTRATION_ID ;
527+ try {
528+ if (mRAC != null ) {
529+ mRAC .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
530+ .emit (mEvent , mCachedBundle .getString (JPushInterface .EXTRA_REGISTRATION_ID ));
531+ } else {
532+ mEvent = RECEIVE_REGISTRATION_ID ;
533+ }
534+ } catch (Exception e ) {
535+ e .printStackTrace ();
536+ }
494537 }
495538 }
496539
0 commit comments