2013-03-05 19 views
0

我试图让GCM Cordova Plugin在提供的示例应用程序中运行。我下载了源代码,并在Eclipse中创建了一个来自现有代码的项目。Cordova/Phonegap用于Android崩溃的GCM插件

现在,在这个项目中有一个名为CORDOVA_GCM_script.js文件,其中有必要更改发件人ID来与我自己的GCM服务标识符(这是我从我的谷歌项目获得):

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

要将信息发送给我的申请,我用Node.js的这个剧本,就如同上this post解释冬青Schinsky提供:

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跟着提出劝告,但应用程序总是崩溃。

任何人都可以给我任何建议吗?

谢谢。

回答

0

多亏了科技工程提供尖端,我注意到这个问题实际上是与服务器;特别是服务器向设备发送消息的方式。

现在我用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); 
}); 

与我以前的脚本(你可以看到它的问题),不知何故该数据未包含在该消息,因此应用试图在日志上打印包含数据的值时崩溃。通过这个脚本,这不会发生。

1

如您的StackTrace所示,当您将null消息传递给Log.v时会发生此错误。

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

尝试将其更改为

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

这仅仅是为了调试的需要一个快速解决方案。

,您可以检查的另一件事是注释行75 Cordova code代码

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

+0

正如你所建议的,我评论了两条线。 。 。应用程序不再崩溃,并且在WebView上我可以看到“MESSAGE - > MSG:undefined”和“MESSAGE - > MSGCOUNT:undefined”这两行,这表明我可能没有以正确的方式从服务器发送消息。 – Octavio 2013-03-05 20:46:38