#APNS for #IOS13 in C#
APNS, or apple’s push notification service has changed a bit in iOS 13, so you need to make some changes to your code to make it work again.
First off, I used to use MOON APNS (https://github.com/arashnorouzi/Moon-APNS/issues) but it seems to be no longer maintained. It used a raw TCP/IP mechanism, which I’m sure is ultra-performant, but it’s really hard to debug, so I decided to just change the framework to PushSharp (https://github.com/Redth/PushSharp)
Now, my code is very much a fire and forget, if it works, good, it it doesn’t – nevermind. I’m not catching or logging any exceptions, you might want to do this. I’m not getting into how to create the .P12 key, that’s a whole post on it’s own.
public static void Push(string message, string apns)
{
// Change p12 after acceptance!
var strP12 = HttpContext.Current.Server.MapPath(“~/certs/2020/sandbox.2020.p12”);
var config = new ApnsConfiguration (ApnsConfiguration.ApnsServerEnvironment.Sandbox,
strP12, “xxxxxx”);
var apnsBroker = new ApnsServiceBroker (config);
apnsBroker.Start ();
apnsBroker.OnNotificationSucceeded += (notification) =>
{
apnsBroker.Stop();
};
var oPayload = new
{
aps = new
{
alert = new
{
body = message
}
}
};
var payloadJson = JsonConvert.SerializeObject(oPayload);
apnsBroker.QueueNotification (new ApnsNotification {
DeviceToken = apns,
Payload = JObject.Parse(payloadJson)
});
}
It only sends a text message, it doesn’t have badges, sounds, or alerts, but that’s all I needed.