@Sumesh 您可以使用.NET 开源库PushSharp 将消息推送到Apple 的APNS 服务。
step1:下载PushSharp开源项目编译https://github.com/Redth/PushSharp!
step2:编译成功后APNS推送需要用到Newtonsoft.Json.dll、PushSharp.Apple.dll、PushSharp.Core.dll三个汇编库文件
step3:然后ios客户端需要提供.p12证书文件和证书文件的加密密码
step4:准备好这些后,新建一个console程序引用上面的库文件,将证书复制到根目录,修改属性,输出到复制目录,和往常一样复制
控制台程序代码如下:
class Program
{
static ApnsConfiguration config;
static ApnsServiceBroker apnsBroker;
static void Main(string[] args)
{
config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, "certificate.p12", "certificate's password");
apnsBroker = new ApnsServiceBroker(config);
//post catch error
apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
{
aggregateEx.Handle(ex =>
{
if (ex is ApnsNotificationException)
{
var notificationException = (ApnsNotificationException)ex;
//handle failed APN msg
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
Console.WriteLine("Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}" + notification.DeviceToken);
}
else
{
//internal catch error
Console.WriteLine("Apple Notification Failed for some unknown reason : {ex.InnerException}" + notification.DeviceToken);
}
// flag handle
return true;
});
};
//successed
apnsBroker.OnNotificationSucceeded += (notification) =>
{
Console.WriteLine("Apple Notification Sent ! "+notification.DeviceToken);
};
//engined start
apnsBroker.Start();
}
/// <summary>
/// apn message
/// </summary>
public static void SendMsg()
{
List<string> MY_DEVICE_TOKENS = new List<string>() {
"1f6f37acad29348c6a5957529c9fa61ad69766ec9c7367948745899cbccdfd51",
"1f6f37acad29348c6a5957529c9fa61ad69766ec9c7367948745899cbccdfd51"
};
foreach (var deviceToken in MY_DEVICE_TOKENS)
{
// queue triger a notification message to iOS client
apnsBroker.QueueNotification(new ApnsNotification
{
DeviceToken = deviceToken, // this one deviceToken from iOS client,it have to
Payload = JObject.Parse("{\"aps\":{\"sound\":\"default\",\"badge\":\"1\",\"alert\":\"This is a test of a mass advertising message push message\"}}")
});
}
//engined stop
apnsBroker.Stop();
Console.Read();
}
}
希望能帮到你