在过去的几天里,我一直在努力尝试让POC手机应用程序正常运行 我正在使用来自phonegap的PushPlugin 我的apk是通过build.phonegap.com服务构建的
以下代码连接到GCM并生成消息ID 但该设备实际上从未获得推送通知
我错过了什么?
Dim regID As String = "APA91bHQX1wnaRjU_Sq_vIiUOjpHxAr3N3Y0XKwWS3SpowFA6iC73eqNRAsyb_9Z_NorhDpVpBXkLzzq94YWE0tRr9vY8gdEtJvxfVi9sF3xH09UvTvT9Heu_lTzhzEa1IO0i74KijNBaVgYjqrEXZDab9sCTclYxA"
Dim request As WebRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.Headers.Add(HttpRequestHeader.Authorization, "key=AIzaSyCU80wS2pUy_6HGM6gJ4JTUdqGH9NhNa2M")
request.Headers.Add("Sender: id=924375227132")
Dim collapsKey = Guid.NewGuid.ToString("N")
Dim postdata As String = "" & _
"delay_while_idle=false" & _
"&time_to_live=108" & _
"&delay_while_idle=1" & _
"&collapse_key=" & collapsKey & "" & _
"&data.payload.msgcnt=3" & _
"&data.payload.message=Welcome to this app" & _
"®istration_id=" & regID
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata)
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim resposne As WebResponse = request.GetResponse
Dim dataresponse As Stream = resposne.GetResponseStream
Dim reader As New StreamReader(dataresponse)
Dim sResponseFromServer As String = reader.ReadToEnd
Console.WriteLine(sResponseFromServer)
reader.Close()
dataresponse.Close()
resposne.Close()
Phonegap HTML code
var pushNotification;
function RegisterDeviceABCD()
{
try
{
alert("Here");
pushNotification = window.plugins.pushNotification;
if ( device.platform == 'android' || device.platform == 'Android' )
{
pushNotification.register(
successHandler,
errorHandler, {
"senderID":"924375227132",
"ecb":"onNotificationGCM"
});
}
else
{
pushNotification.register(
tokenHandler,
errorHandler, {
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"onNotificationAPN"
});
}
}
catch(e)
{
alert(e);
}
}
答案 0 :(得分:0)
我将请求更改为JSON,似乎可以正常工作
Dim postdata As String = ""
postdata &= " { " & vbCrLf
postdata &= " ""collapse_key"": """ & collapsKey & """, " & vbCrLf
postdata &= " ""time_to_live"": 108, " & vbCrLf
postdata &= " ""delay_while_idle"": true, " & vbCrLf
postdata &= " ""data"": { " & vbCrLf
postdata &= " ""msgcnt"": ""3"", " & vbCrLf
postdata &= " ""message"": ""Welcome to this app"" " & vbCrLf
postdata &= " }, " & vbCrLf
postdata &= " ""registration_ids"":[" & vbCrLf & regID & vbCrLf & "] " & vbCrLf
postdata &= " } "
还将请求的内容类型更改为application / json