user2184455
user2184455

Reputation: 31

Phonegap: "processMessage failed" unable to send javascript function (Cordova 2.5.0)

I am trying to make GCM push notifications work in my app and therefore I am using this project (https://github.com/marknutter/GCM-Cordova) as an example.

When I run the example code everything works fine, but when I transfer all the required files and edit my code to let it work in my own app it does not work anymore. I also tried the following plugin (https://github.com/phonegap-build/PushPlugin) and after installing it automatically it gives exactly the same error.

The app does not crash, but when I call the register function my callback function "onNotificationGCM" does not receive any messages back.

Register function:

window.plugins.GCM.register("my_gcm_id", "onNotificationGCM", successHandler, errorHandler );

After some debugging I figured out that the native android code is able to register the phone and does indeed get an ID message from the GCM server, but that it is unable to send this to my javascript.

Native android code:

public static void sendJavascript( JSONObject _json )
{
  String _d =  "javascript:"+gECB+"(" + _json.toString() + ")";
      Log.v(ME + ":sendJavascript", _d);

      if (gECB != null ) {
        gwebView.sendJavascript( _d );
      }
}

LogCat gives the following 'failed' message:

03-24 17:05:21.844: V/GCMPlugin:sendJavascript(31782): javascript:onNotificationGCM({"regid":"APA91bHX...31ASD","event":"registered"})

03-24 17:05:22.834: D/CordovaLog(31782): processMessage failed: Message: Jjavascript:onNotificationGCM({"regid":"APA91bHX...31ASD","event":"registered"})

The capital J in front of the message is strange and maybe that is what causes the problem, but it seems to be happening somewhere in the Cordova 2.5.0 code.

Does anyone have any idea how I can solve this?

Upvotes: 2

Views: 6562

Answers (4)

elessar
elessar

Reputation: 31

Try to add

window.onNotificationGCM = onNotificationGCM;

to change the context of your function

It solve the problem for me

Upvotes: 3

Hirad Nikoo
Hirad Nikoo

Reputation: 1629

I had the same problem. After some debugging as I understood I had an unwanted new line character "\n" at the end of my string that I wasn't responsible for and it seems that phone gap added the character. So what I did was to .replace("\n", "") the string on the Java part of the code.

String js = "javascript:displayTextMessage('" + date.toString() + " - " + msg.replace("\n", "") + "');" ; sendJavascript(js);

Upvotes: 1

obimod
obimod

Reputation: 797

Check your code for any incorrect JSON.parse's... these "illegal access" etc. error messages seem to be thrown when JSON.parse(not_a_json_string) happens.

Upvotes: 0

El Devoper
El Devoper

Reputation: 1093

This is not an Cordova 2.5.0 Bug! If you copy and paste from the "CORDOVA_GCM_script.js" example, you will have something like this:

case 'registered':
    // the definition of the e variable is json return defined in GCMReceiver.java
    // In my case on registered I have EVENT and REGID defined
    gApp.gcmregid = e.regid;
    if ( gApp.gcmregid.length > 0 )
    {
      $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");


      // ==============================================================================
      // ==============================================================================
      // ==============================================================================
      //
      // This is where you would code to send the REGID to your server for this device
      //
      // ==============================================================================
      // ==============================================================================
      // ==============================================================================

    }

    break

Make sure the "gApp" Array and the "gcmregedit" variable is declared in your script, or just don't use them in your eventhandler. Otherwise you'll get an "processMessage failed"-Message because of the "undefined"-error occuring before.

Upvotes: 0

Related Questions