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

会有一股神秘感。 提交于 2020-01-14 02:20:11

问题


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?


回答1:


Try to add

window.onNotificationGCM = onNotificationGCM;

to change the context of your function

It solve the problem for me




回答2:


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);




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/15603395/phonegap-processmessage-failed-unable-to-send-javascript-function-cordova-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!