diff --git a/android/.settings/org.eclipse.buildship.core.prefs b/android/.settings/org.eclipse.buildship.core.prefs index f6d838db..6aa97a90 100644 --- a/android/.settings/org.eclipse.buildship.core.prefs +++ b/android/.settings/org.eclipse.buildship.core.prefs @@ -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 diff --git a/android/build.gradle b/android/build.gradle index 8ddc0645..15b5a7cb 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -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" } diff --git a/android/libs/jpush-android-3.1.6.jar b/android/libs/jpush-android-3.1.6.jar deleted file mode 100755 index daf97207..00000000 Binary files a/android/libs/jpush-android-3.1.6.jar and /dev/null differ diff --git a/android/libs/jpush-android-3.1.7.jar b/android/libs/jpush-android-3.1.7.jar new file mode 100755 index 00000000..bf0a1700 Binary files /dev/null and b/android/libs/jpush-android-3.1.7.jar differ diff --git a/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java b/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java index 6be2b11e..715efe42 100644 --- a/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java +++ b/android/src/main/java/cn/jpush/reactnativejpush/JPushModule.java @@ -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; @@ -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; @@ -123,6 +130,7 @@ public void getInfo(Callback successCallback) { successCallback.invoke(map); } + @ReactMethod public void stopPush() { mContext = getCurrentActivity(); @@ -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(); @@ -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; + } } diff --git a/documents/api.md b/documents/api.md index 4b911931..ec747481 100644 --- a/documents/api.md +++ b/documents/api.md @@ -18,6 +18,7 @@ * [点击推送事件](#点击推送事件) * [接收推送事件](#接收推送事件) * [接收自定义消息事件](#接收自定义消息事件) + * [hasPermission](#haspermission) * [iOS Only API](#ios-only-api) * [setupPush](#setuppush) * [setBadge](#setbadge) @@ -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) @@ -285,6 +285,17 @@ JPushModule.clearNotificationById(notificationId); JPushModule.removeReceiveCustomMsgListener(callback); ``` + +#### hasPermission + +获取应用是否有推送权限。 + +``` +JPushModule.hasPermission( res => { + // res = boolen +}) +``` + ### iOS Only API #### setupPush @@ -356,16 +367,6 @@ JPushModule.getLaunchAppNotification( notification => { }) ``` -#### hasPermission - -获取应用是否有推送权限。 - -``` -JPushModule.hasPermission( res => { - // res = boolen -}) -``` - #### 点击推送启动应用事件 **NOTE**: iOS 需要安装到 jpush-react-native@2.0.0+ 。 diff --git a/documents/api_en.md b/documents/api_en.md index 96db2430..9e74ff74 100644 --- a/documents/api_en.md +++ b/documents/api_en.md @@ -17,6 +17,7 @@ * [clearLocalNotifications](#clearlocalnotifications) * [clearAllNotifications](#clearallnotifications) * [clearNotificationById](#clearnotificationbyId) + * [hasPermission](#haspermission) * [iOS Only API](#ios-only-api) * [setupPush](#setuppush) * [setBadge](#setbadge) @@ -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. diff --git a/example/package.json b/example/package.json index 1e769dca..c325a89c 100644 --- a/example/package.json +++ b/example/package.json @@ -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", diff --git a/index.js b/index.js index fbe43c7c..26401dc2 100644 --- a/index.js +++ b/index.js @@ -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) + }) } /** diff --git a/package.json b/package.json index 8c3784c4..1d4452dc 100644 --- a/package.json +++ b/package.json @@ -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": {