From 2075f3360dd3fe73b954105365e000c904209a40 Mon Sep 17 00:00:00 2001 From: Cyril Joui Date: Sat, 21 Feb 2015 23:09:10 +0100 Subject: [PATCH] [MAYFIX] - Issue466 (https://github.com/phonegap-build/PushPlugin/issues/466) Optimize registration process done @every application start (if already registered, don't call GCM.register) --- .../com/plugin/gcm/GCMIntentService.java | 20 +--------- src/android/com/plugin/gcm/PushPlugin.java | 39 ++++++++++++++++++- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index caee145e..ec2f0707 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -28,26 +28,8 @@ public GCMIntentService() { public void onRegistered(Context context, String regId) { Log.v(TAG, "onRegistered: "+ regId); + PushPlugin.sendJavascriptRegistered(regId); - JSONObject json; - - try - { - json = new JSONObject().put("event", "registered"); - json.put("regid", regId); - - Log.v(TAG, "onRegistered: " + json.toString()); - - // Send this JSON data to the JavaScript application above EVENT should be set to the msg type - // In this case this is the registration ID - PushPlugin.sendJavascript( json ); - - } - catch( JSONException e) - { - // No message to the user is sent, JSON failed - Log.e(TAG, "onRegistered: JSON exception"); - } } @Override diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index 3e390856..6ca19ff8 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -4,7 +4,9 @@ import android.content.Context; import android.os.Bundle; import android.util.Log; + import com.google.android.gcm.GCMRegistrar; + import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; @@ -30,7 +32,7 @@ public class PushPlugin extends CordovaPlugin { private static String gECB; private static String gSenderID; private static Bundle gCachedExtras = null; - private static boolean gForeground = false; + private static boolean gForeground = true; /** * Gets the application context from cordova's main activity. @@ -62,7 +64,18 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); - GCMRegistrar.register(getApplicationContext(), gSenderID); + // Already registered? + String regId = GCMRegistrar.getRegistrationId(getApplicationContext()); + if (regId == null) { + Log.d(TAG, "New registration needed"); + GCMRegistrar.register(getApplicationContext(), gSenderID); + } else { + Log.d(TAG, "Already registered! go on directly ..."); + callbackContext.success(); + // Result + sendJavascriptRegistered(regId); + } + result = true; callbackContext.success(); } catch (JSONException e) { @@ -242,4 +255,26 @@ public static boolean isActive() { return gWebView != null; } + + public static void sendJavascriptRegistered(String regId) { + JSONObject json; + + try + { + json = new JSONObject().put("event", "registered"); + json.put("regid", regId); + + Log.v(TAG, "onRegistered: " + json.toString()); + + // Send this JSON data to the JavaScript application above EVENT should be set to the msg type + // In this case this is the registration ID + sendJavascript( json ); + + } + catch( JSONException e) + { + // No message to the user is sent, JSON failed + Log.e(TAG, "onRegistered: JSON exception"); + } + } }