GCM CCS Receive delivery receipt

2019-06-06 15:34发布

I'm trying to write a 3rd party server .NET application for sending notifications to Android devices using GCM's CCS as outlined here. And I want to use Receive delivery receipts feature.
I created a simple console app using agsXMPP library. My application sends message to GCM with the flag "delivery_receipt_requested": true and receives ACK message from the GCM server, and I see this message is delivered to the target device, but delivery receipt message doesn't come.
Update:
I can receive upstream messages from device. And, finally, I can receive receipts, but it works from time to time. Is it really so unstable feature?
This is my test app:

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

0条回答
登录 后发表回答