From 5ed47781ed5129c9d8c4c68f7cd15450fb457a40 Mon Sep 17 00:00:00 2001 From: ghominejad Date: Sun, 22 Feb 2015 15:37:43 +0330 Subject: [PATCH] Checking internet connectivity before calling register In the most of times calling `GCMRegistrar.register` function with no internet access causes generating new registration id in the next runs (one or two) . We call the `GCMRegistrar.register` if the network is available. --- src/android/com/plugin/gcm/PushPlugin.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/android/com/plugin/gcm/PushPlugin.java b/src/android/com/plugin/gcm/PushPlugin.java index 3e390856..4de02a67 100644 --- a/src/android/com/plugin/gcm/PushPlugin.java +++ b/src/android/com/plugin/gcm/PushPlugin.java @@ -4,6 +4,9 @@ import android.content.Context; import android.os.Bundle; import android.util.Log; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + import com.google.android.gcm.GCMRegistrar; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; @@ -61,8 +64,11 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo gSenderID = (String) jo.get("senderID"); Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + gSenderID); - - GCMRegistrar.register(getApplicationContext(), gSenderID); + + if (isNetworkAvailable()) { + GCMRegistrar.register(getApplicationContext(), gSenderID); + } + result = true; callbackContext.success(); } catch (JSONException e) { @@ -232,7 +238,14 @@ else if (strValue.startsWith("[")) } return null; } - + + private boolean isNetworkAvailable() { + ConnectivityManager connectivityManager + = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + return activeNetworkInfo != null && activeNetworkInfo.isConnected(); + } + public static boolean isInForeground() { return gForeground;