Add new tag and alias methods

This commit is contained in:
Hevin 2017-09-19 17:48:22 +08:00
parent a27cc445ee
commit 3ce2babd9b
4 changed files with 421 additions and 91 deletions

View File

@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="jpush-phonegap-plugin"
version="3.2.3">
version="3.2.4">
<name>JPush</name>
<description>JPush for cordova plugin</description>
@ -192,10 +192,11 @@
<meta-data android:name="JPUSH_APPKEY" android:value="$APP_KEY" />
</config-file>
<source-file src="src/android/libs/jpush-android_v3.0.6.jar" target-dir="libs" />
<source-file src="src/android/libs/jpush-android-3.0.8.jar" target-dir="libs" />
<source-file src="src/android/MyReceiver.java" target-dir="src/cn/jiguang/cordova/push" />
<source-file src="src/android/JPushPlugin.java" target-dir="src/cn/jiguang/cordova/push" />
<source-file src="src/android/JPushEventReceiver.java" target-dir="src/cn/jiguang/cordova/push" />
<source-file src="src/android/res/drawable-hdpi/jpush_richpush_btn_selector.xml"
target-dir="res/drawable" />

View File

@ -0,0 +1,156 @@
package cn.jiguang.cordova.push;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Set;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.service.JPushMessageReceiver;
public class JPushEventReceiver extends JPushMessageReceiver {
private static final String TAG = JPushEventReceiver.class.getSimpleName();
@Override
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
super.onTagOperatorResult(context, jPushMessage);
JSONObject resultJson = new JSONObject();
int sequence = jPushMessage.getSequence();
try {
resultJson.put("sequence", sequence);
} catch (JSONException e) {
e.printStackTrace();
}
CallbackContext callback = JPushPlugin.eventCallbackMap.get(sequence);
if (callback == null) {
Log.i(TAG, "Unexpected error, callback is null!");
return;
}
if (jPushMessage.getErrorCode() == 0) { // success
Set<String> tags = jPushMessage.getTags();
JSONArray tagsJsonArr = new JSONArray();
for (String tag : tags) {
tagsJsonArr.put(tag);
}
try {
if (tagsJsonArr.length() != 0) {
resultJson.put("tags", tagsJsonArr);
}
} catch (JSONException e) {
e.printStackTrace();
}
callback.success(resultJson);
} else {
try {
resultJson.put("errorCode", jPushMessage.getErrorCode());
} catch (JSONException e) {
e.printStackTrace();
}
callback.error(resultJson);
}
JPushPlugin.eventCallbackMap.remove(sequence);
}
@Override
public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
super.onCheckTagOperatorResult(context, jPushMessage);
JSONObject resultJson = new JSONObject();
int sequence = jPushMessage.getSequence();
try {
resultJson.put("sequence", sequence);
} catch (JSONException e) {
e.printStackTrace();
}
CallbackContext callback = JPushPlugin.eventCallbackMap.get(sequence);
if (callback == null) {
Log.i(TAG, "Unexpected error, callback is null!");
return;
}
if (jPushMessage.getErrorCode() == 0) {
try {
resultJson.put("tag", jPushMessage.getCheckTag());
resultJson.put("checkResult", jPushMessage.getTagCheckStateResult());
} catch (JSONException e) {
e.printStackTrace();
}
} else {
try {
resultJson.put("errorCode", jPushMessage.getErrorCode());
} catch (JSONException e) {
e.printStackTrace();
}
callback.error(resultJson);
}
JPushPlugin.eventCallbackMap.remove(sequence);
}
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
super.onAliasOperatorResult(context, jPushMessage);
JSONObject resultJson = new JSONObject();
int sequence = jPushMessage.getSequence();
try {
resultJson.put("sequence", sequence);
} catch (JSONException e) {
e.printStackTrace();
}
CallbackContext callback = JPushPlugin.eventCallbackMap.get(sequence);
if (callback == null) {
Log.i(TAG, "Unexpected error, callback is null!");
return;
}
if (jPushMessage.getErrorCode() == 0) { // success
try {
if (!TextUtils.isEmpty(jPushMessage.getAlias())) {
resultJson.put("alias", jPushMessage.getAlias());
}
} catch (JSONException e) {
e.printStackTrace();
}
callback.success(resultJson);
} else {
try {
resultJson.put("errorCode", jPushMessage.getErrorCode());
} catch (JSONException e) {
e.printStackTrace();
}
callback.error(resultJson);
}
JPushPlugin.eventCallbackMap.remove(sequence);
}
}

View File

