Lakshay Dulani
Lakshay Dulani

Reputation: 1776

How to use the library PushSharp to send push notifications to iOS?

I am using the latest PushSharp version to send push notification through APN. I am using the below code given in their Git wiki page to send the notifications:

// 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 ($"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 ();

foreach (var deviceToken in MY_DEVICE_TOKENS) {
    // Queue a notification to send
    apnsBroker.QueueNotification (new ApnsNotification {
        DeviceToken = deviceToken,
        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 ();

The confusions -

  1. I don't know if the method apnsBroker.QueueNotification sends the push at all, or just queues it up.

  2. I don't know if I need to install the apple certificate in some way on my Windows machine.

There is no proper sample code available online with the latest version of PushSharp.

Upvotes: 1

Views: 882

Answers (2)

Umut Bebek
Umut Bebek

Reputation: 384

The code works as it. But there are some uncertain points as you said.

First notification will be send right away when you queue it, it is just a async mechanism to not to wait the code there. So if anything goes wrong (or right) you can handle it via broker's events.

Second part is a little complicated. First of all you have create a certificate for pushnotifications on a macOS machine. Than you have to upload it to your developer account etc. You can find videos how to that via google. It is pretty long to describe it here. Than you have to export your "Apple Push Services" certificate from your macOSmachine to a p12 file. And get and put that .p12 file to your .net service folder for example to "App_Data" folder and load it like (i assume you are writing a web service):

 var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
                Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "yourfileName.p12"),"yourFilePassword");

I hope that helps you.

Upvotes: 0

Lakshay Dulani
Lakshay Dulani

Reputation: 1776

  1. Just fire the above code in a console application and Pushsharp will send the notifications.

  2. Apple allow a single push token for a push notification at a time.

Upvotes: 0

Related Questions