Question Apple Wallet - Send empty push notification not working

sdurkin

New member
Joined
Sep 15, 2022
Messages
1
Location
Dublin, Ireland
Programming Experience
5-10
Hi all,

We just recently added Apple Pass to our website and we are trying to send push notifications to expire passes when needed, however, when we pass the device ID nothing happens and we get no error.

The Device Token below does not exist, we did random characters for the purpose of this example.
Send Empty Push Notification Code:
public void SendEmptyPushNotification(string pushToken)
        {
            string deviceToken = "df997e197d935d76241c5f3c3ed0480245cbe11fa6d5e8245f";
            var bytes = Encoding.ASCII.GetBytes(deviceToken);
            var hexArray = bytes.Select(b => b.ToString("x2")).ToArray();
            var devicetoken1 = string.Join(string.Empty, hexArray);
            var t = Encoding.ASCII.GetBytes(devicetoken1);


            string certpw = ConfigurationManager.AppSettings["AppleCertificatePassword"].ToString();
            byte[] certificateData = File.ReadAllBytes(ConfigurationManager.AppSettings["ApplePassCertLocation"]);
            X509Certificate2 certificate = new X509Certificate2(certificateData, certpw, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

            string server = "gateway.push.apple.com"; // Production
            using (TcpClient tcpClient = new TcpClient(server, 2195))
            {
                using (SslStream sslStream = new SslStream(tcpClient.GetStream()))
                {
                    try
                    {
                        X509Certificate2Collection certs = new X509Certificate2Collection();
                        certs.Add(certificate);
                        sslStream.AuthenticateAsClient(server, certs, SslProtocols.Tls12, false);
                    }
                    catch (AuthenticationException exp)
                    {
                        throw new AuthenticationException(exp.Message);
                    }
                    catch (IOException exp)
                    {
                        throw new IOException(exp.Message);
                    }
                    try
                    {
                        MemoryStream memoryStream = new MemoryStream();
                        BinaryWriter writer = new BinaryWriter(memoryStream);
                        writer.Write((byte)0);
                        writer.Write((byte)32);
                        writer.Write(t);
                        string payload = "{\"aps\":\"\"}";
                        writer.Write((byte)0);
                        writer.Write((byte)payload.Length);
                        byte[] b1 = Encoding.UTF8.GetBytes(payload);
                        writer.Write(b1);
                        writer.Flush();
                        byte[] array = memoryStream.ToArray();
                        sslStream.Write(array);
                        sslStream.Flush();
                    }
                    catch (ArgumentNullException anu)
                    {
                        success = anu.InnerException.Message;
                    }
                    catch (InvalidOperationException ioe)
                    {
                        success = ioe.InnerException.Message;
                    }
                    catch (HttpRequestException hre)
                    {
                        success = hre.InnerException.Message;
                    }
                    catch (Exception e)
                    {
                        success = e.InnerException.Message;
                    }

                }
            }
        }
 
Looks to me for are trying to use a binary protocol:
  • APNs won’t support legacy binary protocol as of March 31, 2021. We recommend updating to the HTTP/2-based API from this page as soon as possible.

From
 
Back
Top Bottom