我正在尝试编写第三方服务器.NET应用程序,以便按照概述here使用GCM的CCS向Android设备发送通知。
我想使用Receive delivery receipts功能。
我使用agsXMPP库创建了一个简单的控制台应用程序。我的应用程序使用标志" delivery_receipt_requested":true 向GCM发送消息,并从GCM服务器接收ACK消息,我看到此消息已发送到目标设备,但是发送收据消息没有来。
更新:我可以从设备接收上游消息。最后,我可以收到收据,但它不时有效。真的那么不稳定吗?
这是我的测试应用程序:
using System.Threading; using agsXMPP; using agsXMPP.protocol.client;
class Program
{
private static XmppClientConnection xmpp;
static void Main(string[] args)
{
xmpp = new XmppClientConnection
{
UseSSL = true,
UseStartTLS = false,
Server = "gcm.googleapis.com",
ConnectServer = "gcm.googleapis.com",
Port = 5235,
Password = "my pwd",
Username = "my senderId"
};
xmpp.OnAuthError += OnAuthError;
xmpp.OnLogin += xmpp_OnLogin;
xmpp.OnError += OnError;
xmpp.OnMessage += new MessageHandler(xmpp_OnMessage); //I expected delivery receipt here
xmpp.Open();
Console.WriteLine("Press any key to stop ...");
Console.ReadLine();
}
static void xmpp_OnLogin(object sender)
{
Console.WriteLine("LoggedOn");
string msg = string.Format
("<message id=\"\">" +
"<gcm xmlns=\"google:mobile:data\">" +
"{{" +
"\"to\":\"{1}\"," +
"\"message_id\":\"m-{0}\"" +
"\"data\":{{\"somedata\":\"\"}}" +
"\"time_to_live\":600," +
"\"delay_while_idle\": true," +
"\"delivery_receipt_requested\": true}}" +
"</gcm>" +
"</message>", Guid.NewGuid().ToString(),
"device_registartion_id");
xmpp.Send(msg);
}
static void xmpp_OnMessage(object sender, Message msg)
{
//ACK or NACK messages handle here and I expect Delivery receipt here too
Console.WriteLine("xmpp_OnMessage: " + msg.InnerXml);
if (msg.InnerXml.Contains("myapp.name")) //delivery receipt or upstream message
{
//send an ACK back with message ID
string msgAck = string.Format
("<message id=\"\">" +
"<gcm xmlns=\"google:mobile:data\">" +
"{{" +
"\"to\":\"{1}\"," +
"\"message_id\":\"{0}\"" +
"\"message_type\": \"ack\"}}" +
"</gcm>" +
"</message>", msg.Id, "device_registartion_id");
xmpp.Send(msgAck);
}
return;
}
static void OnAuthError(object sender, agsXMPP.Xml.Dom.Element e)
{
Console.WriteLine("OnAuthError: " + e.InnerXml);
}
static void OnError(object sender, Exception ex)
{
Console.WriteLine("OnError: " + ex.Message);
}
}