Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于Android崩溃的Cordova/Phonegap GCM插件_Android_Cordova_Phonegap Plugins_Google Cloud Messaging - Fatal编程技术网

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

用于Android崩溃的Cordova/Phonegap GCM插件,android,cordova,phonegap-plugins,google-cloud-messaging,Android,Cordova,Phonegap Plugins,Google Cloud Messaging,我正在尝试在提供的示例应用程序中运行。我下载了源代码,并在Eclipse中根据现有代码创建了一个项目 现在,在这个项目中有一个名为CORDOVA_GCM_script.js的文件,其中需要更改发件人Id以匹配我自己的GCM服务标识符(我从我的Google项目中获得): 为了将消息发送到我的应用程序,我将node.js与此脚本一起使用,正如Holly Schinsky在上所解释的: 现在,当我在设备上运行应用程序时,它会启动并注册,但当我尝试向它发送消息时,它会崩溃,并与消息“不幸的是,GCM已停

我正在尝试在提供的示例应用程序中运行。我下载了源代码,并在Eclipse中根据现有代码创建了一个项目

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

为了将消息发送到我的应用程序,我将node.js与此脚本一起使用,正如Holly Schinsky在上所解释的:

现在,当我在设备上运行应用程序时,它会启动并注册,但当我尝试向它发送消息时,它会崩溃,并与消息“不幸的是,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)
我找到并遵循了建议,但应用程序不断崩溃

有人能给我一些建议吗


谢谢。

如StackTrace中所示,将
null
消息传递到
Log.v
时会发生此错误

可能是
messageId
为空
console.log(“随消息ID一起发送:”,messageId)

试着把它改成

 console.log("Sent with message ID: ", messageId + "");
这只是一个用于调试目的的快速解决方案

您可以检查的另一件事是在代码中注释第75行


Log.v(ME+“:onMessage”,json.toString())

多亏了iTech提供的提示,我注意到问题其实出在服务器上;特别是服务器向设备发送消息的方式

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

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

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

正如您所建议的,我对这两行都进行了注释。应用程序不再崩溃,在WebView上我可以看到行“MESSAGE->MSG:undefined”和“MESSAGE->MSGCOUNT:undefined”,这表明可能我没有以正确的方式从服务器发送消息。
 console.log("Sent with message ID: ", messageId + "");
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);
});