Search code examples
c#push-notificationcertificateapple-push-notificationspushsharp

Send Push Notification in C#


I am trying to send push notification to iOS device with below coding in VS C# Web project.

Actually below coding without any error, but i didn't received any notification on my device finally, anyone have idea? thanks.

static void Main(string[] args)
    {
        var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, @"D:\Share\Certificates_Prod.p12", "");

        // Create a new broker
        var apnsBroker = new ApnsServiceBroker(config);

        // Wire up events
        apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {

            aggregateEx.Handle(ex => {

                // See what kind of exception it was to further diagnose
                if (ex is ApnsNotificationException)
                {
                    var notificationException = (ApnsNotificationException)ex;

                    // Deal with the failed notification
                    var apnsNotification = notificationException.Notification;
                    var statusCode = notificationException.ErrorStatusCode;

                    Console.WriteLine($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");

                }
                else
                {
                    // Inner exception might hold more useful information like an ApnsConnectionException           
                    Console.WriteLine($"Apple Notification Failed for some unknown reason : {ex.InnerException}");
                }

                // Mark it as handled
                return true;
            });
        };

        apnsBroker.OnNotificationSucceeded += (notification) => {
            Console.WriteLine("Apple Notification Sent!");
        };

        // Start the broker
        apnsBroker.Start();

        apnsBroker.QueueNotification(new ApnsNotification
        {
            DeviceToken = "58f0f386003a4b7be..................................",
            Payload = JObject.Parse("{\"aps\":{\"badge\":7}}")
        });

        // Stop the broker, wait for it to finish   
        // This isn't done after every message, but after you're
        // done with the broker
        apnsBroker.Stop();
    }

Solution

  • You are using the legacy API. There is 5 year complete C# walk though on it here if you wish to continue to use that.

    Apple now supports APNs over http/2. Instead of writing your own code take a look at some existing libraries such as PushSharp which will take care of the low level API and error handling for you.

    // Configuration (NOTE: .pfx can also be used here)
    var config = new ApnsConfiguration (ApnsConfiguration.ApnsServerEnvironment.Sandbox, 
        "push-cert.p12", "push-cert-pwd");
    
    // Create a new broker
    var apnsBroker = new ApnsServiceBroker (config);
    
    // Wire up events
    apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {
    
        aggregateEx.Handle (ex => {
    
            // See what kind of exception it was to further diagnose
            if (ex is ApnsNotificationException) {
                var notificationException = (ApnsNotificationException)ex;
    
                // Deal with the failed notification
                var apnsNotification = notificationException.Notification;
                var statusCode = notificationException.ErrorStatusCode;
    
                Console.WriteLine ($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");
    
            } else {
                // Inner exception might hold more useful information like an ApnsConnectionException           
                Console.WriteLine ($"Notification Failed for some unknown reason : {ex.InnerException}");
            }
    
            // Mark it as handled
            return true;
        });
    };
    
    apnsBroker.OnNotificationSucceeded += (notification) => {
        Console.WriteLine ("Apple Notification Sent!");
    };
    
    // Start the broker
    apnsBroker.Start ();
    
    foreach (var deviceToken in MY_DEVICE_TOKENS) {
        // Queue a notification to send
        apnsBroker.QueueNotification (new ApnsNotification {
            DeviceToken = deviceToken,
            Payload = JObject.Parse ("{\"aps\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"sound\":\"default\"}}")
        });
    }
    
    // Stop the broker, wait for it to finish   
    // This isn't done after every message, but after you're
    // done with the broker
    apnsBroker.Stop ();