Skip to content

Commit

Permalink
Merge pull request #218 from jpush/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KenChoi1992 authored May 8, 2017
2 parents 809ccad + e4707f6 commit af7d6f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 61 deletions.
97 changes: 37 additions & 60 deletions android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package cn.jpush.reactnativejpush;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -23,26 +18,24 @@


import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import cn.jpush.android.data.JPushLocalNotification;

public class JPushModule extends ReactContextBaseJavaModule {

private static String TAG = "JPushModule";
private static int NOTIFICATION_BUILDER_ID = 0;
private Context mContext;
private static ReactApplicationContext mRAC;
private static JPushModule mModule;
private static CountDownLatch mLatch;

public JPushModule(ReactApplicationContext reactContext) {
super(reactContext);
mRAC = reactContext;
mLatch = new CountDownLatch(1);
}

@Override
Expand All @@ -58,13 +51,13 @@ public String getName() {
@Override
public void initialize() {
super.initialize();
mModule = this;
mLatch.countDown();
mRAC = getReactApplicationContext();
}

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
mModule = null;
}

@ReactMethod
Expand Down Expand Up @@ -193,12 +186,16 @@ public void gotResult(int status, String desc, Set<String> set) {
@ReactMethod
public void setStyleBasic() {
mContext = getCurrentActivity();
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(mContext);
builder.statusBarDrawable = IdHelper.getDrawable(mContext, "ic_launcher");
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL; //设置为点击后自动消失
builder.notificationDefaults = Notification.DEFAULT_SOUND; //设置为铃声( Notification.DEFAULT_SOUND)或者震动( Notification.DEFAULT_VIBRATE)
JPushInterface.setPushNotificationBuilder(1, builder);
Logger.toast(mContext, "Basic Builder - 1");
if (mContext != null) {
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(mContext);
builder.statusBarDrawable = IdHelper.getDrawable(mContext, "ic_launcher");
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL; //设置为点击后自动消失
builder.notificationDefaults = Notification.DEFAULT_SOUND; //设置为铃声( Notification.DEFAULT_SOUND)或者震动( Notification.DEFAULT_VIBRATE)
JPushInterface.setPushNotificationBuilder(1, builder);
Logger.toast(mContext, "Basic Builder - 1");
} else {
Logger.d(TAG, "Current activity is null, discard event");
}
}


Expand Down Expand Up @@ -277,30 +274,28 @@ public void onReceive(Context context, Intent data) {
map.putString("message", message);
map.putString("extras", extras);
Logger.i(TAG, "收到自定义消息: " + message);
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("receivePushMsg", map);
sendEvent("receivePushMsg", map, null);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(data.getAction())) {
try {
// 通知内容
String alertContent = bundle.getString(JPushInterface.EXTRA_ALERT);
// extra 字段的 json 字符串
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Logger.i(TAG, "收到推送下来的通知: " + alertContent);
if (!isApplicationRunning(context)) {
// HeadlessService 启动有问题,暂时弃用了
// if (!isApplicationRunning(context)) {
// HeadlessService 启动有问题,暂时弃用了
// Log.i(TAG, "应用尚未切换到前台运行过,启动 HeadlessService");
// Intent intent = new Intent(context, HeadlessService.class);
// intent.putExtra("data", bundle);
// context.startService(intent);
// HeadlessJsTaskService.acquireWakeLockNow(context);
// Save as local notification
// Start up application failed, will save notifications as local notifications.
}
// Save as local notification
// Start up application failed, will save notifications as local notifications.
// }
WritableMap map = Arguments.createMap();
map.putString("alertContent", alertContent);
map.putString("extras", extras);
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("receiveNotification", map);
sendEvent("receiveNotification", map, null);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -309,11 +304,6 @@ public void onReceive(Context context, Intent data) {
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(data.getAction())) {
try {
Logger.d(TAG, "用户点击打开了通知");
if (mRAC == null) {
Log.e(TAG, "mRAC is null!");
mModule = new JPushModule((ReactApplicationContext) context);
Log.d(TAG, "mRAC is null ? " + (mRAC == null));
}
// 通知内容
String alertContent = bundle.getString(JPushInterface.EXTRA_ALERT);
// extra 字段的 json 字符串
Expand Down Expand Up @@ -341,13 +331,7 @@ public void onReceive(Context context, Intent data) {
// Intent[] intents = {intent, detailIntent};
// 同时启动 MainActivity 以及 SecondActivity
// context.startActivities(intents);
if (mRAC != null) {
Log.e(TAG, "Passing openNotification event");
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("openNotification", map);
} else {
Log.e(TAG, "mRAC still null!");
}
sendEvent("openNotification", map, null);
} catch (Exception e) {
e.printStackTrace();
Logger.i(TAG, "Shouldn't access here");
Expand All @@ -359,8 +343,7 @@ public void onReceive(Context context, Intent data) {
String registrationId = data.getExtras().getString(JPushInterface.EXTRA_REGISTRATION_ID);
Logger.d(TAG, "注册成功, registrationId: " + registrationId);
try {
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("getRegistrationId", registrationId);
sendEvent("getRegistrationId", null, registrationId);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -370,26 +353,20 @@ public void onReceive(Context context, Intent data) {

}

private static boolean isApplicationRunning(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}

private static boolean isApplicationRunningBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
private static void sendEvent(String methodName, WritableMap map, String data) {
try {
mLatch.await();
if (mRAC != null) {
if (map != null) {
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(methodName, map);
} else {
mRAC.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(methodName, data);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jpush-react-native",
"version": "1.6.1",
"version": "1.6.2",
"description": "a jpush plugin for react native application",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit af7d6f8

Please sign in to comment.