Skip to content

Commit bc8b224

Browse files
authored
Merge pull request #563 from jpush/dev
Dev
2 parents 3efcdb3 + 77fdde1 commit bc8b224

File tree

10 files changed

+86
-24
lines changed

10 files changed

+86
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#Wed May 30 14:17:39 CST 2018
2-
connection.project.dir=../../../android
1+
connection.project.dir=../example/android
2+
eclipse.preferences.version=1

android/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 23
4+
compileSdkVersion 27
55
buildToolsVersion '26.0.2'
66

77
defaultConfig {
88
minSdkVersion 16
9-
targetSdkVersion 22
9+
targetSdkVersion 27
1010
versionCode 1
1111
versionName "1.0"
1212
}

android/libs/jpush-android-3.1.6.jar

-132 KB
Binary file not shown.

android/libs/jpush-android-3.1.7.jar

136 KB
Binary file not shown.

android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java

+54
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
import android.app.Activity;
44
import android.app.ActivityManager;
5+
import android.app.AppOpsManager;
56
import android.app.Notification;
7+
import android.app.NotificationManager;
68
import android.content.BroadcastReceiver;
79
import android.content.ComponentName;
810
import android.content.Context;
911
import android.content.Intent;
12+
import android.content.pm.ApplicationInfo;
13+
import android.os.Build;
1014
import android.os.Bundle;
1115
import android.util.SparseArray;
1216

@@ -26,6 +30,9 @@
2630

2731
import org.json.JSONObject;
2832

33+
import java.lang.reflect.Field;
34+
import java.lang.reflect.InvocationTargetException;
35+
import java.lang.reflect.Method;
2936
import java.text.SimpleDateFormat;
3037
import java.util.Date;
3138
import java.util.HashSet;
@@ -123,6 +130,7 @@ public void getInfo(Callback successCallback) {
123130
successCallback.invoke(map);
124131
}
125132

133+
126134
@ReactMethod
127135
public void stopPush() {
128136
mContext = getCurrentActivity();
@@ -131,6 +139,13 @@ public void stopPush() {
131139
Logger.toast(mContext, "Stop push success");
132140
}
133141

142+
@ReactMethod
143+
public void hasPermission(Callback callback) {
144+
callback.invoke(hasPermission("OP_POST_NOTIFICATION"));
145+
}
146+
147+
148+
134149
@ReactMethod
135150
public void resumePush() {
136151
mContext = getCurrentActivity();
@@ -760,4 +775,43 @@ public void finishActivity() {
760775
}
761776

762777
}
778+
779+
private boolean hasPermission(String appOpsServiceId) {
780+
781+
Context context = getCurrentActivity().getApplicationContext();
782+
if (Build.VERSION.SDK_INT >= 24) {
783+
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(
784+
Context.NOTIFICATION_SERVICE);
785+
return mNotificationManager.areNotificationsEnabled();
786+
}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
787+
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
788+
ApplicationInfo appInfo = context.getApplicationInfo();
789+
790+
String pkg = context.getPackageName();
791+
int uid = appInfo.uid;
792+
Class appOpsClazz;
793+
794+
try {
795+
appOpsClazz = Class.forName(AppOpsManager.class.getName());
796+
Method checkOpNoThrowMethod = appOpsClazz.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE,
797+
String.class);
798+
Field opValue = appOpsClazz.getDeclaredField(appOpsServiceId);
799+
int value = opValue.getInt(Integer.class);
800+
Object result = checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg);
801+
return Integer.parseInt(result.toString()) == AppOpsManager.MODE_ALLOWED;
802+
} catch (InvocationTargetException e) {
803+
e.printStackTrace();
804+
} catch (IllegalAccessException e) {
805+
e.printStackTrace();
806+
} catch (NoSuchMethodException e) {
807+
e.printStackTrace();
808+
} catch (NoSuchFieldException e) {
809+
e.printStackTrace();
810+
} catch (ClassNotFoundException e) {
811+
e.printStackTrace();
812+
}
813+
}
814+
815+
return false;
816+
}
763817
}

documents/api.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [点击推送事件](#点击推送事件)
1919
* [接收推送事件](#接收推送事件)
2020
* [接收自定义消息事件](#接收自定义消息事件)
21+
* [hasPermission](#haspermission)
2122
* [iOS Only API](#ios-only-api)
2223
* [setupPush](#setuppush)
2324
* [setBadge](#setbadge)
@@ -26,7 +27,6 @@
2627
* [getLaunchAppNotification](#getlaunchappnotification)
2728
* [点击推送启动应用事件](#open-notification-launch-app-event)
2829
* [网络成功登陆事件](#network-did-login-event)
29-
* [hasPermission](#haspermission)
3030
* [Android Only API](#android-only-api)
3131
* [crashLogOFF](#crashlogoff)
3232
* [crashLogON](#crashlogno)
@@ -285,6 +285,17 @@ JPushModule.clearNotificationById(notificationId);
285285
JPushModule.removeReceiveCustomMsgListener(callback);
286286
```
287287

288+
289+
#### hasPermission
290+
291+
获取应用是否有推送权限。
292+
293+
```
294+
JPushModule.hasPermission( res => {
295+
// res = boolen
296+
})
297+
```
298+
288299
### iOS Only API
289300

290301
#### setupPush
@@ -356,16 +367,6 @@ JPushModule.getLaunchAppNotification( notification => {
356367
})
357368
```
358369

359-
#### hasPermission
360-
361-
获取应用是否有推送权限。
362-
363-
```
364-
JPushModule.hasPermission( res => {
365-
// res = boolen
366-
})
367-
```
368-
369370
#### 点击推送启动应用事件
370371

371372
**NOTE**: iOS 需要安装到 jpush-react-native@2.0.0+ 。

documents/api_en.md

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [clearLocalNotifications](#clearlocalnotifications)
1818
* [clearAllNotifications](#clearallnotifications)
1919
* [clearNotificationById](#clearnotificationbyId)
20+
* [hasPermission](#haspermission)
2021
* [iOS Only API](#ios-only-api)
2122
* [setupPush](#setuppush)
2223
* [setBadge](#setbadge)
@@ -240,6 +241,16 @@ JPushModule.clearNotificationById(id);
240241
JPushModule.removeReceiveCustomMsgListener(callback)
241242
```
242243

244+
#### hasPermission
245+
246+
获取应用是否有推送权限。
247+
248+
```
249+
JPushModule.hasPermission( res => {
250+
// res = boolen
251+
})
252+
```
253+
243254
### iOS Only API
244255

245256
All apis can find in jpush-react-native/index.js.

example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "jest"
99
},
1010
"dependencies": {
11-
"jcore-react-native": "^1.2.10",
11+
"jcore-react-native": "^1.2.12",
1212
"jpush-react-native": "file:..",
1313
"react": "16.2.0",
1414
"react-native": "0.52.0",

index.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,15 @@ export default class JPush {
3636
}
3737

3838

39-
4039
/**
41-
* iOS Only
4240
* 判断是否成功授权推送(或是否在设置中成功开启推送功能)
4341
*
4442
* @param {Function} cb
4543
*/
4644
static hasPermission (cb) {
47-
if (Platform.OS == "ios") {
48-
JPushModule.hasPermission(res => {
49-
cb(res)
50-
})
51-
}
45+
JPushModule.hasPermission(res => {
46+
cb(res)
47+
})
5248
}
5349

5450
/**

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jpush-react-native",
3-
"version": "2.2.13",
3+
"version": "2.3.0",
44
"description": "a jpush plugin for react native application",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)