-
Notifications
You must be signed in to change notification settings - Fork 992
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
need way to set ECB callback without also registering device #314
Comments
What I ended up doing is making it so the actual GCM register function is only called on application start without actually changing any JavaScript code. In the PushPlugin.java class, I changed the execute function like so: public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
boolean result = false;
Log.v(TAG, "execute: action=" + action);
if (REGISTER.equals(action)) {
Log.v(TAG, "execute: data=" + data.toString());
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(!prefs.getBoolean("registered", false)) {
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);
GCMRegistrar.register(getApplicationContext(), gSenderID);
callbackContext.success();
// run your one time code
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("registered", true);
editor.commit();
}
result = true;
} 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;
}
} else if (UNREGISTER.equals(action)) {
GCMRegistrar.unregister(getApplicationContext());
Log.v(TAG, "UNREGISTER");
result = true;
callbackContext.success();
} else {
result = false;
Log.e(TAG, "Invalid action : " + action);
callbackContext.error("Invalid action : " + action);
}
return result;
} Basically I added an if statement that adds a shared preference to ensure the GCMRegistrar.register() function is only called once. In my main application activity (under the package com.example.appname for example), I added an onDestroy function which will remove the shared preference when the application is closed. @Override
public void onDestroy()
{
super.onDestroy();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(prefs.getBoolean("registered", false))
{
SharedPreferences.Editor editor = prefs.edit();
editor.remove("registered").commit();
}
} Therefore, the register function will be called only when the app is first opened. Let me know if you need more info. |
Ok, and I added a different method that I can call from JS that only sets the callback (so that I can also easily check for different app versions and register when I want to with the logic in the JS, which is the main development language of the app). (I also don't want to register every time the app is open, which is still too often.) But I would rather not have to maintain a fork, you know? |
Aren't you planning a pull request to the project, campusgrids? It is an interesting patch... |
with respect, that change is still wrong. There is no reason to re-register every time the app starts. Google docs are pretty clear about this. The same is true on iOS---re-registration is simply not necessary. |
Then when would a re-registration be necessary? |
What's necessary is telling the plugin what callback to use. |
Unless that's stored somewhere (which doesn't seem to be the case, looking through the source, though). |
If you implement it, would you be able to post your code? |
I just have an extra case for the android version: public static final String SETECB = "setecb";
...
} else if (SETECB.equals(action)) {
Log.v(TAG, "setecb: data=" + data.toString());
try {
JSONObject jo = data.getJSONObject(0);
gECB = (String) jo.get("ecb");
gSenderID = (String) jo.get("senderID");
result = true;
callbackContext.success();
} catch (JSONException e) {
Log.e(TAG, "setectb: Got JSON Exception " + e.getMessage());
result = false;
callbackContext.error(e.getMessage());
}
} else if (UNREGISTER.equals(action)) {
... Then I call this from the js. Ideally similar/equivalent functionality would be present for other platforms as well; the organization of the iOS code is different, though, and less transparent to me. |
@bwo I've been bothered about the same issue for a couple of days. When the app is installed, in forground, and backgorund modes the plugin works fine, but I will not be able to process any further notification I recieve after the app is exited. |
Indeed, some documentation about how this plugin should be used would be nice. According to Android's docs registration should only ever occur once and then when the application is updated, whereas this plugin apparently requires that registration is done every time that the app starts. |
Guys, I'm new to submitting pull requests but have experience with Android, how can I help to get this issue fixed? |
Let me complement @bwo's method...
Add this line at his function |
If this helps anyone: specifically the commits: |
Guys I was also struggling like you all, and somehow @kentmw 's suggestion helped me to get this working. I have documented the changes here http://stackoverflow.com/questions/32166357/customizing-pushplugin-for-cordova-for-android/32166358#32166358 |
So will this fix by @kishorpawar be pulled into this repo ?? |
@anilbhanushali I haven't actually fixed this issue, the fixes are by @kentmw, I just figured out what more needs to be done besides @kentmw changes and documented on @stackoverflow, so that nobody needs to struggle more. |
It works great on Android! Thank you @kentmw! |
as far as I can tell the only way to register the ECB callback is by calling
register
, which means that if the app is closed and starts up again, I'll have to callregister
again, even if I've already registered. However, it seems pretty clear from google's documentation that, at least for android, this behavior is undesirable:It also means additional roundtrips both to Google and to the backend server to store the new id.
The text was updated successfully, but these errors were encountered: