适用于Android的Cordova / Phonegap GCM插件崩溃

时间:2013-03-05 19:44:44

标签: android cordova phonegap-plugins google-cloud-messaging

我想让GCM Cordova Plugin在提供的示例应用程序中运行。我下载了源代码,在Eclipse中我从现有代码创建了一个项目。

现在,在这个项目中有一个名为CORDOVA_GCM_script.js的文件,需要更改发件人ID以匹配我自己的GCM服务标识符(我从我的Google项目中获取):

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

要将消息发送到我的应用程序,我将node.js与此脚本一起使用,正如Holly Schinsky在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);
    }
});

现在,当我在设备上运行应用程序时,它会启动并注册,但当我尝试向其发送消息时,它会崩溃并存在消息“不幸的是,GCM已停止”

LogCat告诉我这个:

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)

我找到this post并遵循建议的建议,但应用程序一直在崩溃。

有人可以就此提出任何建议吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

如StackTrace中所示,当您将null邮件传递给Log.v时会发生此错误。

可能是messageId为空console.log("Sent with message ID: ", messageId);

尝试将其更改为

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

这只是一个用于调试目的的快速解决方案。

您可以检查的另一件事是评论Cordova code代码

中的第75行

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

答案 1 :(得分:0)

感谢iTech提供的提示,我注意到问题实际上是服务器;特别是,服务器将消息发送到设备的方式。

现在我使用node-gcm和此脚本发送消息:

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

使用我之前的脚本(您可以在问题上看到它),不知何故数据未包含在消息中,因此当尝试在日志上打印包含数据的值时应用程序崩溃。使用此脚本,不会发生这种情况。