diff --git a/doc/Android_detail_api.md b/doc/Android_detail_api.md index 7cb03b6..e90221d 100644 --- a/doc/Android_detail_api.md +++ b/doc/Android_detail_api.md @@ -14,6 +14,18 @@ - [本地通知](#本地通知) - [富媒体页面 JavaScript 回调 API](#富媒体页面-javascript-回调-api) +## 注册成功事件 +### jpush.receiveRegistrationId +集成了 JPush SDK 的应用程序在第一次成功注册到 JPush 服务器时,JPush 服务器会给客户端返回一个唯一的该设备的标识 - RegistrationID。 +就会触发这个事件(注意只有第一次会触发该事件,之后如果想要取到 registrationId,可以直接调用 *getRegistrationID* 方法)。 + +#### 代码示例 +```Javascript +document.addEventListener('jpush.receiveRegistrationId', function (event) { + console.log(event.registrationId) +}, false) +``` + ## 接收通知时获得通知的内容 - 内容: @@ -55,7 +67,6 @@ - true 显示集成日志。 - false 不显示集成日志。 - ## 接收消息和点击通知事件 ### API - receiveMessageInAndroidCallback @@ -82,7 +93,6 @@ - data: js 字符串。 - ## 统计分析 ### API - onResume / onPause diff --git a/package.json b/package.json index feb39bd..7a58c99 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jpush-phonegap-plugin", - "version": "3.1.1", + "version": "3.1.2", "description": "JPush for cordova plugin", "cordova": { "id": "jpush-phonegap-plugin", @@ -29,7 +29,7 @@ ], "peerDependencies": { "cordova-plugin-device": ">=1.0.0", - "cordova-plugin-jcore": "1.1.0" + "cordova-plugin-jcore": "1.1.1" }, "author": "JiGuang", "license": "MIT", diff --git a/plugin.xml b/plugin.xml index 7d556d4..be50de1 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="3.1.2"> JPush Plugin JPush for cordova plugin @@ -186,7 +186,7 @@ - + diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index c55e341..49a05f5 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -262,6 +262,26 @@ public class JPushPlugin extends CordovaPlugin { JPushPlugin.notificationAlert = null; } + static void transmitReceiveRegistrationId(String rId) { + if (instance == null) { + return; + } + JSONObject data = new JSONObject(); + try { + data.put("registrationId", rId); + } catch (JSONException e) { + e.printStackTrace(); + } + String format = "window.plugins.jPushPlugin.receiveRegistrationIdInAndroidCallback(%s);"; + final String js = String.format(format, data.toString()); + cordovaActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + instance.webView.loadUrl("javascript:" + js); + } + }); + } + @Override public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) throws JSONException { diff --git a/src/android/MyReceiver.java b/src/android/MyReceiver.java index bcea33a..06390b1 100644 --- a/src/android/MyReceiver.java +++ b/src/android/MyReceiver.java @@ -1,18 +1,18 @@ package cn.jpush.phonegap; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; + import cn.jpush.android.api.JPushInterface; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; public class MyReceiver extends BroadcastReceiver { - private static String TAG = "JPushPlugin"; + private static final List IGNORED_EXTRAS_KEYS = Arrays.asList( "cn.jpush.android.TITLE", @@ -24,18 +24,15 @@ public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(action)) { + if (action.equals(JPushInterface.ACTION_REGISTRATION_ID)) { + String rId = intent.getStringExtra(JPushInterface.EXTRA_REGISTRATION_ID); + JPushPlugin.transmitReceiveRegistrationId(rId); + } else if (action.equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) { handlingMessageReceive(intent); - } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(action)) { + } else if (action.equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) { handlingNotificationReceive(context, intent); - } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) { + } else if (action.equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) { handlingNotificationOpen(context, intent); - } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(action)) { - // 当在 HTML 页面中调用 JPushWeb.triggerNativeAction(String params) 方法时触发此方法, - // 再进行相关的操作。 - - } else { - Log.d(TAG, "Unhandled intent - " + action); } } @@ -46,8 +43,6 @@ 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; @@ -60,15 +55,13 @@ public class MyReceiver extends BroadcastReceiver { JPushPlugin.transmitNotificationOpen(title, alert, extras); Intent launch = context.getPackageManager().getLaunchIntentForPackage( - context.getPackageName()); + context.getPackageName()); launch.addCategory(Intent.CATEGORY_LAUNCHER); launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(launch); } private void handlingNotificationReceive(Context context, Intent intent) { - Log.i(TAG, "---------------- handlingNotificationReceive"); - Intent launch = context.getPackageManager().getLaunchIntentForPackage( context.getPackageName()); launch.addCategory(Intent.CATEGORY_LAUNCHER); @@ -99,5 +92,4 @@ public class MyReceiver extends BroadcastReceiver { } return extrasMap; } - } diff --git a/src/android/libs/jpush-android_v3.0.1.jar b/src/android/libs/jpush-android_v3.0.1.jar deleted file mode 100644 index ff4f27d..0000000 Binary files a/src/android/libs/jpush-android_v3.0.1.jar and /dev/null differ diff --git a/src/android/libs/jpush-android_v3.0.3.jar b/src/android/libs/jpush-android_v3.0.3.jar new file mode 100644 index 0000000..af976f8 Binary files /dev/null and b/src/android/libs/jpush-android_v3.0.3.jar differ diff --git a/src/ios/Plugins/AppDelegate+JPush.m b/src/ios/Plugins/AppDelegate+JPush.m index 862b1e7..1d8af8b 100644 --- a/src/ios/Plugins/AppDelegate+JPush.m +++ b/src/ios/Plugins/AppDelegate+JPush.m @@ -28,29 +28,27 @@ return [self init_plus]; } -NSDictionary *_launchOptions; +-(void)fireOpenNotification:(NSTimer*)timer{ + if (SharedJPushPlugin) { + [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[timer.userInfo toJsonString]]; + [timer invalidate]; + } +} +NSDictionary *_launchOptions; -(void)applicationDidLaunch:(NSNotification *)notification{ + if (notification) { if (notification.userInfo) { NSDictionary *userInfo1 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo1.count > 0) { - [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) { - if (SharedJPushPlugin) { - [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[userInfo1 toJsonString]]; - [timer invalidate]; - } - }]; + [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo1 repeats:YES]; } + NSDictionary *userInfo2 = [notification.userInfo valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (userInfo2.count > 0) { - [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) { - if (SharedJPushPlugin) { - [JPushPlugin fireDocumentEvent:JPushDocumentEvent_OpenNotification jsString:[userInfo2 toJsonString]]; - [timer invalidate]; - } - }]; + [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(fireOpenNotification:) userInfo:userInfo2 repeats:YES]; } } [JPUSHService setDebugMode]; @@ -64,7 +62,6 @@ NSDictionary *_launchOptions; if (![delay boolValue]) { [self startJPushSDK]; } - } } diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index 33cb3c8..ecc80eb 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -217,6 +217,14 @@ JPushPlugin.prototype.setCustomPushNotificationBuilder = function () { } } +JPushPlugin.prototype.receiveRegistrationIdInAndroidCallback = function (data) { + if (device.platform === 'Android') { + data = JSON.stringify(data) + var event = JSON.parse(data) + cordova.fireDocumentEvent('jpush.receiveRegistrationId', event); + } + } + JPushPlugin.prototype.receiveMessageInAndroidCallback = function (data) { data = JSON.stringify(data) console.log('JPushPlugin:receiveMessageInAndroidCallback: ' + data)