@ -35,53 +35,29 @@ import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import cn.jpush.android.data.JPushLocalNotification;
import cn.jpush.android.service.JPushMessageReceiver;
public class JPushPlugin extends CordovaPlugin {
private final static List<String> methodList =
Arrays.asList(
"addLocalNotification",
"areNotificationEnabled",
"clearAllNotification",
"clearLocalNotifications",
"clearNotificationById",
"getNotification",
"getRegistrationID",
"init",
"isPushStopped",
"onPause",
"onResume",
"requestPermission",
"removeLocalNotification",
"reportNotificationOpened",
"resumePush",
"setAlias",
"setBasicPushNotificationBuilder",
"setCustomPushNotificationBuilder",
"setDebugMode",
"setLatestNotificationNum",
"setPushTime",
"setTags",
"setTagsWithAlias",
"setSilenceTime",
"setStatisticsOpen",
"stopPush"
);
private ExecutorService threadPool = Executors.newFixedThreadPool(1);
private Context mContext;
private static JPushPlugin instance;
private static Activity cordovaActivity;
private static String TAG = "JPushPlugin";
private static boolean shouldCacheMsg = false;
private static boolean isStatisticsOpened = false; // 是否开启统计分析功能
public static String notificationTitle;
public static String notificationAlert;
public static Map<String, Object> notificationExtras = new HashMap<String, Object>();
static String notificationTitle;
static String notificationAlert;
static Map<String, Object> notificationExtras = new HashMap<String, Object>();
public static String openNotificationTitle;
public static String openNotificationAlert;
public static Map<String, Object> openNotificationExtras = new HashMap<String, Object>();
static String openNotificationTitle;
static String openNotificationAlert;
static Map<String, Object> openNotificationExtras = new HashMap<String, Object>();
static Map<Integer, CallbackContext> eventCallbackMap = new HashMap<Integer, CallbackContext>();
public JPushPlugin() {
instance = this;
@ -89,10 +65,10 @@ public class JPushPlugin extends CordovaPlugin {
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
Log.i(TAG, "JPush initialize.");
super.initialize(cordova, webView);
JPushInterface.init(cordova.getActivity().getApplicationContext());
mContext = cordova.getActivity().getApplicationContext();
JPushInterface.init(mContext);
cordovaActivity = cordova.getActivity();
@ -110,17 +86,12 @@ public class JPushPlugin extends CordovaPlugin {
}
public void onPause(boolean multitasking) {
Log.i(TAG, "---------------- onPause");
shouldCacheMsg = true;
if (isStatisticsOpened && multitasking) {
JPushInterface.onPause(this.cordova.getActivity());
}
}
public void onResume(boolean multitasking) {
shouldCacheMsg = false;
Log.i(TAG, "---------------- onResume" + "-" + openNotificationAlert
+ "-" + notificationAlert);
if (isStatisticsOpened && multitasking) {
JPushInterface.onResume(this.cordova.getActivity());
}
@ -142,15 +113,14 @@ public class JPushPlugin extends CordovaPlugin {
instance = null;
}
private static JSONObject getMessageObject(String message,
Map<String, Object> extras) {
private static JSONObject getMessageObject(String message, Map<String, Object> extras) {
JSONObject data = new JSONObject();
try {
data.put("message", message);
JSONObject jExtras = new JSONObject();
for (Entry<String, Object> 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 {
@ -176,8 +146,7 @@ public class JPushPlugin extends CordovaPlugin {
return data;
}
private static JSONObject getNotificationObject(String title,
String alert, Map<String, Object> extras) {
private static JSONObject getNotificationObject(String title, String alert, Map<String, Object> extras) {
JSONObject data = new JSONObject();
try {
data.put("title", title);
@ -226,8 +195,7 @@ public class JPushPlugin extends CordovaPlugin {
});
}
static void transmitNotificationOpen(String title, String alert,
Map<String, Object> extras) {
static void transmitNotificationOpen(String title, String alert, Map<String, Object> extras) {
if (instance == null) {
return;
}
@ -244,8 +212,7 @@ public class JPushPlugin extends CordovaPlugin {
JPushPlugin.openNotificationAlert = null;
}
static void transmitNotificationReceive(String title, String alert,
Map<String, Object> extras) {
static void transmitNotificationReceive(String title, String alert, Map<String, Object> extras) {
if (instance == null) {
return;
}
@ -285,9 +252,6 @@ public class JPushPlugin extends CordovaPlugin {
@Override
public boolean execute(final String action, final JSONArray data,
final CallbackContext callbackContext) throws JSONException {
if (!methodList.contains(action)) {
return false;
}
threadPool.execute(new Runnable() {
@Override
public void run() {
@ -304,7 +268,7 @@ public class JPushPlugin extends CordovaPlugin {
}
void init(JSONArray data, CallbackContext callbackContext) {
JPushInterface.init(this.cordova.getActivity().getApplicationContext());
JPushInterface.init(mContext);
}
void setDebugMode(JSONArray data, CallbackContext callbackContext) {
@ -319,18 +283,17 @@ public class JPushPlugin extends CordovaPlugin {
}
void stopPush(JSONArray data, CallbackContext callbackContext) {
JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext());
JPushInterface.stopPush(mContext);
callbackContext.success();
}
void resumePush(JSONArray data, CallbackContext callbackContext) {
JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext());
JPushInterface.resumePush(mContext);
callbackContext.success();
}
void isPushStopped(JSONArray data, CallbackContext callbackContext) {
boolean isStopped = JPushInterface.isPushStopped(
this.cordova.getActivity().getApplicationContext());
boolean isStopped = JPushInterface.isPushStopped(mContext);
if (isStopped) {
callbackContext.success(1);
} else {
@ -358,7 +321,7 @@ public class JPushPlugin extends CordovaPlugin {
}
if (num != -1) {
JPushInterface.setLatestNotificationNumber(
this.cordova.getActivity().getApplicationContext(), num);
mContext, num);
} else {
callbackContext.error("error num");
}
@ -384,13 +347,13 @@ public class JPushPlugin extends CordovaPlugin {
} catch (JSONException e) {
callbackContext.error("error reading hour json");
}
Context context = this.cordova.getActivity().getApplicationContext();
Context context = mContext;
JPushInterface.setPushTime(context, days, startHour, endHour);
callbackContext.success();
}
void getRegistrationID(JSONArray data, CallbackContext callbackContext) {
Context context = this.cordova.getActivity().getApplicationContext();
Context context = mContext;
String regID = JPushInterface.getRegistrationID(context);
callbackContext.success(regID);
}
@ -413,31 +376,167 @@ public class JPushPlugin extends CordovaPlugin {
}
}
void setTags(JSONArray data, CallbackContext callbackContext) {
void setAlias(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
String alias = null;
try {
HashSet<String> tags = new HashSet<String>();
for (int i = 0; i < data.length(); i++) {
tags.add(data.getString(i));
}
JPushInterface.setTags(this.cordova.getActivity().getApplicationContext(),
tags, mTagWithAliasCallback);
callbackContext.success();
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
alias = params.getString("alias");
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Error reading tags JSON");
callbackContext.error("Parameters error.");
}
JPushInterface.setAlias(mContext, sequence, alias);
eventCallbackMap.put(sequence, callbackContext);
}
void setAlias(JSONArray data, CallbackContext callbackContext) {
void deleteAlias(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
try {
String alias = data.getString(0);
JPushInterface.setAlias(this.cordova.getActivity().getApplicationContext(),
alias, mTagWithAliasCallback);
callbackContext.success();
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Error reading alias JSON");
callbackContext.error("Parameters error.");
}
JPushInterface.deleteAlias(mContext, sequence);
eventCallbackMap.put(sequence, callbackContext);
}
void getAlias(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
try {
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Parameters error.");
}
JPushInterface.getAlias(mContext, sequence);
eventCallbackMap.put(sequence, callbackContext);
}
void setTags(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
Set<String> tags = new HashSet<String>();
try {
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
JSONArray tagsArr = params.getJSONArray("tags");
for (int i = 0; i < tagsArr.length(); i++) {
tags.add(tagsArr.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Parameters error.");
}
JPushInterface.setTags(mContext, sequence, tags);
eventCallbackMap.put(sequence, callbackContext);
}
void addTags(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
Set<String> tags = new HashSet<String>();
try {
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
JSONArray tagsArr = params.getJSONArray("tags");
for (int i = 0; i < tagsArr.length(); i++) {
tags.add(tagsArr.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Parameters error.");
}
JPushInterface.addTags(mContext, sequence, tags);
eventCallbackMap.put(sequence, callbackContext);
}
void deleteTags(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
Set<String> tags = new HashSet<String>();
try {
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
JSONArray tagsArr = params.getJSONArray("tags");
for (int i = 0; i < tagsArr.length(); i++) {
tags.add(tagsArr.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Parameters error.");
}
JPushInterface.deleteTags(mContext, sequence, tags);
eventCallbackMap.put(sequence, callbackContext);
}
void cleanTags(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
try {
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Parameters error.");
}
JPushInterface.cleanTags(mContext, sequence);
eventCallbackMap.put(sequence, callbackContext);
}
void getAllTags(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
try {
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Parameters error.");
}
JPushInterface.getAllTags(mContext, sequence);
eventCallbackMap.put(sequence, callbackContext);
}
void checkTagBindState(JSONArray data, CallbackContext callbackContext) {
int sequence = -1;
String tag = null;
try {
JSONObject params = data.getJSONObject(0);
sequence = params.getInt("sequence");
tag = params.getString("tag");
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("Parameters error.");
}
JPushInterface.checkTagBindState(mContext, sequence, tag);
eventCallbackMap.put(sequence, callbackContext);
}
void setTagsWithAlias(JSONArray data, CallbackContext callbackContext) {
@ -449,7 +548,7 @@ public class JPushPlugin extends CordovaPlugin {
for (int i = 0; i < tagsArray.length(); i++) {
tags.add(tagsArray.getString(i));
}
JPushInterface.setAliasAndTags(this.cordova.getActivity().getApplicationContext(),
JPushInterface.setAliasAndTags(mContext,
alias, tags, mTagWithAliasCallback);
callbackContext.success();
} catch (JSONException e) {
@ -513,8 +612,7 @@ public class JPushPlugin extends CordovaPlugin {
}
}
void addLocalNotification(JSONArray data, CallbackContext callbackContext)
throws JSONException {
void addLocalNotification(JSONArray data, CallbackContext callbackContext) throws JSONException {
int builderId = data.getInt(0);
String content = data.getString(1);
String title = data.getString(2);
@ -537,8 +635,7 @@ public class JPushPlugin extends CordovaPlugin {
JPushInterface.addLocalNotification(this.cordova.getActivity(), ln);
}
void removeLocalNotification(JSONArray data, CallbackContext callbackContext)
throws JSONException {
void removeLocalNotification(JSONArray data, CallbackContext callbackContext) throws JSONException {
int notificationID = data.getInt(0);
JPushInterface.removeLocalNotification(this.cordova.getActivity(), notificationID);
}
@ -634,7 +731,7 @@ public class JPushPlugin extends CordovaPlugin {
String pkg = context.getPackageName();
int uid = appInfo.uid;
Class appOpsClazz = null;
Class appOpsClazz;
try {
appOpsClazz = Class.forName(AppOpsManager.class.getName());

View File

@ -84,12 +84,88 @@ JPushPlugin.prototype.setTagsWithAlias = function (tags, alias, successCallback,
this.callNative('setTagsWithAlias', arrayTagWithAlias, successCallback, errorCallback)
}
JPushPlugin.prototype.setTags = function (tags, successCallback, errorCallback) {
this.callNative('setTags', tags, successCallback, errorCallback)
/**
* 设置标签
* 注意该接口是覆盖逻辑而不是增量逻辑即新的调用会覆盖之前的设置
*
* @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
*/
JPushPlugin.prototype.setTags = function (params, successCallback, errorCallback) {
this.callNative('setTags', [params], successCallback, errorCallback)
}
JPushPlugin.prototype.setAlias = function (alias, successCallback, errorCallback) {
this.callNative('setAlias', [alias], successCallback, errorCallback)
/**
* 新增标签新增不代表设置
* 注意该接口是覆盖逻辑而不是增量逻辑即新的调用会覆盖之前的设置
*
* @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
*/
JPushPlugin.prototype.addTags = function (params, successCallback, errorCallback) {
this.callNative('addTags', [params], successCallback, errorCallback)
}
/**
* 删除指定标签
*
* @param params = { 'sequence': number, 'tags': ['tag1', 'tag2'] }
*/
JPushPlugin.prototype.deleteTags = function (params, successCallback, errorCallback) {
this.callNative('deleteTags', [params], successCallback, errorCallback)
}
/**
* 清除所有标签
*
* @param params = { 'sequence': number }
*/
JPushPlugin.prototype.cleanTags = function (params, successCallback, errorCallback) {
this.callNative('cleanTags', [params], successCallback, errorCallback)
}
/**
* 查询所有标签
*
* @param params = { 'sequence': number }
*/
JPushPlugin.prototype.getAllTags = function (params, successCallback, errorCallback) {
this.callNative('getAllTags', [params], successCallback, errorCallback)
}
/**
* 查询指定标签与当前用户的绑定状态
*
* @param params = { 'sequence': number, 'tag': string }
*/
JPushPlugin.prototype.checkTagBindState = function (params, successCallback, errorCallback) {
this.callNative('checkTagBindState', [params], successCallback, errorCallback)
}
/**
* 设置别名
* 注意该接口是覆盖逻辑而不是增量逻辑即新的调用会覆盖之前的设置
*
* @param params = { 'sequence': number, 'alias': string }
*/
JPushPlugin.prototype.setAlias = function (params, successCallback, errorCallback) {
this.callNative('setAlias', [params], successCallback, errorCallback)
}
/**
* 删除别名
*
* @param params = { 'sequence': number, 'alias': string }
*/
JPushPlugin.prototype.deleteAlias = function (params, successCallback, errorCallback) {
this.callNative('deleteAlias', [params], successCallback, errorCallback)
}
/**
* 查询当前绑定的别名
*
* @param params = { 'sequence': number }
*/
JPushPlugin.prototype.getAlias = function (params, successCallback, errorCallback) {
this.callNative('getAlias', [params], successCallback, errorCallback)
}
// 判断系统设置中是否对本应用启用通知。