【发布时间】:2018-06-29 13:08:33
【问题描述】:
我尝试使用 IIS 上托管的 ASP.NET MVC 项目上的 Pushsharp 库向苹果设备发送推送通知。
我的代码:
public static void SendAppleNotification()
{
// Configuration (NOTE: .pfx can also be used here)
byte[] arr = File.ReadAllBytes("D:\\MySoftware\\pa_Dev.pem");
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
arr, "1234");
// 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();
// Queue a notification to send
apnsBroker.QueueNotification(new ApnsNotification
{
DeviceToken = "660E4433785EFF2B2AA29D5076B039C969F1AADD839D79261328F40B08D26497",
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();
}
注意事项: 1-尝试将 pem 扩展名更改为 p12,但仍然出现相同的问题。 2- 我尝试使用https://pushtry.com/ 发送推送通知,它工作正常,所以问题不是来自认证文件或密码。
pushsharp 内部的问题或缺少配置必须在我的机器上完成,有人知道吗?
【问题讨论】:
标签: c# ios notifications pushsharp