我目前正在努力通过Parse为使用GCM的Android应用程序实现推送通知。 这是我拥有的和有效的: 我遵循GitHub项目的自述步骤Push Notifications 我修改了registrationHandler来注册deviceToken:
var req = {
method: ‘POST’,
url: ‘api.parse.com/1/installations’,
headers: {
‘X-Parse-Application-Id’: ‘xxx’,
‘X-Parse-REST-API-Key’: ‘xxx’,
‘Content-Type’: ‘application/json’
},
data: {
‘deviceType’: ‘android’,
‘pushType’: ‘gcm’,
‘deviceToken’: deviceToken,
‘GCMSenderId’: senderID
}
}
$http(req).success(function(){
alert('Successfully registered');
}).error(function(){
alert('Error with Registration');
});
我在Build Configuration中启用了Push Notifications,并且能够构建和安装apk。 在Parse核心模块中,我可以看到已注册的设备。我已将Google Project ID和API Key添加到Parse。 如果我从安装表中复制deviceToken并将其与此Ruby脚本一起使用,我会立即收到通知。
require 'rubygems'
require 'pushmeup'
APPLICATION_API_KEY = "API_KEY_GOES_HERE"
DEVICE_REGISTRATION_ID = "REGISTRATION_ID_GOES_HERE"
GCM.host = 'https://android.googleapis.com/gcm/send'
GCM.format = :json
GCM.key = APPLICATION_API_KEY
destination = [DEVICE_REGISTRATION_ID]
data = {:message => "PhoneGap Build rocks!", :msgcnt => "1", :soundname => "beep.wav"}
puts GCM.send_notification( destination, data)
但如果我从Parse发布消息,我就不会收到通知。 我错过了什么?
编辑1: 同时我查看了LogCat,发现这些消息实际上是传递给我的设备的,但是处理消息时出现了错误:
D/JsMessageQueue﹕ Enqueued JsMessage: {"from":"123456789","collapse_key":"do_not_collapse","payload":{"data":{"alert":"Testing for UUID","push_hash":"c1a7d096f1157109a590dbade5670dcd"}},"foreground":true,"coldstart":false,"timestamp":"2015-05-19T07:57Z","uuid":"f00dfc28-ef88-411c-b12d-f3eb5dba230b"}, paused: false
05-19 09:57:48.038 24772-24772/ch.hevs.asd D/JsMessageQueue﹕ Flushed 1 messages to JS side.
05-19 09:57:48.038 24772-28185/ch.hevs.asd V/GCMBaseIntentService﹕ Releasing wakelock
05-19 09:57:48.058 24772-24772/ch.hevs.asd I/chromium﹕ [INFO:CONSOLE(1044)] "processMessage failed: Error: SyntaxError: Unexpected token u", source: http://localhost/cordova.js (1044)
05-19 09:57:48.078 24772-24772/ch.hevs.asd I/chromium﹕ [INFO:CONSOLE(1045)] "processMessage failed: Stack: SyntaxError: Unexpected token u
at Object.parse (native)
编辑2: 好的,我调试了传入的推送通知,我从两种方法收到的JSON都不同。所以我认为Cordova的PushPlugin无法处理来自Parse.com的JSON
来自Ruby的消息:
S11 PushPlugin1887095744
{"soundname":"beep.wav",
"msgcnt":"1",
"from":"12345678",
"message":"PhoneGap rocks!",
"collapse_key":"do_not_collapse",
"payload":{},
"foreground":false,
"coldstart":false,
"timestamp":"2015-05-19T08:29Z",
"uuid":"af6605e0-1efb-4c8a-82de-d8858b2ec725"}
来自Parse的消息
S11 PushPlugin1887095744
{"from":"12345678",
"collapse_key":"do_not_collapse",
"payload":{
"data":{"alert":"lets debug this PoS",
"push_hash":"1edebeb8864bc06682c10f1be1058164"}},
"foreground":false,
"coldstart":false,
"timestamp":"2015-05-19T08:25Z",
"uuid":"df575996-c039-4f5b-8120-147fb47911c9"}