Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 394
IvenMS Avatar asked Oct 20 '12 08:10

IvenMS


2 Answers

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.

like image 185
IvenMS Avatar answered Sep 21 '22 02:09

IvenMS


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.

like image 35
user1506863 Avatar answered Sep 19 '22 02:09

user1506863