添加控制统计分析功能的API

添加控制统计分析的api,并修改了相关文档和示例代码,开发者可通过 js 代码直接控制是否启用统计分析功能。
This commit is contained in:
Hevin 2016-03-04 15:20:45 +08:00
parent dde913b922
commit 7d4b6acacd
4 changed files with 530 additions and 531 deletions

View File

@ -88,7 +88,7 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。
具体的API请参考这里 具体的API请参考这里
#### iOS和adnroid通用API简介 #### iOS和android通用API简介
+ 停止与恢复推送服务 API + 停止与恢复推送服务 API
@ -152,7 +152,7 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。
[iOS API详细说明](document/iOS_detail_api.md) [iOS API详细说明](document/iOS_detail_api.md)
#### adnroid API简介 #### android API简介
+ 获取集成日志 + 获取集成日志
window.plugins.jPushPlugin.setDebugMode(mode) window.plugins.jPushPlugin.setDebugMode(mode)
@ -165,7 +165,12 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。
+ 统计分析 API + 统计分析 API
onResume / onPause(java api) window.plugins.jPushPlugin.setStatisticsOpen(boolean)
或在 MainActivity 中的 onPause() 和 onResume() 方法中分别调用
JPushInterface.onResume(this) 和 JPushInterface.onPause(this) 来启用统计分析功能,
如果使用这种方式启用统计分析功能则window.plugins.jPushPlugin.setStatisticsOpen(boolean)
方法不再有效,建议不要同时使用。
+ 清除通知 API + 清除通知 API
@ -195,7 +200,7 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。
###常见问题 ###常见问题
####1. androd ####1. android
eclipse中phonegap工程import之后出现:`Type CallbackContext cannot be resolved to a type` eclipse中phonegap工程import之后出现:`Type CallbackContext cannot be resolved to a type`
解决方案eclipse中右键单击工程名Build Path->Config Build Path->Projects->选中 工程名称CordovaLib->点击 add 解决方案eclipse中右键单击工程名Build Path->Config Build Path->Projects->选中 工程名称CordovaLib->点击 add

View File

