mirror of
https://github.com/jpush/jpush-phonegap-plugin.git
synced 2025-02-15 00:22:50 +08:00
Android添加手机号设置接口
This commit is contained in:
parent
10b29611c5
commit
fad4745e01
@ -200,6 +200,16 @@
|
||||
alert(error.code)
|
||||
})
|
||||
});
|
||||
|
||||
$("#setMobileNumber").click(function (event) {
|
||||
var number = $("#mobileNumberText").val()
|
||||
window.JPush.setMobileNumber({ sequence: 5, mobileNumber: number },
|
||||
function (result) {
|
||||
$("#mobileNumberResult").html(result.mobileNumber)
|
||||
}, function (error){
|
||||
alert(error.code)
|
||||
})
|
||||
})
|
||||
};
|
||||
|
||||
document.addEventListener("deviceready", onDeviceReady, false);
|
||||
@ -251,6 +261,14 @@
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<label>MobileNumber: </label>
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="text" id="mobileNumberText" />
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div data-role="fieldcontain">
|
||||
@ -265,6 +283,10 @@
|
||||
<input type="button" id="deleteAlias" value="Delete alias" />
|
||||
</div>
|
||||
|
||||
<div data-role="fieldcontain">
|
||||
<input type="button" id="setMobileNumber" value="Set mobileNumber" />
|
||||
</div>
|
||||
|
||||
<div data-role="fieldcontain">
|
||||
<label id="tagsPrompt">设置 Tag 的结果:</label>
|
||||
<label id="tagsResult">null</label>
|
||||
@ -273,6 +295,10 @@
|
||||
<label id="aliasPrompt">设置 Alias 的结果:</label>
|
||||
<label id="aliasResult">null</label>
|
||||
</div>
|
||||
<div data-role="fieldcontain">
|
||||
<label id="mobileNumberPrompt">设置手机号的结果:</label>
|
||||
<label id="mobileNumberResult">null</label>
|
||||
</div>
|
||||
<div data-role="fieldcontain">
|
||||
<label id="notificationPrompt">接受的通知内容:</label>
|
||||
<label id="notificationResult">null</label>
|
||||
|
46
src/android/JLogger.java
Normal file
46
src/android/JLogger.java
Normal file
@ -0,0 +1,46 @@
|
||||
package cn.jiguang.cordova.push;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class JLogger {
|
||||
|
||||
public static final String TAG = "[Cordova-JPush]";
|
||||
|
||||
private static boolean isLoggerEnable = false;
|
||||
|
||||
public static void setLoggerEnable(boolean loggerEnable) {
|
||||
Log.d(TAG, "setLoggerEnable:" + loggerEnable);
|
||||
isLoggerEnable = loggerEnable;
|
||||
}
|
||||
|
||||
public static void i(String tag,String msg) {
|
||||
if (isLoggerEnable) {
|
||||
Log.i(TAG+tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void d(String tag,String msg) {
|
||||
if (isLoggerEnable) {
|
||||
Log.d(TAG+tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void v(String tag,String msg) {
|
||||
if (isLoggerEnable) {
|
||||
Log.v(TAG+tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void w(String tag,String msg) {
|
||||
if (isLoggerEnable) {
|
||||
Log.w(TAG+tag, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void e(String tag,String error) {
|
||||
if (isLoggerEnable) {
|
||||
Log.e(TAG+tag, error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -3,20 +3,18 @@ package cn.jiguang.cordova.push;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
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.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import cn.jpush.android.api.CustomMessage;
|
||||
import cn.jpush.android.api.JPushInterface;
|
||||
import cn.jpush.android.api.JPushMessage;
|
||||
import cn.jpush.android.api.NotificationMessage;
|
||||
import cn.jpush.android.helper.Logger;
|
||||
import cn.jpush.android.service.JPushMessageReceiver;
|
||||
|
||||
public class JPushEventReceiver extends JPushMessageReceiver {
|
||||
@ -26,143 +24,58 @@ public class JPushEventReceiver extends JPushMessageReceiver {
|
||||
@Override
|
||||
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
|
||||
super.onTagOperatorResult(context, jPushMessage);
|
||||
//Log.e(TAG,"onTagOperatorResult:"+jPushMessage);
|
||||
JSONObject resultJson = new JSONObject();
|
||||
JLogger.d(TAG,"onTagOperatorResult:"+jPushMessage);
|
||||
|
||||
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 {
|
||||
tryCallback(jPushMessage, new SuccessCallback() {
|
||||
@Override
|
||||
public void onSuccessCallback(JSONObject resultJson) throws JSONException {
|
||||
Set<String> tags = jPushMessage.getTags();
|
||||
JSONArray tagsJsonArr = new JSONArray();
|
||||
for (String tag : tags) {
|
||||
tagsJsonArr.put(tag);
|
||||
}
|
||||
if (tagsJsonArr.length() != 0) {
|
||||
resultJson.put("tags", tagsJsonArr);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
callback.success(resultJson);
|
||||
|
||||
} else {
|
||||
try {
|
||||
resultJson.put("code", 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);
|
||||
|
||||
//Log.e(TAG,"onCheckTagOperatorResult:"+jPushMessage);
|
||||
JSONObject resultJson = new JSONObject();
|
||||
JLogger.d(TAG,"onCheckTagOperatorResult:"+jPushMessage);
|
||||
|
||||
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 {
|
||||
tryCallback(jPushMessage, new SuccessCallback() {
|
||||
@Override
|
||||
public void onSuccessCallback(JSONObject resultJson) throws JSONException {
|
||||
resultJson.put("tag", jPushMessage.getCheckTag());
|
||||
resultJson.put("isBind", jPushMessage.getTagCheckStateResult());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callback.success(resultJson);
|
||||
|
||||
} else {
|
||||
try {
|
||||
resultJson.put("code", 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);
|
||||
|
||||
//Log.e(TAG,"onAliasOperatorResult:"+jPushMessage);
|
||||
JSONObject resultJson = new JSONObject();
|
||||
JLogger.d(TAG,"onAliasOperatorResult:"+jPushMessage);
|
||||
|
||||
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 {
|
||||
tryCallback(jPushMessage, new SuccessCallback() {
|
||||
@Override
|
||||
public void onSuccessCallback(JSONObject resultJson) throws JSONException {
|
||||
if (!TextUtils.isEmpty(jPushMessage.getAlias())) {
|
||||
resultJson.put("alias", jPushMessage.getAlias());
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
callback.success(resultJson);
|
||||
|
||||
} else {
|
||||
try {
|
||||
resultJson.put("code", jPushMessage.getErrorCode());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
callback.error(resultJson);
|
||||
}
|
||||
|
||||
JPushPlugin.eventCallbackMap.remove(sequence);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRegister(Context context, String regId) {
|
||||
//Log.e(TAG,"onRegister:"+regId);
|
||||
JLogger.d(TAG,"onRegister:"+regId);
|
||||
JPushPlugin.transmitReceiveRegistrationId(regId);
|
||||
}
|
||||
|
||||
@ -179,24 +92,74 @@ public class JPushEventReceiver extends JPushMessageReceiver {
|
||||
public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
|
||||
super.onNotifyMessageArrived(context, notificationMessage);
|
||||
|
||||
//Log.e(TAG,"onNotifyMessageArrived:"+notificationMessage);
|
||||
JLogger.d(TAG,"onNotifyMessageArrived:"+notificationMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
|
||||
super.onNotifyMessageOpened(context, notificationMessage);
|
||||
//Log.e(TAG,"onNotifyMessageOpened:"+notificationMessage);
|
||||
JLogger.d(TAG,"onNotifyMessageOpened:"+notificationMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
|
||||
super.onMobileNumberOperatorResult(context, jPushMessage);
|
||||
//Log.e(TAG,"onMobileNumberOperatorResult:"+jPushMessage);
|
||||
JLogger.d(TAG,"onMobileNumberOperatorResult:"+jPushMessage);
|
||||
|
||||
tryCallback(jPushMessage, new SuccessCallback() {
|
||||
@Override
|
||||
public void onSuccessCallback(JSONObject resultJson) throws JSONException {
|
||||
if (!TextUtils.isEmpty(jPushMessage.getMobileNumber())) {
|
||||
resultJson.put("mobileNumber", jPushMessage.getMobileNumber());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMultiActionClicked(Context context, Intent intent) {
|
||||
super.onMultiActionClicked(context, intent);
|
||||
//Log.e(TAG,"onMultiActionClicked:"+intent);
|
||||
JLogger.d(TAG,"onMultiActionClicked:"+intent);
|
||||
}
|
||||
|
||||
interface SuccessCallback{
|
||||
void onSuccessCallback(JSONObject resultJson) throws JSONException;
|
||||
}
|
||||
public void tryCallback(JPushMessage jPushMessage,SuccessCallback successCallback){
|
||||
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) {
|
||||
Logger.i(TAG, "Unexpected error, callback is null!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (jPushMessage.getErrorCode() == 0) {
|
||||
try {
|
||||
successCallback.onSuccessCallback(resultJson);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callback.success(resultJson);
|
||||
|
||||
} else {
|
||||
try {
|
||||
resultJson.put("code", jPushMessage.getErrorCode());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callback.error(resultJson);
|
||||
}
|
||||
|
||||
JPushPlugin.eventCallbackMap.remove(sequence);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -59,8 +59,9 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
mContext = cordova.getActivity().getApplicationContext();
|
||||
Log.d(TAG,"initialize plugin");
|
||||
|
||||
JPushInterface.init(mContext);
|
||||
// JPushInterface.init(mContext);
|
||||
|
||||
cordovaActivity = cordova.getActivity();
|
||||
|
||||
@ -254,6 +255,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
try {
|
||||
mode = data.getBoolean(0);
|
||||
JPushInterface.setDebugMode(mode);
|
||||
JLogger.setLoggerEnable(mode);
|
||||
callbackContext.success();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
@ -296,6 +298,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("error reading num json");
|
||||
return;
|
||||
}
|
||||
if (num != -1) {
|
||||
JPushInterface.setLatestNotificationNumber(mContext, num);
|
||||
@ -364,6 +367,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.setAlias(mContext, sequence, alias);
|
||||
@ -379,6 +383,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.deleteAlias(mContext, sequence);
|
||||
@ -394,6 +399,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.getAlias(mContext, sequence);
|
||||
@ -416,6 +422,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.setTags(mContext, sequence, tags);
|
||||
@ -438,6 +445,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.addTags(mContext, sequence, tags);
|
||||
@ -460,6 +468,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.deleteTags(mContext, sequence, tags);
|
||||
@ -476,6 +485,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.cleanTags(mContext, sequence);
|
||||
@ -492,6 +502,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.getAllTags(mContext, sequence);
|
||||
@ -510,6 +521,7 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
JPushInterface.checkTagBindState(mContext, sequence, tag);
|
||||
@ -642,6 +654,25 @@ public class JPushPlugin extends CordovaPlugin {
|
||||
JPushInterface.setBadgeNumber(mContext, badgeNumb);
|
||||
}
|
||||
|
||||
void setMobileNumber(JSONArray data, CallbackContext callbackContext) throws JSONException {
|
||||
int sequence = -1;
|
||||
String number = null;
|
||||
|
||||
try {
|
||||
JSONObject params = data.getJSONObject(0);
|
||||
sequence = params.getInt("sequence");
|
||||
number = params.getString("mobileNumber");
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("Parameters error.");
|
||||
return;
|
||||
}
|
||||
|
||||
eventCallbackMap.put(sequence, callbackContext);
|
||||
JPushInterface.setMobileNumber(mContext,sequence, number);
|
||||
}
|
||||
|
||||
private boolean isValidHour(int hour) {
|
||||
return !(hour < 0 || hour > 23);
|
||||
}
|
||||
|
@ -482,6 +482,20 @@ JPushPlugin.prototype.setBadgeNumber = function(badgeNumb) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置手机号。
|
||||
*
|
||||
* @param params = { 'sequence': number, 'mobileNumber': string }
|
||||
*/
|
||||
JPushPlugin.prototype.setMobileNumber = function(
|
||||
params,
|
||||
successCallback,
|
||||
errorCallback
|
||||
) {
|
||||
this.callNative("setMobileNumber", [params], successCallback, errorCallback);
|
||||
|
||||
};
|
||||
|
||||
if (!window.plugins) {
|
||||
window.plugins = {};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user