diff --git a/README.md b/README.md index f9f970d..6f3e761 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,16 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 1. cordova create 文件夹名字 包名 应用名字 cordova create Myproj com.myproj.jpush MyTestProj - + 2. 添加平台 - cd Myproj + cd Myproj cordova platform add android cordova platform add ios ps:这里请注意iOS平台,必须先执行 `cordova platform add ios`, 然后再执行`cordova plugin add xxxxx`命令,不然有一些必须要的链接库需要手动添加 - + ###Cordova CLI/Phonegap 安装 Android & iOS @@ -53,14 +53,14 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 3). 在js中调用函数,初始化jpush sdk - window.plugins.jPushPlugin.init(); + window.plugins.jPushPlugin.init(); //由于phonegap插件采用了Lazy load的特性, 所以这里建议在js文件能执行的最开始就加 ### Android 手工安装 [Android 手工安装文档地址](document/Android_handle_install.md) - + ### IOS手工安装 @@ -88,10 +88,10 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 具体的API请参考这里 -#### iOS和adnroid通用API简介 +#### iOS和android通用API简介 + 停止与恢复推送服务 API - + window.plugins.jPushPlugin.init() window.plugins.jPushPlugin.stopPush() window.plugins.jPushPlugin.resumePush() @@ -101,17 +101,17 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 + 获取 RegistrationID API window.plugins.jPushPlugin.getRegistrationID(callback) - + + 别名与标签 API - + window.plugins.jPushPlugin.setTagsWithAlias(tags,alias) window.plugins.jPushPlugin.setTags(tags) window.plugins.jPushPlugin.setAlias(alias) + 获取点击通知内容 - + event - jpush.openNotification + 获取通知内容 - + event - jpush.receiveNotification + 获取自定义消息推送内容 @@ -128,9 +128,9 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 event - jpush.receiveMessage //推荐使用事件的方式传递,但同时保留了receiveMessageIniOSCallback的回调函数,兼容以前的代码 window.plugins.jPushPlugin.receiveMessageIniOSCallback(data) - + + 页面的统计 - + window.plugins.jPushPlugin.startLogPageView (pageName) window.plugins.jPushPlugin.stopLogPageView (pageName) window.plugins.jPushPlugin.beginLogPageView (pageName,duration) @@ -142,31 +142,36 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 + 本地通知 + 后续版本加入 - + + 日志等级设置 - + window.plugins.jPushPlugin.setDebugModeFromIos () window.plugins.jPushPlugin.setLogOFF() - - + + [iOS API详细说明](document/iOS_detail_api.md) -#### adnroid API简介 - +#### android API简介 + + 获取集成日志 window.plugins.jPushPlugin.setDebugMode(mode) + 接收推送消息和点击通知 - + //下面这两个api 是兼容旧有的代码 window.plugins.jPushPlugin.receiveMessageInAndroidCallback(data) window.plugins.jPushPlugin.openNotificationInAndroidCallback(data) + 统计分析 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 window.plugins.jPushPlugin.clearAllNotification() @@ -177,11 +182,11 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 window.plugins.jPushPlugin.setCustomPushNotificationBuilder = function() + 设置保留最近通知条数 API - + window.plugins.jPushPlugin.setLatestNotificationNum(num) - + + 本地通知API - + window.plugins.jPushPlugin.addLocalNotification(builderId, content, title, @@ -195,18 +200,18 @@ jpush-phonegap-plugin 支持 iOS,Android 的推送插件。 ###常见问题 -####1. androd +####1. android eclipse中phonegap工程import之后出现:`Type CallbackContext cannot be resolved to a type` 解决方案:eclipse中右键单击工程名,Build Path->Config Build Path->Projects->选中 工程名称-CordovaLib->点击 add ####2. iOS 设置/修改 APP_KEY - - 在PushConfig.plist 中修改。PushConfig.plist 其他值说明: + + 在PushConfig.plist 中修改。PushConfig.plist 其他值说明: CHANNEL 渠道标识 IsProduction 是否生产环境(暂未启用) - + ###更多 - [JPush官网文档](http://docs.jpush.io/) \ No newline at end of file + [JPush官网文档](http://docs.jpush.io/) diff --git a/example/index.html b/example/index.html index 28a093a..6d51792 100644 --- a/example/index.html +++ b/example/index.html @@ -105,6 +105,7 @@ window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0); } else { window.plugins.jPushPlugin.setDebugMode(true); + window.plugins.jPushPlugin.setStatisticsOpen(true); } } catch (exception) { @@ -220,5 +221,3 @@ - - diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index 5d4b71e..67f7f4e 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -1,14 +1,9 @@ package cn.jpush.phonegap; -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.Set; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import android.content.Context; +import android.util.Log; + +import com.jpushphonegapdemo.jpush.R; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; @@ -17,406 +12,399 @@ import org.apache.cordova.CordovaWebView; import org.json.JSONArray; import org.json.JSONException; 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.CustomPushNotificationBuilder; import cn.jpush.android.api.JPushInterface; -import cn.jpush.android.data.JPushLocalNotification; import cn.jpush.android.api.TagAliasCallback; -import android.util.Log; +import cn.jpush.android.data.JPushLocalNotification; public class JPushPlugin extends CordovaPlugin { - private final static List methodList = - Arrays.asList( - "getRegistrationID", - "setTags", - "setTagsWithAlias", - "setAlias", - "getNotification", - "setBasicPushNotificationBuilder", - "setCustomPushNotificationBuilder", - "setPushTime", - "init", - "setDebugMode", - "stopPush", - "resumePush", - "isPushStopped", - "setLatestNotificationNum", - "setPushTime", - "clearAllNotification", - "clearNotificationById", - "addLocalNotification", - "removeLocalNotification", - "clearLocalNotifications", - "onResume", - "onPause", - "reportNotificationOpened"); - - private ExecutorService threadPool = Executors.newFixedThreadPool(1); - private static JPushPlugin instance; + private final static List methodList = + Arrays.asList( + "getRegistrationID", + "setTags", + "setTagsWithAlias", + "setAlias", + "getNotification", + "setBasicPushNotificationBuilder", + "setCustomPushNotificationBuilder", + "setPushTime", + "init", + "setDebugMode", + "stopPush", + "resumePush", + "isPushStopped", + "setLatestNotificationNum", + "setPushTime", + "clearAllNotification", + "clearNotificationById", + "addLocalNotification", + "removeLocalNotification", + "clearLocalNotifications", + "onResume", + "onPause", + "reportNotificationOpened", + "setStatisticsOpen"); + + private ExecutorService threadPool = Executors.newFixedThreadPool(1); + private static JPushPlugin instance; 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 Map notificationExtras=new HashMap(); - public static String openNotificationAlert; - public static Map openNotificationExtras=new HashMap(); + public static String notificationAlert; + public static Map notificationExtras = new HashMap(); + public static String openNotificationAlert; + public static Map openNotificationExtras = new HashMap(); + public JPushPlugin() { + instance = this; + } - public JPushPlugin() { - instance = this; - } + @Override + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); - @Override - public void initialize(CordovaInterface cordova, CordovaWebView webView) { - super.initialize(cordova, webView); + Log.i(TAG, "---------------- initialize" + "-" + JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert); - Log.i(TAG, "---------------- initialize"+"-"+JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert); + shouldCacheMsg = false; - shouldCacheMsg = false; + //如果同时缓存了打开事件openNotificationAlert 和 消息事件notificationAlert,只向UI 发 打开事件。 + //这样做是为了和iOS 统一 + if (JPushPlugin.openNotificationAlert != null) { + JPushPlugin.notificationAlert = null; + JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert, + JPushPlugin.openNotificationExtras); + } + if (JPushPlugin.notificationAlert != null) { + JPushPlugin.transmitReceive(JPushPlugin.notificationAlert, + JPushPlugin.notificationExtras); + } + //JPushInterface.init(cordova.getActivity().getApplicationContext()); + } - //如果同时缓存了打开事件openNotificationAlert 和 消息事件notificationAlert,只向UI 发 打开事件。 - //这样做是为了和iOS 统一 - if(JPushPlugin.openNotificationAlert != null){ - JPushPlugin.notificationAlert = null; - JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert, JPushPlugin.openNotificationExtras); - } - if(JPushPlugin.notificationAlert!=null){ - JPushPlugin.transmitReceive(JPushPlugin.notificationAlert, JPushPlugin.notificationExtras); - } + public void onPause(boolean multitasking) { + Log.i(TAG, "---------------- onPause"); + shouldCacheMsg = true; + if (isStatisticsOpened) { + JPushInterface.onPause(this.cordova.getActivity()); + } + } + public void onResume(boolean multitasking) { + shouldCacheMsg = false; + Log.i(TAG, "---------------- onResume" + "-" + JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert); + if (isStatisticsOpened) { + JPushInterface.onResume(this.cordova.getActivity()); + } + if (JPushPlugin.openNotificationAlert != null) { + JPushPlugin.notificationAlert = null; + JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert, + JPushPlugin.openNotificationExtras); + } + if (JPushPlugin.notificationAlert != null) { + JPushPlugin.transmitReceive(JPushPlugin.notificationAlert, + JPushPlugin.notificationExtras); + } + } - //JPushInterface.init(cordova.getActivity().getApplicationContext()); - } - - - - public void onPause(boolean multitasking) { - Log.i(TAG, "---------------- onPause"); - shouldCacheMsg = true; - } - - public void onResume(boolean multitasking) { - shouldCacheMsg = false; - Log.i(TAG, "---------------- onResume"+"-"+JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert); - - if(JPushPlugin.openNotificationAlert != null){ - JPushPlugin.notificationAlert = null; - JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert, JPushPlugin.openNotificationExtras); - } - if(JPushPlugin.notificationAlert!=null){ - JPushPlugin.transmitReceive(JPushPlugin.notificationAlert, JPushPlugin.notificationExtras); - } - } - - - private static JSONObject notificationObject(String message, - Map extras) { - JSONObject data = new JSONObject(); - try { - data.put("message", message); - JSONObject jExtras = new JSONObject(); - for(Entry entry:extras.entrySet()){ - if(entry.getKey().equals("cn.jpush.android.EXTRA")){ - JSONObject jo = new JSONObject((String)entry.getValue()); + private static JSONObject notificationObject(String message, + Map extras) { + JSONObject data = new JSONObject(); + try { + data.put("message", message); + JSONObject jExtras = new JSONObject(); + for (Entry entry : extras.entrySet()) { + if (entry.getKey().equals("cn.jpush.android.EXTRA")) { + JSONObject jo = new JSONObject((String) entry.getValue()); jExtras.put("cn.jpush.android.EXTRA", jo); } else { - jExtras.put(entry.getKey(),entry.getValue()); + jExtras.put(entry.getKey(), entry.getValue()); } - } - if(jExtras.length()>0) - { - data.put("extras", jExtras); - } - } catch (JSONException e) { + } + if (jExtras.length() > 0) { + data.put("extras", jExtras); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return data; + } - } - return data; - } + private static JSONObject openNotificationObject(String alert, + Map extras) { + JSONObject data = new JSONObject(); + try { + data.put("alert", alert); + JSONObject jExtras = new JSONObject(); + for (Entry entry : extras.entrySet()) { + if (entry.getKey().equals("cn.jpush.android.EXTRA")) { + JSONObject jo = new JSONObject((String) entry.getValue()); + jExtras.put("cn.jpush.android.EXTRA", jo); + } else { + jExtras.put(entry.getKey(), entry.getValue()); + } + } + if (jExtras.length() > 0) { + data.put("extras", jExtras); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return data; + } - private static JSONObject openNotificationObject(String alert, - Map extras){ - JSONObject data = new JSONObject(); - try{ - data.put("alert", alert); - JSONObject jExtras = new JSONObject(); - for(Entry entry:extras.entrySet()){ - if(entry.getKey().equals("cn.jpush.android.EXTRA")){ - JSONObject jo = new JSONObject((String)entry.getValue()); - jExtras.put("cn.jpush.android.EXTRA", jo); - }else{ - jExtras.put(entry.getKey(),entry.getValue()); - } - } - if(jExtras.length()>0) - { - data.put("extras", jExtras); - } - } catch (JSONException e) { - - } - return data; - } - static void transmitPush(String message, Map extras) { - if (instance == null) { - return; - } - JSONObject data = notificationObject(message, extras); - String js = String - .format("window.plugins.jPushPlugin.receiveMessageInAndroidCallback('%s');", - data.toString()); - - - try { - instance.webView.sendJavascript(js); - + static void transmitPush(String message, Map extras) { + if (instance == null) { + return; + } + JSONObject data = notificationObject(message, extras); + String js = String.format( + "window.plugins.jPushPlugin.receiveMessageInAndroidCallback('%s');", + data.toString()); + try { + instance.webView.sendJavascript(js); // String jsEvent=String // .format("cordova.fireDocumentEvent('jpush.receiveMessage',%s)", // data.toString()); // instance.webView.sendJavascript(jsEvent); - } catch (NullPointerException e) { + } catch (Exception e) { + e.printStackTrace(); + } + } - } catch (Exception e) { + static void transmitOpen(String alert, Map extras) { + if (instance == null) { + return; + } + if (JPushPlugin.shouldCacheMsg) { + return; + } + Log.i(TAG, "---------------- transmitOpen"); - } - } - static void transmitOpen(String alert, Map extras) { - if (instance == null) { - return; - } - - if(JPushPlugin.shouldCacheMsg){ - return; - } - - Log.i(TAG, "---------------- transmitOpen"); - - JSONObject data = openNotificationObject(alert, extras); - String js = String - .format("window.plugins.jPushPlugin.openNotificationInAndroidCallback('%s');", - data.toString()); - - try { - instance.webView.sendJavascript(js); - + JSONObject data = openNotificationObject(alert, extras); + String js = String.format( + "window.plugins.jPushPlugin.openNotificationInAndroidCallback('%s');", + data.toString()); + try { + instance.webView.sendJavascript(js); // String jsEvent=String // .format("cordova.fireDocumentEvent('jpush.openNotification',%s)", // data.toString()); // instance.webView.sendJavascript(jsEvent); - } catch (NullPointerException e) { + } catch (Exception e) { + e.printStackTrace(); + } + JPushPlugin.openNotificationAlert = null; + } - } catch (Exception e) { + static void transmitReceive(String alert, Map extras) { + if (instance == null) { + return; + } + if (JPushPlugin.shouldCacheMsg) { + return; + } + JSONObject data = openNotificationObject(alert, extras); + String js = String.format( + "window.plugins.jPushPlugin.receiveNotificationInAndroidCallback('%s');", + data.toString()); + Log.i(TAG, "--------->" + js); + try { + instance.webView.sendJavascript(js); + } catch (Exception e) { + e.printStackTrace(); + } + JPushPlugin.notificationAlert = null; + } - } - JPushPlugin.openNotificationAlert = null; - } - static void transmitReceive(String alert, Map extras) { - if (instance == null) { - return; - } + @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() { + try { + Method method = JPushPlugin.class.getDeclaredMethod(action, + JSONArray.class, CallbackContext.class); + method.invoke(JPushPlugin.this, data, callbackContext); + } catch (Exception e) { + Log.e(TAG, e.toString()); + } + } + }); + return true; + } - if(JPushPlugin.shouldCacheMsg){ - return; - } + void init(JSONArray data, CallbackContext callbackContext) { + JPushInterface.init(this.cordova.getActivity().getApplicationContext()); + //callbackContext.success(); + } - JSONObject data = openNotificationObject(alert, extras); - String js = String - .format("window.plugins.jPushPlugin.receiveNotificationInAndroidCallback('%s');", - data.toString()); + void setDebugMode(JSONArray data, CallbackContext callbackContext) { + boolean mode; + try { + mode = data.getBoolean(0); + // if (mode.equals("true")) { + // JPushInterface.setDebugMode(true); + // } else if (mode.equals("false")) { + // JPushInterface.setDebugMode(false); + // } else { + // callbackContext.error("error mode"); + // } + JPushInterface.setDebugMode(mode); + callbackContext.success(); + } catch (JSONException e) { + e.printStackTrace(); + } + } - try { - - instance.webView.sendJavascript(js); + void stopPush(JSONArray data, CallbackContext callbackContext) { + JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext()); + callbackContext.success(); + } - } catch (NullPointerException e) { + void resumePush(JSONArray data, CallbackContext callbackContext) { + JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext()); + callbackContext.success(); + } - } catch (Exception e) { + void isPushStopped(JSONArray data, CallbackContext callbackContext) { + boolean isStopped = JPushInterface.isPushStopped( + this.cordova.getActivity().getApplicationContext()); + if (isStopped) { + callbackContext.success(1); + } else { + callbackContext.success(0); + } + } - } - JPushPlugin.notificationAlert = null; + void setLatestNotificationNum(JSONArray data, CallbackContext callbackContext) { + int num = -1; + try { + num = data.getInt(0); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("error reading num json"); + } + if (num != -1) { + JPushInterface.setLatestNotificationNumber( + this.cordova.getActivity().getApplicationContext(), num); + } else { + callbackContext.error("error num"); + } + } - } + void setPushTime(JSONArray data, CallbackContext callbackContext) { + Set days = new HashSet(); + JSONArray dayArray; + int startHour = -1; + int endHour = -1; + try { + dayArray = data.getJSONArray(0); + for (int i = 0; i < dayArray.length(); i++) { + days.add(dayArray.getInt(i)); + } + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("error reading days json"); + } + try { + startHour = data.getInt(1); + endHour = data.getInt(2); + } catch (JSONException e) { + callbackContext.error("error reading hour json"); + } + Context context = this.cordova.getActivity().getApplicationContext(); + JPushInterface.setPushTime(context, days, startHour, endHour); + callbackContext.success(); + } - @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() { - try { - Method method = JPushPlugin.class.getDeclaredMethod(action, - JSONArray.class, CallbackContext.class); - method.invoke(JPushPlugin.this, data, callbackContext); - } catch (Exception e) { - Log.e(TAG,e.toString()); - } - } - }); - return true; - } - - void init(JSONArray data,CallbackContext callbackContext){ - JPushInterface.init(this.cordova.getActivity().getApplicationContext()); - //callbackContext.success(); - } - - void setDebugMode(JSONArray data, CallbackContext callbackContext) { - boolean mode; - try { - mode = data.getBoolean(0); - // if (mode.equals("true")) { - // JPushInterface.setDebugMode(true); - // } else if (mode.equals("false")) { - // JPushInterface.setDebugMode(false); - // } else { - // callbackContext.error("error mode"); - // } - JPushInterface.setDebugMode(mode); - callbackContext.success(); - } catch (JSONException e) { - } - } - - void stopPush(JSONArray data, - CallbackContext callbackContext){ - JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext()); - callbackContext.success(); - } - - void resumePush(JSONArray data, - CallbackContext callbackContext){ - JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext()); - callbackContext.success(); - } - - void isPushStopped(JSONArray data, - CallbackContext callbackContext){ - boolean isStopped =JPushInterface.isPushStopped(this.cordova.getActivity().getApplicationContext()); - if(isStopped){ - callbackContext.success(1); - }else{ - callbackContext.success(0); - } - } - - void setLatestNotificationNum(JSONArray data, - CallbackContext callbackContext){ - int num = -1; - try { - num = data.getInt(0); - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.error("error reading num json"); - } - if(num != -1){ - JPushInterface.setLatestNotificationNumber(this.cordova.getActivity().getApplicationContext(), num); - }else{ - callbackContext.error("error num"); - } - } - - void setPushTime(JSONArray data, - CallbackContext callbackContext){ - Set days = new HashSet(); - JSONArray dayArray; - int startHour = -1; - int endHour = -1; - try { - dayArray = data.getJSONArray(0); - for (int i = 0; i < dayArray.length(); i++) { - days.add(dayArray.getInt(i)); - } - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.error("error reading days json"); - } - try{ - startHour = data.getInt(1); - endHour = data.getInt(2); - }catch(JSONException e){ - callbackContext.error("error reading hour json"); - } - JPushInterface.setPushTime(this.cordova.getActivity().getApplicationContext(), days, startHour, endHour); - callbackContext.success(); - } - - void getRegistrationID(JSONArray data, CallbackContext callbackContext) { - String regID= JPushInterface.getRegistrationID(this.cordova.getActivity().getApplicationContext()); - callbackContext.success(regID); + void getRegistrationID(JSONArray data, CallbackContext callbackContext) { + Context context = this.cordova.getActivity().getApplicationContext(); + String regID = JPushInterface.getRegistrationID(context); + callbackContext.success(regID); + } - } - void onResume(JSONArray data, CallbackContext callbackContext) { - JPushInterface.onResume(this.cordova.getActivity()); - } - void onPause(JSONArray data, CallbackContext callbackContext) { - JPushInterface.onPause(this.cordova.getActivity()); - } - void reportNotificationOpened(JSONArray data, CallbackContext callbackContext) { - try { - String msgID; - msgID = data.getString(0); - JPushInterface.reportNotificationOpened(this.cordova.getActivity(),msgID); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + void onResume(JSONArray data, CallbackContext callbackContext) { + JPushInterface.onResume(this.cordova.getActivity()); + } - } - void setTags(JSONArray data, CallbackContext callbackContext) { + void onPause(JSONArray data, CallbackContext callbackContext) { + JPushInterface.onPause(this.cordova.getActivity()); + } - try { - HashSet tags=new HashSet(); - for(int i=0;i tags = new HashSet(); + for (int i = 0; i < data.length(); i++) { + tags.add(data.getString(i)); + } + JPushInterface.setTags(this.cordova.getActivity() + .getApplicationContext(), tags, mTagWithAliasCallback); + callbackContext.success(); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("Error reading tags JSON"); + } + } - void setAlias(JSONArray data, CallbackContext callbackContext) { - try { - String alias = data.getString(0); - JPushInterface.setAlias(this.cordova.getActivity() - .getApplicationContext(), alias,mTagWithAliasCallback); - callbackContext.success(); - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.error("Error reading alias JSON"); - } - } + void setAlias(JSONArray data, CallbackContext callbackContext) { + try { + String alias = data.getString(0); + JPushInterface.setAlias(this.cordova.getActivity() + .getApplicationContext(), alias, mTagWithAliasCallback); + callbackContext.success(); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("Error reading alias JSON"); + } + } - void setTagsWithAlias(JSONArray data, CallbackContext callbackContext) { - HashSet tags = new HashSet(); - String alias; - try { - alias = data.getString(0); - JSONArray tagsArray = data.getJSONArray(1); - for (int i = 0; i < tagsArray.length(); i++) { - tags.add(tagsArray.getString(i)); - } - - JPushInterface.setAliasAndTags(this.cordova.getActivity() - .getApplicationContext(), alias, tags,mTagWithAliasCallback); - callbackContext.success(); - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.error("Error reading tagAlias JSON"); - } - } + void setTagsWithAlias(JSONArray data, CallbackContext callbackContext) { + HashSet tags = new HashSet(); + String alias; + try { + alias = data.getString(0); + JSONArray tagsArray = data.getJSONArray(1); + for (int i = 0; i < tagsArray.length(); i++) { + tags.add(tagsArray.getString(i)); + } + JPushInterface.setAliasAndTags(this.cordova.getActivity() + .getApplicationContext(), alias, tags, mTagWithAliasCallback); + callbackContext.success(); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("Error reading tagAlias JSON"); + } + } // void getNotification(JSONArray data, CallbackContext callBackContext) { // String alert = JPushPlugin.notificationAlert; @@ -436,123 +424,123 @@ public class JPushPlugin extends CordovaPlugin { // JPushPlugin.notificationExtras = new HashMap(); // } - void setBasicPushNotificationBuilder(JSONArray data, - CallbackContext callbackContext) { - BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder( - this.cordova.getActivity()); - builder.developerArg0 = "Basic builder 1"; - JPushInterface.setPushNotificationBuilder(1, builder); - JSONObject obj = new JSONObject(); - try { - obj.put("id", 1); - } catch (JSONException e) { - e.printStackTrace(); - } - //callbackContext.success(obj); - } + void setBasicPushNotificationBuilder(JSONArray data, + CallbackContext callbackContext) { + BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder( + this.cordova.getActivity()); + builder.developerArg0 = "Basic builder 1"; + JPushInterface.setPushNotificationBuilder(1, builder); + JSONObject obj = new JSONObject(); + try { + obj.put("id", 1); + } catch (JSONException e) { + e.printStackTrace(); + } + //callbackContext.success(obj); + } - void setCustomPushNotificationBuilder(JSONArray data, - 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"; - builder.layoutIconDrawable = R.drawable.jpush_notification_icon; - JPushInterface.setPushNotificationBuilder(2, builder); - JSONObject obj = new JSONObject(); - try { - obj.put("id", 2); - } catch (JSONException e) { - e.printStackTrace(); - } - //callbackContext.success(obj); - } - - void clearAllNotification(JSONArray data, - CallbackContext callbackContext){ - JPushInterface.clearAllNotifications(this.cordova.getActivity()); - //callbackContext.success(); - } - - void clearNotificationById(JSONArray data, - CallbackContext callbackContext){ - int notificationId=-1; - try { - notificationId = data.getInt(0); - } catch (JSONException e) { - e.printStackTrace(); - callbackContext.error("error reading id json"); - } - if(notificationId != -1){ - JPushInterface.clearNotificationById(this.cordova.getActivity(), notificationId); - }else{ - callbackContext.error("error id"); - } - } - void addLocalNotification(JSONArray data, - CallbackContext callbackContext) throws JSONException{ - //builderId,content,title,notificaitonID,broadcastTime,extras - - int builderId=data.getInt(0); - String content =data.getString(1); - String title = data.getString(2); - int notificationID= data.getInt(3); - int broadcastTime=data.getInt(4); - JSONObject extras=data.getJSONObject(5); - - JPushLocalNotification ln = new JPushLocalNotification(); - ln.setBuilderId(builderId); - ln.setContent(content); - ln.setTitle(title); - ln.setNotificationId(notificationID) ; - ln.setBroadcastTime(System.currentTimeMillis() + broadcastTime); + void setCustomPushNotificationBuilder(JSONArray data, + 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"; + builder.layoutIconDrawable = R.drawable.jpush_notification_icon; + JPushInterface.setPushNotificationBuilder(2, builder); + JSONObject obj = new JSONObject(); + try { + obj.put("id", 2); + } catch (JSONException e) { + e.printStackTrace(); + } + //callbackContext.success(obj); + } - ln.setExtras(extras.toString()) ; - JPushInterface.addLocalNotification(this.cordova.getActivity(), ln); - - } - void removeLocalNotification(JSONArray data, - CallbackContext callbackContext) throws JSONException{ - - int notificationID=data.getInt(0); - JPushInterface.removeLocalNotification(this.cordova.getActivity(),notificationID); + void clearAllNotification(JSONArray data, CallbackContext callbackContext) { + JPushInterface.clearAllNotifications(this.cordova.getActivity()); + //callbackContext.success(); + } - } - void clearLocalNotifications(JSONArray data, - CallbackContext callbackContext){ - - JPushInterface.clearLocalNotifications(this.cordova.getActivity()); + void clearNotificationById(JSONArray data, CallbackContext callbackContext) { + int notificationId = -1; + try { + notificationId = data.getInt(0); + } catch (JSONException e) { + e.printStackTrace(); + callbackContext.error("error reading id json"); + } + if (notificationId != -1) { + JPushInterface.clearNotificationById(this.cordova.getActivity(), + notificationId); + } else { + callbackContext.error("error id"); + } + } - } - - private final TagAliasCallback mTagWithAliasCallback = new TagAliasCallback() { + void addLocalNotification(JSONArray data, CallbackContext callbackContext) + throws JSONException { + //builderId,content,title,notificaitonID,broadcastTime,extras + int builderId = data.getInt(0); + String content = data.getString(1); + String title = data.getString(2); + int notificationID = data.getInt(3); + int broadcastTime = data.getInt(4); + JSONObject extras = data.getJSONObject(5); + JPushLocalNotification ln = new JPushLocalNotification(); + ln.setBuilderId(builderId); + ln.setContent(content); + ln.setTitle(title); + ln.setNotificationId(notificationID); + ln.setBroadcastTime(System.currentTimeMillis() + broadcastTime); + + ln.setExtras(extras.toString()); + JPushInterface.addLocalNotification(this.cordova.getActivity(), ln); + } + + void removeLocalNotification(JSONArray data, CallbackContext callbackContext) + throws JSONException { + int notificationID = data.getInt(0); + JPushInterface.removeLocalNotification(this.cordova.getActivity(), + notificationID); + } + + void clearLocalNotifications(JSONArray data, CallbackContext callbackContext) { + 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() { @Override public void gotResult(int code, String alias, Set tags) { - if (instance == null) { - return; - } - - JSONObject data = new JSONObject(); - try { - data.put("resultCode", code); - data.put("tags", tags); - data.put("alias", alias); - - String jsEvent=String - .format("cordova.fireDocumentEvent('jpush.setTagsWithAlias',%s)", - data.toString()); - instance.webView.sendJavascript(jsEvent); - - - } catch (JSONException e) { - - } - + if (instance == null) { + return; + } + JSONObject data = new JSONObject(); + try { + data.put("resultCode", code); + data.put("tags", tags); + data.put("alias", alias); + String jsEvent = String.format( + "cordova.fireDocumentEvent('jpush.setTagsWithAlias',%s)", + data.toString()); + instance.webView.sendJavascript(jsEvent); + } catch (JSONException e) { + e.printStackTrace(); + } } - - }; - - - + }; + } diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index 099252b..61daa9f 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -18,28 +18,28 @@ JPushPlugin.prototype.error_callback = function(msg){ console.log("Javascript Callback Error: " + msg) } -JPushPlugin.prototype.call_native = function(name, args, callback){ +JPushPlugin.prototype.call_native = function(name, args, callback){ ret = cordova.exec(callback,this.error_callback,'JPushPlugin',name,args); return ret; } //public plugin function -JPushPlugin.prototype.startLogPageView = function(pageName){ +JPushPlugin.prototype.startLogPageView = function(pageName){ if(this.isPlatformIOS()){ - this.call_native( "startLogPageView",[pageName],null); - } + this.call_native( "startLogPageView",[pageName],null); + } } JPushPlugin.prototype.stopLogPageView = function(pageName){ if(this.isPlatformIOS()){ - this.call_native( "stopLogPageView",[pageName],null); + this.call_native( "stopLogPageView",[pageName],null); } } JPushPlugin.prototype.beginLogPageView = function(pageName,duration){ if(this.isPlatformIOS()){ - this.call_native( "beginLogPageView",[pageName,duration],null); + this.call_native( "beginLogPageView",[pageName,duration],null); } } JPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge){ @@ -73,7 +73,7 @@ JPushPlugin.prototype.setTagsWithAlias = function(tags,alias){ } JPushPlugin.prototype.setTags = function(tags){ - + try{ this.call_native("setTags",tags,null); } @@ -83,15 +83,15 @@ JPushPlugin.prototype.setTags = function(tags){ } JPushPlugin.prototype.setAlias = function(alias){ - try{ + try{ this.call_native("setAlias",[alias],null); } - catch(exception){ + catch(exception){ console.log(exception); } } JPushPlugin.prototype.getRegistrationID = function(callback){ - + try{ var data=[]; this.call_native("getRegistrationID",[data],callback); @@ -102,8 +102,8 @@ JPushPlugin.prototype.getRegistrationID = function(callback){ } JPushPlugin.prototype.setBadge = function(value){ - - if(this.isPlatformIOS()){ + + if(this.isPlatformIOS()){ try{ this.call_native("setBadge",[value],null); } @@ -114,7 +114,7 @@ JPushPlugin.prototype.setBadge = function(value){ } } JPushPlugin.prototype.resetBadge = function(){ - + if(this.isPlatformIOS()){ try{ var data=[]; @@ -145,7 +145,7 @@ JPushPlugin.prototype.receiveMessageIniOSCallback = function(data){ var content = bToObj.content; console.log(content); } - catch(exception){ + catch(exception){ console.log("JPushPlugin:receiveMessageIniOSCallback"+exception); } } @@ -164,7 +164,7 @@ JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){ //console.log(extras['cn.jpush.android.CONTENT_TYPE']); //console.log(extras['cn.jpush.android.EXTRA']); } - catch(exception){ + catch(exception){ console.log("JPushPlugin:pushCallback "+exception); } } @@ -174,9 +174,9 @@ JPushPlugin.prototype.openNotificationInAndroidCallback = function(data){ try{ console.log("JPushPlugin:openNotificationInAndroidCallback"); var bToObj = JSON.parse(data); - this.openNotification=bToObj; + this.openNotification=bToObj; cordova.fireDocumentEvent('jpush.openNotification',null); - + //console.log(data); //var bToObj = JSON.parse(data); //var alert = bToObj.alert; @@ -191,7 +191,7 @@ JPushPlugin.prototype.openNotificationInAndroidCallback = function(data){ //console.log(extras['cn.jpush.android.NOTIFICATION_ID']); //console.log("JPushPlugin:openNotificationCallback is ready"); } - catch(exception){ + catch(exception){ console.log(exception); } } @@ -199,9 +199,9 @@ JPushPlugin.prototype.receiveNotificationInAndroidCallback = function(data){ try{ console.log("JPushPlugin:receiveNotificationInAndroidCallback"); var bToObj = JSON.parse(data); - this.receiveNotification=bToObj; + this.receiveNotification=bToObj; cordova.fireDocumentEvent('jpush.receiveNotification',null); - + //console.log(data); //var bToObj = JSON.parse(data); //var alert = bToObj.alert; @@ -216,7 +216,7 @@ JPushPlugin.prototype.receiveNotificationInAndroidCallback = function(data){ //console.log(extras['cn.jpush.android.NOTIFICATION_ID']); //console.log("JPushPlugin:openNotificationCallback is ready"); } - catch(exception){ + catch(exception){ console.log(exception); } } @@ -244,7 +244,7 @@ JPushPlugin.prototype.stopPush = function(){ JPushPlugin.prototype.resumePush = function(){ data=[] this.call_native("resumePush",data,null); - + } JPushPlugin.prototype.setDebugMode = function(mode){ if(device.platform == "Android") { @@ -276,7 +276,7 @@ JPushPlugin.prototype.isPushStopped = function(callback){ data=[]; this.call_native("isPushStopped",data,callback) - + } JPushPlugin.prototype.init = function(){ @@ -288,7 +288,7 @@ JPushPlugin.prototype.init = function(){ this.call_native("init",data,null); } - + } JPushPlugin.prototype.setDebugMode = function(mode){ @@ -316,11 +316,21 @@ JPushPlugin.prototype.clearLocalNotifications = function(){ JPushPlugin.prototype.reportNotificationOpened = function(msgID){ if(device.platform == "Android") { - + this.call_native("reportNotificationOpened",[msgID],null); } } +/** + *是否开启统计分析功能,用于“用户使用时长”,“活跃用户”,“用户打开次数”的统计,并上报到服务器上, + *在 Portal 上展示给开发者。 + **/ +JPushPlugin.prototype.setStatisticsOpen = function(mode) { + if(device.platform == "Android") { + this.call_native("setStatisticsOpen", [mode], null); + } +} + //iOS single @@ -330,9 +340,6 @@ if(!window.plugins){ if(!window.plugins.jPushPlugin){ window.plugins.jPushPlugin = new JPushPlugin(); -} - -module.exports = new JPushPlugin(); - - +} +module.exports = new JPushPlugin();