Questions? Feedback? powered by Olark live chat software
Přeskočit navigaci

Notification Hubs enabling APNS priority for pushes to iOS devices

Publikováno dne 14 června, 2016

Program Manager, Azure App Service

Apple Push Notification Services added “apns-priority” to its notification send request headers a while back for users to specify send priorities with ten marking immediate and high-priority sends and five marking device power saving sends. We want to share a few sample snippets for how to use this feature with Notification Hubs' .NET SDK.

To push without templates, simply set the X-Apns-Priority property in the notification headers before you send the push request.

var notification = new AppleNotification(@"
{
  ""aps"": {
    ""content-available"": 1
  },
  ""data"": {
    ""key"": ""value""
  }
}");
notification.Priority = "5";

await notificationHubClient.SendNotificationAsync(notification);

To perform template push using template Registration model, you can set the apns-priority either in the device registration itself or during the sending process depending on your scenario.

If you prefer linking the apns-priority to the templates themselves, add priority value to your registration.

var registration = new AppleTemplateRegistrationDescription(
  "deviceToken",
  @"{
    ""aps"": {
      ""content-available"": 1
    },
    ""data"": {
      ""key"": ""$(Message)""
    }
  }");

registration.Priority = "10";
await notificationHubClient.CreateRegistrationAsync(registration);

If you want flexibility over apns-priority as you send to templates, add a placeholder for it and specify the priority during send.

// Template registration with priority expression
var registration = new AppleTemplateRegistrationDescription(
  "deviceToken",
  @"{
    ""aps"": {
      ""alert"":""New notification!""
    }
  }");
registration.Priority = "$(Priority)";

await notificationHubClient.CreateRegistrationAsync(registration);

To specify priority on send:

var notification = new TemplateNotification(new Dictionary<string, string>()
{
  {"Priority", "10"}
});
await notificationHubClient.SendNotificationAsync(notification);

 

Try it out and let us know what you think!