【问题标题】:How to send a push notification to more than one device (iOS) in one request?如何在一个请求中向多个设备(iOS)发送推送通知?
【发布时间】:2017-01-11 09:05:33
【问题描述】:

我一个一个地向用户发送通知,这需要一段时间。 这是我的代码

var deviceios = db.Devices;

        int port = 2195;
        String hostname = "gateway.sandbox.push.apple.com";


        string certificatePath = System.Web.HttpContext.Current.Server.MapPath("mypath/cert.p12);

        string certificatePassword = "password";



        foreach (var item in deviceios)
        {


            X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.MachineKeySet);
            X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

            TcpClient client = new TcpClient(hostname, port);
            SslStream sslStream = new SslStream(
                            client.GetStream(),
                            false,
                            new RemoteCertificateValidationCallback(ValidateServerCertificate),
                            null
            );

            try
            {
                sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Default, false);
            }
            catch (AuthenticationException ex)
            {
                client.Close();
                return;
            }

            //// Encode a test message into a byte array.
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memoryStream);

            writer.Write((byte)0);  //The command
            writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
            writer.Write((byte)32); //The deviceId length (big-endian second byte)
            string devicetocken = item.device1;//  iphone device token
            byte[] b0 = HexString2Bytes(devicetocken);
            WriteMultiLineByteArray(b0);

            writer.Write(b0);
            String payload;
            string strmsgbody = "";
            int totunreadmsg = 1;
            strmsgbody = message;
            payload = "{\"aps\":{\"alert\":\"" + strmsgbody + "\",\"badge\":" + totunreadmsg.ToString() + ",\"sound\":\"mailsent.wav\"},\"acme1\":\"bar\",\"acme2\":42}";
            writer.Write((byte)0); //First byte of payload length; (big-endian first byte)  
            byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
            writer.Write((byte)b1.Length);    //payload length (big-endian second byte)  
            writer.Write(b1);
            writer.Flush();
            byte[] array = memoryStream.ToArray();
            try
            {
                sslStream.Write(array);
                sslStream.Flush();
            }
            catch
            {



            }
            client.Close();

要将通知发送到多个设备,我必须将其单独发送到每个设备。我可以向设备列表发送通知吗?

【问题讨论】:

  • 尝试使用您自己的服务器。下一次更新,你放入一个监听网络服务器的函数,当它触发一些事件时,它会声明一个推送事件。
  • 您是否考虑过使用 ASP.Net SignalR?它就是为此目的而设计的。
  • 使用自己的服务器不会改变 APN 的工作方式。同样使用 SignalR 与推送通知完全不同。至于问题,您不是为每个通知打开和关闭连接吗?那将需要很长时间。通知必须一一发送,但它们可以在单个连接内发送。您只需要处理错误消息。

标签: c# asp.net-mvc push-notification apple-push-notifications


【解决方案1】:

由于您使用 .NET 发送通知,您可以尝试使用 Azure 通知中心。有一个发送模板通知的选项,允许您发送跨平台通知。

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-aspnet-cross-platform-notification

此外,您还可以指定标签表达式,以便向注册了目标标签的特定设备组发送通知。我相信这可以帮助您通过一个请求向多个设备发送通知。结合这一点并使用模板,您甚至可以通过单个请求向多个 Android、Windows Phone 和 iOS 设备发送通知。

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-tags-segment-push-message

您可以在此处找到有关 Azure 通知中心的更多信息:

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification-overview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    相关资源
    最近更新 更多