简体   繁体   中英

GCM Cordova Push Notification not working when App not running

The application is in PhoneGap. I am using push notification feature with https://github.com/marknutter/GCM-Cordova

I implemented the whole feature and it worked fine until yesterday. When I checked the app last day, push notification is not working when the app is not running. All notifications are receiving on my device, clicking on the message opens the app.

But the controls are not receiving at the javascript portion where message notification handles.

Code from file GCMIntentService.java:

JSONObject json;
json = new JSONObject().put("event", "message");

json.put("message", extras.getString("message"));
json.put("link", extras.getString("link"));

Log.v(ME + ":onMessage ", json.toString());

GCMPlugin.sendJavascript( json );
// Send the MESSAGE to the Javascript application

This code will work when the message received. But at that time, App is not running.

When I click on the notification message, the app opens as usual, not registering the message event; thereby not getting the control in JavaScript to handle push notification.

Code for CORDOVA_GCM_script.js is available at:

https://github.com/marknutter/GCM-Cordova/blob/master/assets/www/CORDOVA_GCM_script.js

Note : It works fine when the app is running.

EDIT: The notification runs sometimes. JavaScript is getting message event randomly.

EDIT 2:

I definitely know the problem.

From the java file, it triggers the javascript code when opening the app by clicking on the push notification message. But at that time, the WebView is not loaded, so not able to execute the script (which is an event trigger). Is this is the condition, everyone using the plugin will experience the same. Is there any workaround or any fix for it?

Am I doing anything wrong?

Using GCM-Cordova plugin will not handle the notification correctly if the application is not running. It is because the WebView was not loaded at the time when the plugin initiate the javascript.

Solution:

  • Modified the plugin code to save message data as temporary when the notice arrives.
  • Created another plugin with methods to read this loaded data and clear data.
  • Call my custom plugin method on deviceready and checked if there any data.
  • If there any, clear the locally stored data and invoke the method to show notice message.

Finally able to solve this issue with the help of two plugins - GCM-Cordova and custom plugin.

IvenMS - I too am having the same problem so you are not the only one. If I understand praneetloke correctly, the Push Notifications will not work with PhoneGap apps which are not currently running.

i've found a simple solution. In the GCMIntentService, just before send javascript check if the webview is loaded :

while (GCMPlugin.gwebView == null || GCMPlugin.gwebView.webView == null || !GCMPlugin.gwebView.webView.isEnabled()) {
    // Wait until webView is enabled
}
GCMPlugin.sendJavascript(json);

This works for me ... maybe is a good choise to add also a timeout in order to prevent an infinite loop ( it shouldn't happen but ... )

Davide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM