繁体   English   中英

如何发送 iOS - 带有 P12 证书的 APN 通知 - C# 样本

[英]How to send iOS - APN Notification With P12 Certificate - C# Samples

我是 IOS APN 的新手 - 通知生成服务,请帮助我们如何从 C# Windows Service -.Net Framework 4.5 / 4.7 发送 APN?

厌倦了 Moon-APN、Pushsharp、DotAPN 但没有结果。 如果有人有逐步过程的示例代码,请分享。

提前致谢:-)

@Sumesh 您可以使用.NET开源库PushSharp将消息推送到 Apple 的 APNS 服务。

step1:下载PushSharp开源项目编译https://github.com/Redth/PushSharp

step2: After successful compilation, APNS push needs to use Newtonsoft.Json.dll , PushSharp.Apple.dll , PushSharp.Core.dll three assembly library files

step3:那么ios客户端需要提供.p12证书文件和证书文件的加密密码

step4:准备好这些后,新建一个console程序,引用上面的库文件,将证书复制到根目录,更改属性,output到复制目录照常复制

控制台程序代码如下:

 class Program
    {
        static ApnsConfiguration config;
        static ApnsServiceBroker apnsBroker;
        static void Main(string[] args)
        {
            config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, "certificate.p12", "certificate's password");
            apnsBroker = new ApnsServiceBroker(config);
            //post catch error 
            apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
            {
                aggregateEx.Handle(ex =>
                {
                 
                    if (ex is ApnsNotificationException)
                    {
                        var notificationException = (ApnsNotificationException)ex;
                        //handle failed APN msg 
                        var apnsNotification = notificationException.Notification;
                        var statusCode = notificationException.ErrorStatusCode;
                        Console.WriteLine("Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}" + notification.DeviceToken);
                    }
                    else
                    {
                        //internal catch error 
                        Console.WriteLine("Apple Notification Failed for some unknown reason : {ex.InnerException}" + notification.DeviceToken);
                    }
                    // flag handle
                    return true;
                });
            };
            //successed
            apnsBroker.OnNotificationSucceeded += (notification) =>
            {
                Console.WriteLine("Apple Notification Sent ! "+notification.DeviceToken);
            };
            //engined start
            apnsBroker.Start();
        }
 
        /// <summary>
        /// apn message
        /// </summary>
        public static void SendMsg()
        {
            List<string> MY_DEVICE_TOKENS = new List<string>() {
                "1f6f37acad29348c6a5957529c9fa61ad69766ec9c7367948745899cbccdfd51",
                "1f6f37acad29348c6a5957529c9fa61ad69766ec9c7367948745899cbccdfd51" 
            };
 
            foreach (var deviceToken in MY_DEVICE_TOKENS)
            {
                // queue triger a notification message to iOS client
                apnsBroker.QueueNotification(new ApnsNotification
                {
                    DeviceToken = deviceToken, // this one deviceToken from iOS client,it have to
                    Payload = JObject.Parse("{\"aps\":{\"sound\":\"default\",\"badge\":\"1\",\"alert\":\"This is a test of a mass advertising message push message\"}}")
                });
            }
 
            //engined stop
            apnsBroker.Stop();
            Console.Read();
        }
    }

我希望我能帮助你

暂无
暂无

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

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