我正在使用最新的PushSharp版本通过APN发送推送通知。我正在使用Git wiki页面中给出的以下代码发送通知:
// Configuration (NOTE: .pfx can also be used here)
var config = new ApnsConfiguration (ApnsConfiguration.ApnsServerEnvironment.Sandbox,
"push-cert.p12", "push-cert-pwd");
// 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 ();
foreach (var deviceToken in MY_DEVICE_TOKENS) {
// Queue a notification to send
apnsBroker.QueueNotification (new ApnsNotification {
DeviceToken = deviceToken,
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 ();混乱-
apnsBroker.QueueNotification是否会发送推送,或者只是将其排队。最新版本的PushSharp在线上没有合适的示例代码可供使用。
发布于 2017-03-04 14:16:28
发布于 2017-03-15 13:15:44
代码按原样工作。但正如你所说,还有一些不确定的地方。
第一个通知将在排队时立即发送,这只是一种不等待代码的异步机制。因此,如果有任何问题(或正确的),您可以通过经纪人的事件来处理。
第二部分有点复杂。首先,您已经为macOS机器上的推送通知创建了证书。比你必须上传到你的开发者帐户等。你可以找到视频如何通过谷歌。在这里描述它是相当长的。而不是将"Apple服务“证书从macOSmachine导出到p12文件。然后获取并将该.p12文件放到您的.net服务文件夹中,例如,将其加载到"App_Data“文件夹中(我假设您正在编写web服务):
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "yourfileName.p12"),"yourFilePassword");我希望这对你有帮助。
https://stackoverflow.com/questions/37464454
复制相似问题