phonegap-build / PushPlugin

This repository is deprecated head to phonegap/phonegap-push-plugin
https://github.com/phonegap/phonegap-plugin-push
MIT License
1.31k stars 996 forks source link

GCM token NotRegistered #466

Open alextomas opened 9 years ago

alextomas commented 9 years ago

I have a problem with notifications GCM and tokens expiry

Check the token device and sending a notification, but over time the token is no longer valid and get an error: NotRegistered. I saw that my application registers a different register_id every time I run the application.

What is happening?

Thank You!

andreszs commented 9 years ago

Try to debug your server side to see if you are sending the POST request exactly as it should be sent, or maybe you are improperly reading/sending/storing the token, which would make it void.

Also, remember that every time you reinstall the app, the previous token will become obsolete, you must get a new token/regid every time the app is installed/reinstalled.

cyriljoui commented 9 years ago

Hi !

Where did you get this information? Could you please give us the link (Google Developer?). I think there is something missing on PushPlugin (Android but maybe also in IOS ...), every time we must call register() on plugin (even if application is already registered).

We may implement register execution like this

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {

// ... if (REGISTER.equals(action)) {

        Log.v(TAG, "execute: data=" + data.toString());

        try {
            JSONObject jo = data.getJSONObject(0);

            gWebView = this.webView;
            Log.v(TAG, "execute: jo=" + jo.toString());

            gECB = (String) jo.get("ecb");
            gSenderID = (String) jo.get("senderID");

            Log.v(TAG, "execute: ECB=" + gECB + " senderID=" + 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) {
            Log.e(TAG, "execute: Got JSON Exception " + e.getMessage());
            result = false;
            callbackContext.error(e.getMessage());
        }

        if ( gCachedExtras != null) {
            Log.v(TAG, "sending cached extras");
            sendExtras(gCachedExtras);
            gCachedExtras = null;
        }

    }

// ....

    return result;
}

// ... 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");
    }       
}
ghominejad commented 9 years ago

@cyriljoui thank you, Is it possible that gcm expires the registration id and GCMRegistrar.getRegistrationId returns us the old id?

In the most of times the problem occurs when you run the register function with no internet access then in the next runs (one or two) you'll get new registration id! I've solved the problem by creating new bindEvents function. i call this function when i haven't internet access otherwise i call the register.

cyriljoui commented 9 years ago

I've never got any issue with getRegistrationId() ... I will check with Google about expiration. Thank you for your feedback about no internet access, you're right ! Did you push back your fix to this repository?

ghominejad commented 9 years ago

@cyriljoui Yes, (#497), it checks network connectivity before calling GCMRegistrar.register