react native 的Android中,怎样设置能够让程序在未运行时能够收到通知

react-native
jpush
标签: #<Tag:0x00007fb83bd44ce8> #<Tag:0x00007fb83bd44b08>

(AaronHero) #15
04-20 11:02:21.867 25954-25954/com.reactiosjpush D/JPushModule: 用户点击打开了通知
04-20 11:02:21.867 25954-25954/com.reactiosjpush W/System.err: java.lang.NullPointerException
04-20 11:02:21.867 25954-25954/com.reactiosjpush W/System.err:     at cn.jpush.reactnativejpush.JPushModule$JPushReceiver.onReceive(JPushModule.java:340)
04-20 11:02:21.867 25954-25954/com.reactiosjpush W/System.err:     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2423)
04-20 11:02:21.867 25954-25954/com.reactiosjpush W/System.err:     at android.app.ActivityThread.access$1700(ActivityThread.java:141)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1288)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at android.os.Looper.loop(Looper.java:136)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5052)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
04-20 11:02:21.877 25954-25954/com.reactiosjpush W/System.err:     at dalvik.system.NativeStart.main(Native Method)
04-20 11:02:21.877 25954-25954/com.reactiosjpush I/JPushModule: Try to start application

这个是打开通知的日志信息,怎样拉起应用并跳转界面?


#16

(AaronHero) #17

en,你发的我都看了。我把代码都换成jpush-react-native的demo代码,在应用未启动时,点开通知,不能够拉起应用


#18

那么如果你 打开手机系统的限制,自启动什么的可否解决问题?


(AaronHero) #19

自启动,允许通知什么的都设置了


(KenChoi) #20

你在点开通知的地方启动 MainActivity:

JPushModule.java

try {
                        Intent intent = new Intent();
                        intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        Logger.i(TAG, "Cannot find MainActivity, will discard onClick event.");
                    }

常见问题 - JPush 合集(持续更新)
(AaronHero) #21
else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(data.getAction())) {
                try {
                    Logger.d(TAG, "用户点击打开了通知");
                    // 通知内容
                    String alertContent = bundle.getString(JPushInterface.EXTRA_ALERT);
                    // extra 字段的 json 字符串
                    String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                    WritableMap map = Arguments.createMap();
                    map.putString("alertContent", alertContent);
                    map.putString("extras", extras);
                    mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                            .emit("openNotification", map);
                    // judge if application is running in background, opening initial Activity.
                    // You can change here to open appointed Activity. All you need to do is create
                    // the appointed Activity, and use JS render the appointed Activity.
                    // Please reference examples' SecondActivity for detail,
                    // and JS files are in folder: example/react-native-android
                    if (isApplicationRunningBackground(context)) {
                        Intent intent = new Intent();
                        intent.setClass(context, mModule.mContext.getClass());
                        Logger.d(TAG, "context.getClass: " + mModule.mContext.getClass());
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
                        // application running in foreground, do nothing
                    }else {
                        Intent intent = new Intent();
                        intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Logger.i(TAG, "Try to start application");
                    if (mRAC != null) {
                        try {
                            Intent intent = new Intent();
                            intent.setClassName(mRAC.getPackageName(), mRAC.getPackageName() + ".MainActivity");
                            intent.putExtras(bundle);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            context.startActivity(intent);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                            Logger.i(TAG, "Cannot find MainActivity, will discard onClick event.");
                        }

                    }
                }

            }

点击通知没反应


(KenChoi) #22

有没有打印日志?


(AaronHero) #23
04-20 16:29:51.880 11296-11296/com.mydemo D/JPushModule: 用户点击打开了通知
04-20 16:29:51.910 11296-11296/com.mydemo W/System.err: java.lang.NullPointerException
04-20 16:29:51.910 11296-11296/com.mydemo W/System.err:     at cn.jpush.reactnativejpush.JPushModule$JPushReceiver.onReceive(JPushModule.java:314)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2423)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at android.app.ActivityThread.access$1700(ActivityThread.java:141)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1288)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at android.os.Looper.loop(Looper.java:136)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5052)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
04-20 16:29:51.930 11296-11296/com.mydemo W/System.err:     at dalvik.system.NativeStart.main(Native Method)
04-20 16:29:51.930 11296-11296/com.mydemo I/JPushModule: Try to start application

上述是程序未启动情况下,打开通知的信息


(KenChoi) #25

