Octavio
Octavio

Reputation: 456

Cordova / Phonegap GCM Plugin for Android crashing

I am trying to get the GCM Cordova Plugin to run in the sample application provided. I downloaded the source and in Eclipse I created a project from existing code.

Now, in this project there is a file called CORDOVA_GCM_script.js, where it is necessary to change the sender Id to match my own GCM Service identifier (which I get from my Google Project):

window.plugins.GCM.register("my_sender_id", "GCM_Event", GCM_Success, GCM_Fail );

To send the message to my application, I use node.js with this script, just as explained by Holly Schinsky on this post:

var GCM = require('gcm').GCM;

var apiKey = 'someCharsRepresentingMyKey';
var gcm = new GCM(apiKey);

var message = {
    registration_id: 'myDeviceRegistrationId', // required
    collapse_key: 'demo', 
    'message': 'Yourturn',
    'title': 'My Game',
    'msgcnt': '1'
};

gcm.send(message, function(err, messageId){
    if (err) {
        console.log("Something has gone wrong!");
    } else {
        console.log("Sent with message ID: ", messageId);
    }
});

Now, when I run the application on the device, it starts and gets registered but when I try to send a message to it, it crashes and exists with the message "Unfortunately, GCM has stopped"

LogCat shows me this:

03-05 20:15:39.897: E/AndroidRuntime(19007): FATAL EXCEPTION: IntentService[GCMIntentService-GCMIntentService-2]
03-05 20:15:39.897: E/AndroidRuntime(19007): java.lang.NullPointerException: println needs a message
03-05 20:15:39.897: E/AndroidRuntime(19007):    at android.util.Log.println_native(Native Method)
03-05 20:15:39.897: E/AndroidRuntime(19007):    at android.util.Log.v(Log.java:117)
03-05 20:15:39.897: E/AndroidRuntime(19007):    at com.cordova2.gcm.GCMIntentService.onMessage(GCMIntentService.java:63)
03-05 20:15:39.897: E/AndroidRuntime(19007):    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:179)
03-05 20:15:39.897: E/AndroidRuntime(19007):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
03-05 20:15:39.897: E/AndroidRuntime(19007):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 20:15:39.897: E/AndroidRuntime(19007):    at android.os.Looper.loop(Looper.java:137)
03-05 20:15:39.897: E/AndroidRuntime(19007):    at android.os.HandlerThread.run(HandlerThread.java:60)

I found this post and followed the proposed advise, but the app keeps crashing.

Can anyone give me any advise on this?

Thank you.

Upvotes: 0

Views: 2462

Answers (2)

Octavio
Octavio

Reputation: 456

Thanks to the tip provided by iTech, I noticed that the problem was actually with the server; in particular, with the way the server was sending the message to the device.

Now I use node-gcm and this script to send the message:

var gcm = require('/usr/local/lib/node_modules/node-gcm');
var message = new gcm.Message();
var sender = new gcm.Sender('charsRepresentingAPIKey');
var registrationIds = [];

message.addData('message', 'hello');
message.addData('msgcnt', '1');
message.collapseKey = 'demo';
message.delayWhileIdle = true;
message.timeToLive = 3;

registrationIds.push('charsRepresentingRegIDOfDevice');

sender.send(message, registrationIds, 4, function (err, result) {
        console.log(result);
});

With my previous script (you can see it on the question), somehow the data was not included in the message and therefore the application crashed when trying to print on the log the values for the included data. With this script, this does not happen.

Upvotes: 0

iTech
iTech

Reputation: 18440

As shown in your StackTrace, this error occurs when you pass a null message to Log.v.

It might be the messageId is null console.log("Sent with message ID: ", messageId);

Try to change it to

 console.log("Sent with message ID: ", messageId + "");

It is just a quick solution for debugging purpose.

The other thing you can check is to comment line 75 in Cordova code code

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

Upvotes: 1

Related Questions