update doc & add some new features

支持安卓直接获得通知标题;
优化安卓获取附加字段的方法。
This commit is contained in:
Hevin 2016-04-09 18:06:11 +08:00
parent 1ff72e4df4
commit dfb883e24c
6 changed files with 63 additions and 27 deletions

View File

@ -63,14 +63,14 @@ JPush-PhoneGap-Plugin 支持 iOS, Android 的推送插件。
window.plugins.jPushPlugin.init();
### Android 手安装
### Android 手安装
[Android 手安装文档地址](document/Android_handle_install.md)
[Android 手安装文档地址](document/Android_handle_install.md)
### iOS 手安装
### iOS 手安装
[IOS手安装文档地址](document/iOS_handle_install.md)
[IOS手安装文档地址](document/iOS_handle_install.md)
###示例

View File

@ -1,13 +1,21 @@
#Android安装
#Android安装
下载 JPush PhoneGap 插件,并解压缩,标记插件目录为:`$JPUSH_PLUGIN_DIR`
1. 复制:`$JPUSH_PLUGIN_DIR`/src/android/*.java 到 cn/jpush/phonega/ 目录下(即cn.jpush.phonegap 的包下)
2. 复制:`$JPUSH_PLUGIN_DIR`/src/android/armeabi/libjpush.so 到 lib/armeabi/
3. 复制:`$JPUSH_PLUGIN_DIR`/src/android/jpush-sdk-release2.0.6.jar 到 lib/
4. 复制:`$JPUSH_PLUGIN_DIR`/src/android/test_notification_layout.xml 到 res/layout/
5. 复制:`$JPUSH_PLUGIN_DIR`/src/android/jpush_notification_icon.png 到 res/drawable/
6. 修改 AndroidManifest.xml 在 manifest 节点下添加以下权限
1. 复制:$JPUSH_PLUGIN_DIR/src/android/*.java 到 cn/jpush/phonega/ 目录下(即cn.jpush.phonegap 的包下)
2. 复制:$JPUSH_PLUGIN_DIR/src/android/armeabi/libjpush.so 到 lib/armeabi/
3. 复制:$JPUSH_PLUGIN_DIR/src/android/jpush-sdk-release2.0.6.jar 到 lib/
4. 复制:$JPUSH_PLUGIN_DIR/src/android/test_notification_layout.xml 到 res/layout/
5. 复制:$JPUSH_PLUGIN_DIR/src/android/jpush_notification_icon.png 到 res/drawable/
6. 复制:$JPUSH_PLUGIN_DIR/www/JPushPlugin.js 到 assets/www/plugins/cn.jpush.phonegap.JPushPlugin.www/
和 platform_www/plugins/cn.jpush.phonegap.JPushPlugin/www/
并在 JPushPlugin.js 文件的代码外添加:
cordova.define("cn.jpush.phonegap.JPushPlugin.JPushPlugin", function(require, exports, module) {
// 代码
});
7. 修改 AndroidManifest.xml 在 manifest 节点下添加以下权限
<!-- Required 一些系统要求的权限,如访问网络等-->
<uses-permission android:name="$PACKAGE_NAME.permission.JPUSH_MESSAGE"/>

View File

@ -1,4 +1,4 @@
### iOS手安装
### iOS手安装
下载 JPush PhoneGap 插件,并解压缩,标记插件目录为:`$JPUSH_PLUGIN_DIR`

View File

@ -66,7 +66,6 @@
alertContent = event.aps.alert;
}
$("#notificationResult").html(alertContent);
} catch (exeption) {
console.log(exception)
}
@ -90,7 +89,6 @@
try {
window.plugins.jPushPlugin.init();
getRegistrationID();
if (device.platform != "Android") {
window.plugins.jPushPlugin.setDebugModeFromIos();
window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);

View File

@ -70,8 +70,11 @@ public class JPushPlugin extends CordovaPlugin {
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>();
public static String openNotificationTitle;
public static String openNotificationAlert;
public static Map<String, Object> openNotificationExtras = new HashMap<String, Object>();
@ -93,11 +96,13 @@ public class JPushPlugin extends CordovaPlugin {
//这样做是为了和iOS 统一
if (JPushPlugin.openNotificationAlert != null) {
JPushPlugin.notificationAlert = null;
JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert,
JPushPlugin.transmitOpen(JPushPlugin.notificationTitle,
JPushPlugin.openNotificationAlert,
JPushPlugin.openNotificationExtras);
}
if (JPushPlugin.notificationAlert != null) {
JPushPlugin.transmitReceive(JPushPlugin.notificationAlert,
JPushPlugin.transmitReceive(JPushPlugin.notificationTitle,
JPushPlugin.notificationAlert,
JPushPlugin.notificationExtras);
}
//JPushInterface.init(cordova.getActivity().getApplicationContext());
@ -121,11 +126,13 @@ public class JPushPlugin extends CordovaPlugin {
}
if (JPushPlugin.openNotificationAlert != null) {
JPushPlugin.notificationAlert = null;
JPushPlugin.transmitOpen(JPushPlugin.openNotificationAlert,
JPushPlugin.transmitOpen(JPushPlugin.notificationTitle,
JPushPlugin.openNotificationAlert,
JPushPlugin.openNotificationExtras);
}
if (JPushPlugin.notificationAlert != null) {
JPushPlugin.transmitReceive(JPushPlugin.notificationAlert,
JPushPlugin.transmitReceive(JPushPlugin.notificationTitle,
JPushPlugin.notificationAlert,
JPushPlugin.notificationExtras);
}
}
@ -139,6 +146,13 @@ public class JPushPlugin extends CordovaPlugin {
for (Entry<String, Object> entry : extras.entrySet()) {
if (entry.getKey().equals("cn.jpush.android.EXTRA")) {
JSONObject jo = new JSONObject((String) entry.getValue());
String key;
Iterator keys = jo.keys();
while(keys.hasNext()) {
key = keys.next().toString();
Log.i(TAG, key);
jExtras.put(key, jo.getString(key));
}
jExtras.put("cn.jpush.android.EXTRA", jo);
} else {
jExtras.put(entry.getKey(), entry.getValue());
@ -153,15 +167,22 @@ public class JPushPlugin extends CordovaPlugin {
return data;
}
private static JSONObject openNotificationObject(String alert,
Map<String, Object> extras) {
private static JSONObject openNotificationObject(String title,
String alert, Map<String, Object> extras) {
JSONObject data = new JSONObject();
try {
data.put("title", title);
data.put("alert", alert);
JSONObject jExtras = new JSONObject();
for (Entry<String, Object> entry : extras.entrySet()) {
if (entry.getKey().equals("cn.jpush.android.EXTRA")) {
JSONObject jo = new JSONObject((String) entry.getValue());
String key;
Iterator keys = jo.keys();
while(keys.hasNext()) {
key = keys.next().toString();
jExtras.put(key, jo.getString(key));
}
jExtras.put("cn.jpush.android.EXTRA", jo);
} else {
jExtras.put(entry.getKey(), entry.getValue());
@ -191,14 +212,14 @@ public class JPushPlugin extends CordovaPlugin {
});
}
static void transmitOpen(String alert, Map<String, Object> extras) {
static void transmitOpen(String title, String alert, Map<String, Object> extras) {
if (instance == null) {
return;
}
if (JPushPlugin.shouldCacheMsg) {
return;
}
JSONObject data = openNotificationObject(alert, extras);
JSONObject data = openNotificationObject(title, alert, extras);
String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);";
final String js = String.format(format, data.toString());
cordovaActivity.runOnUiThread(new Runnable() {
@ -207,14 +228,15 @@ public class JPushPlugin extends CordovaPlugin {
instance.webView.loadUrl("javascript:" + js);
}
});
JPushPlugin.openNotificationTitle = null;
JPushPlugin.openNotificationAlert = null;
}
static void transmitReceive(String alert, Map<String, Object> extras) {
static void transmitReceive(String title, String alert, Map<String, Object> extras) {
if (instance == null) {
return;
}
JSONObject data = openNotificationObject(alert, extras);
JSONObject data = openNotificationObject(title, alert, extras);
String format = "window.plugins.jPushPlugin.receiveNotificationInAndroidCallback(%s);";
final String js = String.format(format, data.toString());
cordovaActivity.runOnUiThread(new Runnable() {
@ -223,6 +245,7 @@ public class JPushPlugin extends CordovaPlugin {
instance.webView.loadUrl("javascript:" + js);
}
});
JPushPlugin.notificationTitle = null;
JPushPlugin.notificationAlert = null;
}

View File

@ -44,13 +44,16 @@ public class MyReceiver extends BroadcastReceiver {
private void handlingNotificationOpen(Context context, Intent intent) {
Log.i(TAG, "---------------- handlingNotificationOpen");
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
JPushPlugin.openNotificationTitle = title;
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
JPushPlugin.openNotificationAlert = alert;
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.openNotificationExtras = extras;
JPushPlugin.transmitOpen(alert, extras);
JPushPlugin.transmitOpen(title, alert, extras);
Intent launch = context.getPackageManager().getLaunchIntentForPackage(
context.getPackageName());
@ -62,17 +65,21 @@ public class MyReceiver extends BroadcastReceiver {
private void handlingNotificationReceive(Context context, Intent intent) {
Log.i(TAG, "---------------- handlingNotificationReceive");
Intent launch = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
Intent launch = context.getPackageManager().getLaunchIntentForPackage(
context.getPackageName());
launch.addCategory(Intent.CATEGORY_LAUNCHER);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
JPushPlugin.notificationTitle = title;
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
JPushPlugin.notificationAlert = alert;
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.notificationExtras = extras;
JPushPlugin.transmitReceive(alert, extras);
JPushPlugin.transmitReceive(title, alert, extras);
}
private Map<String, Object> getNotificationExtras(Intent intent) {