Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAYFIX] - Issue466 #499

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions src/android/com/plugin/gcm/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 37 additions & 2 deletions src/android/com/plugin/gcm/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
}
}
}