From 5f93fc3f499ed43b0b812a377a8561389156f71d Mon Sep 17 00:00:00 2001 From: Hevin Date: Wed, 13 Jul 2016 23:07:01 +0800 Subject: [PATCH] Update API to don't depend support lib. --- README.md | 4 +- package.json | 2 +- plugin.xml | 2 - src/android/JPushPlugin.java | 76 +++++++++++++++++++++++++----------- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 518eee7..c31f5b3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.org/jpush/jpush-phonegap-plugin.svg?branch=master)](https://travis-ci.org/jpush/jpush-phonegap-plugin) [![QQ Group](https://img.shields.io/badge/QQ%20Group-413602425-red.svg)]() -[![release](https://img.shields.io/badge/release-2.2.1-blue.svg)](https://github.com/jpush/jpush-phonegap-plugin/releases) +[![release](https://img.shields.io/badge/release-2.2.3-blue.svg)](https://github.com/jpush/jpush-phonegap-plugin/releases) [![platforms](https://img.shields.io/badge/platforms-iOS%7CAndroid-lightgrey.svg)](https://github.com/jpush/jpush-phonegap-plugin) [![weibo](https://img.shields.io/badge/weibo-JPush-blue.svg)](http://weibo.com/jpush?refer_flag=1001030101_&is_all=1) @@ -75,8 +75,6 @@ Cordova CLI 的具体用法可参考 [Cordova CLI 官方文档](https://cordova. 解决方案:Eclipse 中右键单击工程名,Build Path -> Config Build Path -> Projects -> 选中工程名称 -> CordovaLib -> 点击 add。 -- 最新版插件依赖 android-support-v4:24.0.0 jar 包,需要及时更新 *Android Support Repository*。 - ### 2. iOS - 收不到推送: diff --git a/package.json b/package.json index af0e59f..745725f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jpush-phonegap-plugin", - "version": "2.2.3", + "version": "2.2.4", "description": "JPush for cordova plugin", "cordova": { "id": "jpush-phonegap-plugin", diff --git a/plugin.xml b/plugin.xml index 3464037..289fe3d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -58,8 +58,6 @@ - - diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index ef65ef1..c68f70b 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -1,8 +1,11 @@ package cn.jpush.phonegap; +import android.annotation.TargetApi; import android.app.Activity; +import android.app.AppOpsManager; import android.content.Context; -import android.support.v4.app.NotificationManagerCompat; +import android.content.pm.ApplicationInfo; +import android.os.Build; import android.text.TextUtils; import android.util.Log; @@ -14,6 +17,8 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashMap; @@ -138,7 +143,7 @@ public class JPushPlugin extends CordovaPlugin { } private static JSONObject getMessageObject(String message, - Map extras) { + Map extras) { JSONObject data = new JSONObject(); try { data.put("message", message); @@ -172,7 +177,7 @@ public class JPushPlugin extends CordovaPlugin { } private static JSONObject getNotificationObject(String title, - String alert, Map extras) { + String alert, Map extras) { JSONObject data = new JSONObject(); try { data.put("title", title); @@ -180,7 +185,7 @@ public class JPushPlugin extends CordovaPlugin { JSONObject jExtras = new JSONObject(); for (Entry entry : extras.entrySet()) { if (entry.getKey().equals("cn.jpush.android.EXTRA")) { - JSONObject jo = null; + JSONObject jo; if (TextUtils.isEmpty((String) entry.getValue())) { jo = new JSONObject(); } else { @@ -222,7 +227,7 @@ public class JPushPlugin extends CordovaPlugin { } static void transmitNotificationOpen(String title, String alert, - Map extras) { + Map extras) { if (instance == null) { return; } @@ -240,7 +245,7 @@ public class JPushPlugin extends CordovaPlugin { } static void transmitNotificationReceive(String title, String alert, - Map extras) { + Map extras) { if (instance == null) { return; } @@ -259,7 +264,7 @@ public class JPushPlugin extends CordovaPlugin { @Override public boolean execute(final String action, final JSONArray data, - final CallbackContext callbackContext) throws JSONException { + final CallbackContext callbackContext) throws JSONException { if (!methodList.contains(action)) { return false; } @@ -314,13 +319,11 @@ public class JPushPlugin extends CordovaPlugin { } void areNotificationEnabled(JSONArray data, final CallbackContext callback) { - NotificationManagerCompat nmc = NotificationManagerCompat.from( - cordova.getActivity().getApplicationContext()); int isEnabled; - if (nmc.areNotificationsEnabled()) { + if (hasPermission("OP_POST_NOTIFICATION")) { isEnabled = 1; } else { - isEnabled = 0; + isEnabled = 0; } callback.success(isEnabled); } @@ -436,7 +439,7 @@ public class JPushPlugin extends CordovaPlugin { } void setBasicPushNotificationBuilder(JSONArray data, - CallbackContext callbackContext) { + CallbackContext callbackContext) { BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder( this.cordova.getActivity()); builder.developerArg0 = "Basic builder 1"; @@ -450,22 +453,16 @@ public class JPushPlugin extends CordovaPlugin { } /** - * 自定义推送通知栏样式,需要自己实现具体代码。 - * http://docs.jiguang.cn/client/android_tutorials/#_11 - */ + * 自定义推送通知栏样式,需要自己实现具体代码。 + * http://docs.jiguang.cn/client/android_tutorials/#_11 + */ void setCustomPushNotificationBuilder(JSONArray data, - CallbackContext callbackContext) { + CallbackContext callbackContext) { // CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder( // this.cordova.getActivity(), R.layout.test_notification_layout, // R.id.icon, R.id.title, R.id.text); - // builder.developerArg0 = "Custom Builder 1"; // JPushInterface.setPushNotificationBuilder(2, builder); - // JSONObject obj = new JSONObject(); - // try { - // obj.put("id", 2); - // } catch (JSONException e) { - // e.printStackTrace(); - // } + // JPushInterface.setDefaultPushNotificationBuilder(builder); } void clearAllNotification(JSONArray data, CallbackContext callbackContext) { @@ -600,4 +597,37 @@ public class JPushPlugin extends CordovaPlugin { } }; + @TargetApi(Build.VERSION_CODES.KITKAT) + private boolean hasPermission(String appOpsServiceId) { + Context context = cordova.getActivity().getApplicationContext(); + AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); + ApplicationInfo appInfo = context.getApplicationInfo(); + + String pkg = context.getPackageName(); + int uid = appInfo.uid; + Class appOpsClazz = null; + + 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 true; + } + }