你改成这样试试;

} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(data.getAction())) {
                try {
                    Logger.d(TAG, "用户点击打开了通知");
                    // 通知内容
                    String alertContent = bundle.getString(JPushInterface.EXTRA_ALERT);
                    // extra 字段的 json 字符串
                    String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                    WritableMap map = Arguments.createMap();
                    map.putString("alertContent", alertContent);
                    map.putString("extras", extras);
                    // judge if application is running in background, opening initial Activity.
                    // You can change here to open appointed Activity. All you need to do is create
                    // the appointed Activity, and use JS render the appointed Activity.
                    // Please reference examples' SecondActivity for detail,
                    // and JS files are in folder: example/react-native-android
                    if (isApplicationRunningBackground(context)) {
                        Intent intent = new Intent();
                        intent.setClass(context, mModule.mContext.getClass());
                        Logger.d(TAG, "context.getClass: " + mModule.mContext.getClass());
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
                        mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                            .emit("openNotification", map);

                        // application running in foreground, do nothing
                    } else {
                        Intent intent = new Intent();
                        intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
  new ReactApplicationContext(context).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                            .emit("openNotification", map);

                    }

(AaronHero) #26

也不行!!!,应用未启动接收到通知也会
java.lang.NullPointerException
04-20 16:29:51.910 11296-11296/com.mydemo W/System.err: at cn.jpush.reactnativejpush.JPushModule$JPushReceiver.onReceive(JPushModule.java:314)…的错误信息


(KenChoi) #27

应该是 mContext 为空了,我刚才试了,改成这样

if (!isApplicationRunning(context) || mModule.mContext == null) {
                        Intent intent = new Intent();
                        intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
                        new ReactApplicationContext(context).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("openNotification", map);
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(context, mModule.mContext.getClass());
                        Logger.d(TAG, "context.getClass: " + mModule.mContext.getClass());
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
                        mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("openNotification", map);
                    }

(AaronHero) #28

我按照您说的运行了,但都没有执行,

                } catch (Exception e) {
                    e.printStackTrace();
                    Logger.i(TAG, "Try to start application");
                    if (mRAC != null) {
                        try {
                            Intent intent = new Intent();
                            intent.setClassName(mRAC.getPackageName(), mRAC.getPackageName() + ".MainActivity");
                            intent.putExtras(bundle);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            context.startActivity(intent);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                            Logger.i(TAG, "Cannot find MainActivity, will discard onClick event.");
                        }

                    }
                }

log中打印了 Logger.i(TAG, “Try to start application”)这句话


(KenChoi) #29

之前的这句你没删掉吧?

mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("openNotification", map);


(AaronHero) #30

您好,首先非常感谢在百忙中为我解惑。现在程序能够拉起应用了,不过我试了试跳转指定界面,感觉有个问题需要处理一下,就是应用在未启动时,点击通知会直接跳到指定页面,返回后直接退出了应用,怎样先拉起应用,在跳转界面?


(KenChoi) #31

你先打开 MainActivity,然后再 index.android.js 中接收事件,然后跳转到指定界面:

// map 中增加跳转页面的键值对,比如
map.putString("jumpTo", "second_page");
if (!isApplicationRunning(context) || mModule.mContext == null) {
                        Intent intent = new Intent();
                        intent.setClassName(context.getPackageName(), context.getPackageName() + ".MainActivity");
                        intent.putExtras(bundle);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(intent);
                        new ReactApplicationContext(context).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("openNotification", map);

index.android.js

componentDidMount() {
    JPushModule.addReceiveOpenNotificationListener((map) => {
        console.log("Opening notification!");
        console.log("map.extra: " + map.key);
        var page = map.jumpTo;
        if (page != undefined) {
          // jump to second page
        }
    });
}

(AaronHero) #32

郁闷了,
04-21 10:40:00.182 981-1444/com.mydemo D/PushProtocol: init m_nSocket - -5
04-21 10:40:00.482 981-1444/com.mydemo W/JIGUANG-JCore: [ConnectingHelper] Login failed with return code:-997有时遇到这种情况就不能收到通知了。


#33

网络有问题,登录失败自然收不到。


(AaronHero) #34
new ReactApplicationContext(context).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("openNotification", map);

mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("openNotification", map);

是不是差距比较大?当我用第一种的时候都不会调用openNotificationListener方法,当用第二种的时候能调用,但程序未启动时打开通知,只能拉起界面,并不能执行openNotificationListener方法。


(KenChoi) #35

你判断一下 mRAC 是否为空就行了,如果不为空你就直接发送事件,如果为空就试着这样

mModule = new JPushModule((ReactApplicationContext) context);
...

然后再发送。