Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复应用在热启动的情况下,点击推送无法触发addNotificationListener的问题 #794

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions android/src/main/java/cn/jiguang/plugins/push/JPushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
Expand All @@ -27,16 +27,20 @@
import cn.jiguang.plugins.push.receiver.JPushBroadcastReceiver;
import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.data.JPushLocalNotification;

public class JPushModule extends ReactContextBaseJavaModule {
public class JPushModule extends ReactContextBaseJavaModule implements LifecycleEventListener {

public static ReactApplicationContext reactContext;

public static boolean isAppForeground = false;

public static NotificationMessage notificationMessage = null;

public JPushModule(ReactApplicationContext reactApplicationContext) {
super(reactContext);
reactApplicationContext.addLifecycleEventListener(this);
reactContext = reactApplicationContext;
}

Expand All @@ -59,6 +63,11 @@ public void init() {
JPushHelper.sendEvent(JConstants.NOTIFICATION_EVENT, writableMap);
JPushBroadcastReceiver.NOTIFICATION_BUNDLE = null;
}
if (JPushModule.notificationMessage != null) {
WritableMap writableMap = JPushHelper.convertNotificationToMap(JConstants.NOTIFICATION_OPENED, notificationMessage);
JPushHelper.sendEvent(JConstants.NOTIFICATION_EVENT, writableMap);
JPushModule.notificationMessage = null;
}
}

@ReactMethod
Expand Down Expand Up @@ -371,7 +380,7 @@ public void addLocalNotification(ReadableMap readableMap) {
return;
}
String notificationID = readableMap.getString(JConstants.MESSAGE_ID);
if(notificationID==null || TextUtils.isEmpty(notificationID)){
if (notificationID == null || TextUtils.isEmpty(notificationID)) {
JLogger.w(JConstants.PARAMS_ILLEGAL);
return;
}
Expand All @@ -398,7 +407,7 @@ public void removeLocalNotification(ReadableMap readableMap) {
}
if (readableMap.hasKey(JConstants.MESSAGE_ID)) {
String notificationID = readableMap.getString(JConstants.MESSAGE_ID);
if(notificationID==null || TextUtils.isEmpty(notificationID)){
if (notificationID == null || TextUtils.isEmpty(notificationID)) {
JLogger.w(JConstants.PARAMS_ILLEGAL);
return;
}
Expand Down Expand Up @@ -462,33 +471,33 @@ public void deleteGeofence(ReadableMap readableMap) {
}

@ReactMethod
public void clearAllNotifications(){
public void clearAllNotifications() {
JPushInterface.clearAllNotifications(reactContext);
}

@ReactMethod
public void clearNotificationById(ReadableMap readableMap){
if (readableMap == null){
public void clearNotificationById(ReadableMap readableMap) {
if (readableMap == null) {
JLogger.w(JConstants.PARAMS_NULL);
return;
}
if (readableMap.hasKey(JConstants.NOTIFICATION_ID)){
if (readableMap.hasKey(JConstants.NOTIFICATION_ID)) {
Integer id = readableMap.getInt(JConstants.NOTIFICATION_ID);
JPushInterface.clearNotificationById(reactContext,id);
}else {
JPushInterface.clearNotificationById(reactContext, id);
} else {
JLogger.w("there are no " + JConstants.GEO_FENCE_ID);
}
}

@ReactMethod
public void setPowerSaveMode(boolean bool){
JPushInterface.setPowerSaveMode(reactContext,bool);
public void setPowerSaveMode(boolean bool) {
JPushInterface.setPowerSaveMode(reactContext, bool);
}

@ReactMethod
public void isNotificationEnabled(Callback callback){
public void isNotificationEnabled(Callback callback) {
Integer isEnabled = JPushInterface.isNotificationEnabled(reactContext);
if (callback == null){
if (callback == null) {
JLogger.w(JConstants.CALLBACK_NULL);
return;
}
Expand Down Expand Up @@ -538,4 +547,26 @@ public void onActivityDestroyed(Activity activity) {
});
}

@Override
public void onHostResume() {

}

@Override
public void onHostPause() {

}

@Override
public void onHostDestroy() {
JLogger.d("onHostDestroy");
notificationMessage = null;
}

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
JLogger.d("onCatalystInstanceDestroy");
notificationMessage = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void onNotifyMessageOpened(Context context, NotificationMessage notificat
JLogger.d("onNotifyMessageOpened:" + notificationMessage.toString());
if (JPushModule.reactContext != null) {
if (!JPushModule.isAppForeground) JPushHelper.launchApp(context);
JPushModule.notificationMessage = notificationMessage;
WritableMap writableMap = JPushHelper.convertNotificationToMap(JConstants.NOTIFICATION_OPENED, notificationMessage);
JPushHelper.sendEvent(JConstants.NOTIFICATION_EVENT, writableMap);
} else {
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export default class JPush {
/**
* 自定义消息事件
*/
static addCustomMessagegListener(
static addCustomMessageListener(
callback: Callback<{
/**
* 唯一标识自定义消息的 ID
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ export default class JPush {
* extras:对应 Portal 推送消息界面上的“可选设置”里的附加字段
*
* */
static addCustomMessagegListener(callback) {
static addCustomMessageListener(callback) {
listeners[callback] = DeviceEventEmitter.addListener(
CustomMessageEvent, result => {
callback(result)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "jpush-react-native",
"name": "react-native-mjpush",
"description": "React Native JPush component for Android and iOS",
"homepage":"https://github.com/jpush/jpush-react-native",
"homepage":"https://github.com/bashen1/jpush-react-native",
"main": "index.js",
"types": "index.d.ts",
"license": "ISC",
"author": "wicked.tc130",
"version": "2.8.1",
"version": "2.8.2",
"repository": {
"type": "git",
"url": "https://github.com/jpush/jpush-react-native"
"url": "https://github.com/bashen1/jpush-react-native"
},
"keywords": [
"react-native",
Expand Down