简体   繁体   English

如何在一个请求中向多个设备(iOS)发送推送通知?

[英]How to send a push notification to more than one device (iOS) in one request?

I send notification to users one by one and it takes a while. 我会一一向用户发送通知,这需要一段时间。 This my code 这是我的代码

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

to send a notification to multiple devices i must send it to each device individually. 要将通知发送到多个设备,我必须将其分别发送到每个设备。 Can I send notifications to list of device? 我可以发送通知到设备列表吗?

Since you are using .NET for sending notifications, you can try to use Azure Notification Hub. 由于您使用.NET发送通知,因此可以尝试使用Azure Notification Hub。 There is an option to send template notifications, which allows you to send cross-platform notifications. 有一个发送模板通知的选项,它使您可以发送跨平台的通知。

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

Also, you can specify tag expressions, which allow you to send notifications to specific group of devices that are registered with targeted tags. 另外,您可以指定标签表达式,该表达式允许您将通知发送到使用目标标签注册的特定设备组。 I believe this could help you with sending notifications to multiple devices with one request. 我相信这可以帮助您通过一个请求将通知发送到多个设备。 Combining this and using templates, you could even send notifications to multiple Android, Windows Phone and iOS devices with single request. 结合使用此模板和模板,您甚至可以通过一个请求将通知发送到多个Android,Windows Phone和iOS设备。

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

You can find more about Azure Notification Hub here: 您可以在此处找到有关Azure通知中心的更多信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM