问题
I'm trying to edit the phonegap/cordova PushPlugin plugin for android in order to not create an status-bar Notification when app is not active but always doing some javascript work.
I want to receive my GCM message an do some work on it with javascript at anytime.
GCM message received by PushPlugin => javascript function called.
I've no problem to make this working when my cordova app is active (ie in foreground or paused).
But I can't use sendJavascript(...)
when the app has been destroyed. It makes the app crash.
Is there a way to keep the callback context or to wake the webview when the app is not active anymore ?
PS : I'm completely lost within all my google research that give me old results and all different versions of phonegap.
UPDATE :
I've actually that code :
public class PushPlugin extends CordovaPlugin {
private static CallbackContext callbackCont;
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
callbackCont = callbackContext;
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, "Start");
progressResult.setKeepCallback(true);
callbackCont.sendPluginResult(progressResult);
}
//Fired by an Receiver
public static void action(JSONObject _json) {
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, "Message");
progressResult.setKeepCallback(true);
callbackCont.sendPluginResult(progressResult);
}
}
As expected the javascript success function is fired only when app is active (foreground or paused)
回答1:
Since the app is at first a chrome packaged app, build for mobile with cca, I use now the chrome app GCM plugin that is just supported by cca and that wake up the app when an new message is coming.
See https://github.com/MobileChromeApps/mobile-chrome-apps/tree/master/chrome-cordova/plugins/chrome.gcm
来源:https://stackoverflow.com/questions/23349847/phonegap-plugin-result-when-app-is-not-active-anymore