GCM Cordova Push notification not working when the app is down

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

I implemented the whole function and worked until yesterday. When I checked the application on the last day, the push notification does not work when the application does not work. All notifications are received on my device, clicking on the message opens the application.

But the controls do not get in the javascript part where the message notification is processed.

Code from the GCMIntentService.java file:

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 a message is received. But at that time, the application is not working.

When I click on a notification, the application opens, as usual, without registering a message event; thereby not gaining control of JavaScript to handle push notifications.

The 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 application is running.

EDIT: Notification is sometimes done. JavaScript receives a random message event.

EDIT 2:

I definitely know the problem.

From the java file, it runs javascript code when opening the application by clicking on the push notification message. But at that time, WebView is not loading, so the script (which is the event trigger) cannot be executed. This condition, everyone who uses the plugin will experience the same thing. Is there any workaround or some fix for it?

Am I doing something wrong?

+7
source share
3 answers

I found a simple solution. In the GCMIntentService, before submitting 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 it's a good choice to add a timeout too, to prevent an infinite loop (this should not happen, but ...)

David

+3
source

Using the GCM-Cordova plugin will not correctly process the notification if the application does not work. This is because the WebView was not loading while the plugin initiated javascript.

Decision:

  • Changed the plugin code to save message data as temporary when a notification arrives.
  • Created another plugin with methods for reading this downloaded data and clearing the data.
  • Call my custom plugin method on deviceready and check if there is any data.
  • If there is, clear the locally stored data and call the method to display a notification.

Finally, you can solve this problem with two plugins - GCM-Cordova and a custom plugin.

+4
source

IvenMS - I have the same problem too, so you're not the only one. If I understood praneetloke correctly, Push Notifications will not work with PhoneGap applications that currently do not work.

+3
source

All Articles