Skip to content

Commit

Permalink
Merge pull request #563 from jpush/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JoshLipan authored Nov 16, 2018
2 parents 3efcdb3 + 77fdde1 commit bc8b224
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 24 deletions.
4 changes: 2 additions & 2 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Wed May 30 14:17:39 CST 2018
connection.project.dir=../../../android
connection.project.dir=../example/android
eclipse.preferences.version=1
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
compileSdkVersion 27
buildToolsVersion '26.0.2'

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand Down
Binary file removed android/libs/jpush-android-3.1.6.jar
Binary file not shown.
Binary file added android/libs/jpush-android-3.1.7.jar
Binary file not shown.
54 changes: 54 additions & 0 deletions android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.SparseArray;

Expand All @@ -26,6 +30,9 @@

import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
Expand Down Expand Up @@ -123,6 +130,7 @@ public void getInfo(Callback successCallback) {
successCallback.invoke(map);
}


@ReactMethod
public void stopPush() {
mContext = getCurrentActivity();
Expand All @@ -131,6 +139,13 @@ public void stopPush() {
Logger.toast(mContext, "Stop push success");
}

@ReactMethod
public void hasPermission(Callback callback) {
callback.invoke(hasPermission("OP_POST_NOTIFICATION"));
}



@ReactMethod
public void resumePush() {
mContext = getCurrentActivity();
Expand Down Expand Up @@ -760,4 +775,43 @@ public void finishActivity() {
}

}

private boolean hasPermission(String appOpsServiceId) {

Context context = getCurrentActivity().getApplicationContext();
if (Build.VERSION.SDK_INT >= 24) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
return mNotificationManager.areNotificationsEnabled();
}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();

String pkg = context.getPackageName();
int uid = appInfo.uid;
Class appOpsClazz;

try {
appOpsClazz = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClazz.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE,
String.class);
Field opValue = appOpsClazz.getDeclaredField(appOpsServiceId);
int value = opValue.getInt(Integer.class);
Object result = checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg);
return Integer.parseInt(result.toString()) == AppOpsManager.MODE_ALLOWED;
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

return false;
}
}
23 changes: 12 additions & 11 deletions documents/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [点击推送事件](#点击推送事件)
* [接收推送事件](#接收推送事件)
* [接收自定义消息事件](#接收自定义消息事件)
* [hasPermission](#haspermission)
* [iOS Only API](#ios-only-api)
* [setupPush](#setuppush)
* [setBadge](#setbadge)
Expand All @@ -26,7 +27,6 @@
* [getLaunchAppNotification](#getlaunchappnotification)
* [点击推送启动应用事件](#open-notification-launch-app-event)
* [网络成功登陆事件](#network-did-login-event)
* [hasPermission](#haspermission)
* [Android Only API](#android-only-api)
* [crashLogOFF](#crashlogoff)
* [crashLogON](#crashlogno)
Expand Down Expand Up @@ -285,6 +285,17 @@ JPushModule.clearNotificationById(notificationId);
JPushModule.removeReceiveCustomMsgListener(callback);
```


#### hasPermission

获取应用是否有推送权限。

```
JPushModule.hasPermission( res => {
// res = boolen
})
```

### iOS Only API

#### setupPush
Expand Down Expand Up @@ -356,16 +367,6 @@ JPushModule.getLaunchAppNotification( notification => {
})
```

#### hasPermission

获取应用是否有推送权限。

```
JPushModule.hasPermission( res => {
// res = boolen
})
```

#### 点击推送启动应用事件

**NOTE**: iOS 需要安装到 [email protected]+ 。
Expand Down
11 changes: 11 additions & 0 deletions documents/api_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [clearLocalNotifications](#clearlocalnotifications)
* [clearAllNotifications](#clearallnotifications)
* [clearNotificationById](#clearnotificationbyId)
* [hasPermission](#haspermission)
* [iOS Only API](#ios-only-api)
* [setupPush](#setuppush)
* [setBadge](#setbadge)
Expand Down Expand Up @@ -240,6 +241,16 @@ JPushModule.clearNotificationById(id);
JPushModule.removeReceiveCustomMsgListener(callback)
```

#### hasPermission

获取应用是否有推送权限。

```
JPushModule.hasPermission( res => {
// res = boolen
})
```

### iOS Only API

All apis can find in jpush-react-native/index.js.
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "jest"
},
"dependencies": {
"jcore-react-native": "^1.2.10",
"jcore-react-native": "^1.2.12",
"jpush-react-native": "file:..",
"react": "16.2.0",
"react-native": "0.52.0",
Expand Down
10 changes: 3 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,15 @@ export default class JPush {
}



/**
* iOS Only
* 判断是否成功授权推送(或是否在设置中成功开启推送功能)
*
* @param {Function} cb
*/
static hasPermission (cb) {
if (Platform.OS == "ios") {
JPushModule.hasPermission(res => {
cb(res)
})
}
JPushModule.hasPermission(res => {
cb(res)
})
}

/**
Expand Down
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": "2.2.13",
"version": "2.3.0",
"description": "a jpush plugin for react native application",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit bc8b224

Please sign in to comment.