@ -105,6 +105,7 @@
window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0); window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
} else { } else {
window.plugins.jPushPlugin.setDebugMode(true); window.plugins.jPushPlugin.setDebugMode(true);
window.plugins.jPushPlugin.setStatisticsOpen(true);
} }
} }
catch (exception) { catch (exception) {
@ -220,5 +221,3 @@
</div> </div>
</body> </body>
</html> </html>

View File

@ -1,14 +1,9 @@
package cn.jpush.phonegap; package cn.jpush.phonegap;
import java.lang.reflect.Method; import android.content.Context;
import java.util.Arrays; import android.util.Log;
import java.util.HashMap;
import java.util.HashSet; import com.jpushphonegapdemo.jpush.R;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
@ -17,16 +12,23 @@ import org.apache.cordova.CordovaWebView;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Map.Entry;
import __PACKAGE_NAME__.R; import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import cn.jpush.android.api.BasicPushNotificationBuilder; import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.CustomPushNotificationBuilder; import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface; import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.data.JPushLocalNotification;
import cn.jpush.android.api.TagAliasCallback; import cn.jpush.android.api.TagAliasCallback;
import android.util.Log; import cn.jpush.android.data.JPushLocalNotification;
public class JPushPlugin extends CordovaPlugin { public class JPushPlugin extends CordovaPlugin {
@ -54,20 +56,21 @@ public class JPushPlugin extends CordovaPlugin {
"clearLocalNotifications", "clearLocalNotifications",
"onResume", "onResume",
"onPause", "onPause",
"reportNotificationOpened"); "reportNotificationOpened",
"setStatisticsOpen");
private ExecutorService threadPool = Executors.newFixedThreadPool(1); private ExecutorService threadPool = Executors.newFixedThreadPool(1);
private static JPushPlugin instance; private static JPushPlugin instance;
private static String TAG = "JPushPlugin"; private static String TAG = "JPushPlugin";
private static boolean shouldCacheMsg = false; private static boolean shouldCacheMsg = false;
private static boolean isStatisticsOpened = true; // 是否开启统计分析功能
public static String notificationAlert; public static String notificationAlert;
public static Map<String, Object> notificationExtras = new HashMap<String, Object>(); public static Map<String, Object> notificationExtras = new HashMap<String, Object>();
public static String openNotificationAlert; public static String openNotificationAlert;
public static Map<String, Object> openNotificationExtras = new HashMap<String, Object>(); public static Map<String, Object> openNotificationExtras = new HashMap<String, Object>();
public JPushPlugin() { public JPushPlugin() {
instance = this; instance = this;
} }
@ -84,37 +87,41 @@ public class JPushPlugin extends CordovaPlugin {
//这样做是为了和iOS 统一 //这样做是为了和iOS 统一
if (JPushPlugin.openNotificationAlert != null) { if (JPushPlugin.openNotificationAlert != null) {
JPushPlugin.notificationAlert = null; JPushPlugin.notificationAlert = null;
JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert, JPushPlugin.openNotificationExtras); JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert,
JPushPlugin.openNotificationExtras);
} }
if (JPushPlugin.notificationAlert != null) { if (JPushPlugin.notificationAlert != null) {
JPushPlugin.transmitReceive(JPushPlugin.notificationAlert, JPushPlugin.notificationExtras); JPushPlugin.transmitReceive(JPushPlugin.notificationAlert,
JPushPlugin.notificationExtras);
} }
//JPushInterface.init(cordova.getActivity().getApplicationContext()); //JPushInterface.init(cordova.getActivity().getApplicationContext());
} }
public void onPause(boolean multitasking) { public void onPause(boolean multitasking) {
Log.i(TAG, "---------------- onPause"); Log.i(TAG, "---------------- onPause");
shouldCacheMsg = true; shouldCacheMsg = true;
if (isStatisticsOpened) {
JPushInterface.onPause(this.cordova.getActivity());
}
} }
public void onResume(boolean multitasking) { public void onResume(boolean multitasking) {
shouldCacheMsg = false; shouldCacheMsg = false;
Log.i(TAG, "---------------- onResume" + "-" + JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert); Log.i(TAG, "---------------- onResume" + "-" + JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert);
if (isStatisticsOpened) {
JPushInterface.onResume(this.cordova.getActivity());
}
if (JPushPlugin.openNotificationAlert != null) { if (JPushPlugin.openNotificationAlert != null) {
JPushPlugin.notificationAlert = null; JPushPlugin.notificationAlert = null;
JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert, JPushPlugin.openNotificationExtras); JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert,
JPushPlugin.openNotificationExtras);
} }
if (JPushPlugin.notificationAlert != null) { if (JPushPlugin.notificationAlert != null) {
JPushPlugin.transmitReceive(JPushPlugin.notificationAlert, JPushPlugin.notificationExtras); JPushPlugin.transmitReceive(JPushPlugin.notificationAlert,
JPushPlugin.notificationExtras);
} }
} }
private static JSONObject notificationObject(String message, private static JSONObject notificationObject(String message,
Map<String, Object> extras) { Map<String, Object> extras) {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
@ -129,12 +136,11 @@ public class JPushPlugin extends CordovaPlugin {
jExtras.put(entry.getKey(), entry.getValue()); jExtras.put(entry.getKey(), entry.getValue());
} }
} }
if(jExtras.length()>0) if (jExtras.length() > 0) {
{
data.put("extras", jExtras); data.put("extras", jExtras);
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace();
} }
return data; return data;
} }
@ -153,93 +159,77 @@ public class JPushPlugin extends CordovaPlugin {
jExtras.put(entry.getKey(), entry.getValue()); jExtras.put(entry.getKey(), entry.getValue());
} }
} }
if(jExtras.length()>0) if (jExtras.length() > 0) {
{
data.put("extras", jExtras); data.put("extras", jExtras);
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace();
} }
return data; return data;
} }
static void transmitPush(String message, Map<String, Object> extras) { static void transmitPush(String message, Map<String, Object> extras) {
if (instance == null) { if (instance == null) {
return; return;
} }
JSONObject data = notificationObject(message, extras); JSONObject data = notificationObject(message, extras);
String js = String String js = String.format(
.format("window.plugins.jPushPlugin.receiveMessageInAndroidCallback('%s');", "window.plugins.jPushPlugin.receiveMessageInAndroidCallback('%s');",
data.toString()); data.toString());
try { try {
instance.webView.sendJavascript(js); instance.webView.sendJavascript(js);
// String jsEvent=String // String jsEvent=String
// .format("cordova.fireDocumentEvent('jpush.receiveMessage',%s)", // .format("cordova.fireDocumentEvent('jpush.receiveMessage',%s)",
// data.toString()); // data.toString());
// instance.webView.sendJavascript(jsEvent); // instance.webView.sendJavascript(jsEvent);
} catch (NullPointerException e) {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
}
}
}
}
static void transmitOpen(String alert, Map<String, Object> extras) { static void transmitOpen(String alert, Map<String, Object> extras) {
if (instance == null) { if (instance == null) {
return; return;
} }
if (JPushPlugin.shouldCacheMsg) { if (JPushPlugin.shouldCacheMsg) {
return; return;
} }
Log.i(TAG, "---------------- transmitOpen"); Log.i(TAG, "---------------- transmitOpen");
JSONObject data = openNotificationObject(alert, extras); JSONObject data = openNotificationObject(alert, extras);
String js = String String js = String.format(
.format("window.plugins.jPushPlugin.openNotificationInAndroidCallback('%s');", "window.plugins.jPushPlugin.openNotificationInAndroidCallback('%s');",
data.toString()); data.toString());
try { try {
instance.webView.sendJavascript(js); instance.webView.sendJavascript(js);
// String jsEvent=String // String jsEvent=String
// .format("cordova.fireDocumentEvent('jpush.openNotification',%s)", // .format("cordova.fireDocumentEvent('jpush.openNotification',%s)",
// data.toString()); // data.toString());
// instance.webView.sendJavascript(jsEvent); // instance.webView.sendJavascript(jsEvent);
} catch (NullPointerException e) {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} }
JPushPlugin.openNotificationAlert = null; JPushPlugin.openNotificationAlert = null;
} }
static void transmitReceive(String alert, Map<String, Object> extras) { static void transmitReceive(String alert, Map<String, Object> extras) {
if (instance == null) { if (instance == null) {
return; return;
} }
if (JPushPlugin.shouldCacheMsg) { if (JPushPlugin.shouldCacheMsg) {
return; return;
} }
JSONObject data = openNotificationObject(alert, extras); JSONObject data = openNotificationObject(alert, extras);
String js = String String js = String.format(
.format("window.plugins.jPushPlugin.receiveNotificationInAndroidCallback('%s');", "window.plugins.jPushPlugin.receiveNotificationInAndroidCallback('%s');",
data.toString()); data.toString());
Log.i(TAG, "--------->" + js);
try { try {
instance.webView.sendJavascript(js); instance.webView.sendJavascript(js);
} catch (NullPointerException e) {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} }
JPushPlugin.notificationAlert = null; JPushPlugin.notificationAlert = null;
} }
@Override @Override
@ -282,24 +272,23 @@ public class JPushPlugin extends CordovaPlugin {
JPushInterface.setDebugMode(mode); JPushInterface.setDebugMode(mode);
callbackContext.success(); callbackContext.success();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace();
} }
} }
void stopPush(JSONArray data, void stopPush(JSONArray data, CallbackContext callbackContext) {
CallbackContext callbackContext){
JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext()); JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext());
callbackContext.success(); callbackContext.success();
} }
void resumePush(JSONArray data, void resumePush(JSONArray data, CallbackContext callbackContext) {
CallbackContext callbackContext){
JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext()); JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext());
callbackContext.success(); callbackContext.success();
} }
void isPushStopped(JSONArray data, void isPushStopped(JSONArray data, CallbackContext callbackContext) {
CallbackContext callbackContext){ boolean isStopped = JPushInterface.isPushStopped(
boolean isStopped =JPushInterface.isPushStopped(this.cordova.getActivity().getApplicationContext()); this.cordova.getActivity().getApplicationContext());
if (isStopped) { if (isStopped) {
callbackContext.success(1); callbackContext.success(1);
} else { } else {
@ -307,8 +296,7 @@ public class JPushPlugin extends CordovaPlugin {
} }
} }
void setLatestNotificationNum(JSONArray data, void setLatestNotificationNum(JSONArray data, CallbackContext callbackContext) {
CallbackContext callbackContext){
int num = -1; int num = -1;
try { try {
num = data.getInt(0); num = data.getInt(0);
@ -317,14 +305,14 @@ public class JPushPlugin extends CordovaPlugin {
callbackContext.error("error reading num json"); callbackContext.error("error reading num json");
} }
if (num != -1) { if (num != -1) {
JPushInterface.setLatestNotificationNumber(this.cordova.getActivity().getApplicationContext(), num); JPushInterface.setLatestNotificationNumber(
this.cordova.getActivity().getApplicationContext(), num);
} else { } else {
callbackContext.error("error num"); callbackContext.error("error num");
} }
} }
void setPushTime(JSONArray data, void setPushTime(JSONArray data, CallbackContext callbackContext) {
CallbackContext callbackContext){
Set<Integer> days = new HashSet<Integer>(); Set<Integer> days = new HashSet<Integer>();
JSONArray dayArray; JSONArray dayArray;
int startHour = -1; int startHour = -1;
@ -344,34 +332,36 @@ public class JPushPlugin extends CordovaPlugin {
} catch (JSONException e) { } catch (JSONException e) {
callbackContext.error("error reading hour json"); callbackContext.error("error reading hour json");
} }
JPushInterface.setPushTime(this.cordova.getActivity().getApplicationContext(), days, startHour, endHour); Context context = this.cordova.getActivity().getApplicationContext();
JPushInterface.setPushTime(context, days, startHour, endHour);
callbackContext.success(); callbackContext.success();
} }
void getRegistrationID(JSONArray data, CallbackContext callbackContext) { void getRegistrationID(JSONArray data, CallbackContext callbackContext) {
String regID= JPushInterface.getRegistrationID(this.cordova.getActivity().getApplicationContext()); Context context = this.cordova.getActivity().getApplicationContext();
String regID = JPushInterface.getRegistrationID(context);
callbackContext.success(regID); callbackContext.success(regID);
} }
void onResume(JSONArray data, CallbackContext callbackContext) { void onResume(JSONArray data, CallbackContext callbackContext) {
JPushInterface.onResume(this.cordova.getActivity()); JPushInterface.onResume(this.cordova.getActivity());
} }
void onPause(JSONArray data, CallbackContext callbackContext) { void onPause(JSONArray data, CallbackContext callbackContext) {
JPushInterface.onPause(this.cordova.getActivity()); JPushInterface.onPause(this.cordova.getActivity());
} }
void reportNotificationOpened(JSONArray data, CallbackContext callbackContext) { void reportNotificationOpened(JSONArray data, CallbackContext callbackContext) {
try { try {
String msgID; String msgID;
msgID = data.getString(0); msgID = data.getString(0);
JPushInterface.reportNotificationOpened(this.cordova.getActivity(), msgID); JPushInterface.reportNotificationOpened(this.cordova.getActivity(), msgID);
} catch (JSONException e) { } catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
void setTags(JSONArray data, CallbackContext callbackContext) {
void setTags(JSONArray data, CallbackContext callbackContext) {
try { try {
HashSet<String> tags = new HashSet<String>(); HashSet<String> tags = new HashSet<String>();
for (int i = 0; i < data.length(); i++) { for (int i = 0; i < data.length(); i++) {
@ -380,7 +370,6 @@ public class JPushPlugin extends CordovaPlugin {
JPushInterface.setTags(this.cordova.getActivity() JPushInterface.setTags(this.cordova.getActivity()
.getApplicationContext(), tags, mTagWithAliasCallback); .getApplicationContext(), tags, mTagWithAliasCallback);
callbackContext.success(); callbackContext.success();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
callbackContext.error("Error reading tags JSON"); callbackContext.error("Error reading tags JSON");
@ -408,7 +397,6 @@ public class JPushPlugin extends CordovaPlugin {
for (int i = 0; i < tagsArray.length(); i++) { for (int i = 0; i < tagsArray.length(); i++) {
tags.add(tagsArray.getString(i)); tags.add(tagsArray.getString(i));
} }
JPushInterface.setAliasAndTags(this.cordova.getActivity() JPushInterface.setAliasAndTags(this.cordova.getActivity()
.getApplicationContext(), alias, tags, mTagWithAliasCallback); .getApplicationContext(), alias, tags, mTagWithAliasCallback);
callbackContext.success(); callbackContext.success();
@ -468,14 +456,12 @@ public class JPushPlugin extends CordovaPlugin {
//callbackContext.success(obj); //callbackContext.success(obj);
} }
void clearAllNotification(JSONArray data, void clearAllNotification(JSONArray data, CallbackContext callbackContext) {
CallbackContext callbackContext){
JPushInterface.clearAllNotifications(this.cordova.getActivity()); JPushInterface.clearAllNotifications(this.cordova.getActivity());
//callbackContext.success(); //callbackContext.success();
} }
void clearNotificationById(JSONArray data, void clearNotificationById(JSONArray data, CallbackContext callbackContext) {
CallbackContext callbackContext){
int notificationId = -1; int notificationId = -1;
try { try {
notificationId = data.getInt(0); notificationId = data.getInt(0);
@ -484,15 +470,16 @@ public class JPushPlugin extends CordovaPlugin {
callbackContext.error("error reading id json"); callbackContext.error("error reading id json");
} }
if (notificationId != -1) { if (notificationId != -1) {
JPushInterface.clearNotificationById(this.cordova.getActivity(), notificationId); JPushInterface.clearNotificationById(this.cordova.getActivity(),
notificationId);
} else { } else {
callbackContext.error("error id"); callbackContext.error("error id");
} }
} }
void addLocalNotification(JSONArray data,
CallbackContext callbackContext) throws JSONException{
//builderId,content,title,notificaitonID,broadcastTime,extras
void addLocalNotification(JSONArray data, CallbackContext callbackContext)
throws JSONException {
//builderId,content,title,notificaitonID,broadcastTime,extras
int builderId = data.getInt(0); int builderId = data.getInt(0);
String content = data.getString(1); String content = data.getString(1);
String title = data.getString(2); String title = data.getString(2);
@ -509,50 +496,51 @@ public class JPushPlugin extends CordovaPlugin {
ln.setExtras(extras.toString()); ln.setExtras(extras.toString());
JPushInterface.addLocalNotification(this.cordova.getActivity(), ln); 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); int notificationID = data.getInt(0);
JPushInterface.removeLocalNotification(this.cordova.getActivity(),notificationID); JPushInterface.removeLocalNotification(this.cordova.getActivity(),
notificationID);
} }
void clearLocalNotifications(JSONArray data,
CallbackContext callbackContext){
void clearLocalNotifications(JSONArray data, CallbackContext callbackContext) {
JPushInterface.clearLocalNotifications(this.cordova.getActivity()); JPushInterface.clearLocalNotifications(this.cordova.getActivity());
}
/**
* 决定是否启用统计分析功能
* @param data
* @param callbackContext
*/
void setStatisticsOpen(JSONArray data, CallbackContext callbackContext) {
try {
isStatisticsOpened = data.getBoolean(0);
} catch (JSONException e) {
e.printStackTrace();
}
} }
private final TagAliasCallback mTagWithAliasCallback = new TagAliasCallback() { private final TagAliasCallback mTagWithAliasCallback = new TagAliasCallback() {
@Override @Override
public void gotResult(int code, String alias, Set<String> tags) { public void gotResult(int code, String alias, Set<String> tags) {
if (instance == null) { if (instance == null) {
return; return;
} }
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
try { try {
data.put("resultCode", code); data.put("resultCode", code);
data.put("tags", tags); data.put("tags", tags);
data.put("alias", alias); data.put("alias", alias);
String jsEvent = String.format(
String jsEvent=String "cordova.fireDocumentEvent('jpush.setTagsWithAlias',%s)",
.format("cordova.fireDocumentEvent('jpush.setTagsWithAlias',%s)",
data.toString()); data.toString());
instance.webView.sendJavascript(jsEvent); instance.webView.sendJavascript(jsEvent);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace();
} }
} }
}; };
} }

View File

@ -321,6 +321,16 @@ JPushPlugin.prototype.reportNotificationOpened = function(msgID){
} }
} }
/**
*是否开启统计分析功能用于用户使用时长活跃用户用户打开次数的统计并上报到服务器上
* Portal 上展示给开发者
**/
JPushPlugin.prototype.setStatisticsOpen = function(mode) {
if(device.platform == "Android") {
this.call_native("setStatisticsOpen", [mode], null);
}
}
//iOS single //iOS single
@ -333,6 +343,3 @@ if(!window.plugins.jPushPlugin){
} }
module.exports = new JPushPlugin(); module.exports = new JPushPlugin();