Send Json to APNS using Push-Sharp library

NET picture NET · Mar 19, 2013 · Viewed 8.4k times · Source

I need to send json to IOS device like this:

    {"aps":{"content-available":1}}

Should I use AppleNotification or AppleNotificationPayLoad class? please give sample code.Here is example how I create notification now:

        AppleNotification notification = NotificationFactory.Apple()
                                                      .ForDeviceToken(token)
                                                      .WithAlert(message)
                                                      .WithSound("default")
                                                      .WithBadge(7);

Answer

Brent Barbata picture Brent Barbata · Sep 6, 2013

As Eran mentioned, with PushSharp you can only send custom payload parameters outside of aps. So if you wanted to send something like this:

{"aps":{ whatever data... },"content-available":1}

You can do so like this:

// Add these to your references
using System.IO;
using PushSharp;
using PushSharp.Apple;
using PushSharp.Core;

//Create our push services broker
var push = new PushBroker();

//Registering the Apple Service and sending an iOS Notification
var appleCert = File.ReadAllBytes(string.Format(@"applePushCert.p12"));
push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "password"));
push.QueueNotification(new AppleNotification()
        .ForDeviceToken("your device token")
        .WithAlert("Hello World!")
        .WithBadge(7)
        .WithSound("sound.caf")
        .WithCustomItem("content-available", 1));