Sumesh
Sumesh

Reputation: 11

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

I am new to IOS APN - Notification generating service , Please help on How we can send APN from C# Windows Service - .Net Framework 4.5 / 4.7?

Tired with Moon-APN , Pushsharp , DotAPN but no result. If any one have sample code with step by step process please share .

Thanks in Advance :-)

Upvotes: 1

Views: 3259

Answers (1)

M_JSL
M_JSL

Reputation: 75

@Sumesh you can use the .NET open source library PushSharp to push messages to Apple's APNS service.

step1: Download PushSharp open source project compilation 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: Then the ios client needs to provide the .p12 certificate file and the encryption password of the certificate file

step4: After preparing these, create a new console program to reference the above library file, copy the certificate to the root directory, change the attributes, and output to the copy directory as always copy

console program code as follow:

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

I hope I can help you

Upvotes: 2

Related Questions