【发布时间】:2012-12-07 15:05:05
【问题描述】:
我正在尝试优化我服务器上的推送通知。现在我一个一个地发送那些(带有一个旧图书馆),这需要一段时间(4小时)。
我重构了我的服务以发送带有大量设备令牌的通知(现在我尝试批量处理 500 个令牌)。为此,我正在使用Redth/PushSharp 库。我关注了sample code,然后我对其进行了调整以将通知发送到多个设备令牌。
PushService service = new PushService();
//Wire up the events
service.Events.OnDeviceSubscriptionExpired += new PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
service.Events.OnDeviceSubscriptionIdChanged += new PushSharp.Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
service.Events.OnChannelException += new PushSharp.Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException);
service.Events.OnNotificationSendFailure += new PushSharp.Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
service.Events.OnNotificationSent += new PushSharp.Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent);
service.Events.OnChannelCreated += new PushSharp.Common.ChannelEvents.ChannelCreatedDelegate(Events_OnChannelCreated);
service.Events.OnChannelDestroyed += new PushSharp.Common.ChannelEvents.ChannelDestroyedDelegate(Events_OnChannelDestroyed);
//Configure and start ApplePushNotificationService
string p12Filename = ...
string p12FilePassword = ...
var appleCert = File.ReadAllBytes(p12Filename);
service.StartApplePushService(new ApplePushChannelSettings(true, appleCert, p12FilePassword));
var appleNotification = NotificationFactory.Apple();
foreach (var itemToProcess in itemsToProcess)
{
itemToProcess.NotificationDateTime = DateTime.Now;
mobile.SubmitChanges();
string deviceToken = GetCleanDeviceToken(itemToProcess.MobileDevice.PushNotificationIdentifier);
appleNotification.ForDeviceToken(deviceToken);
}
service.QueueNotification(appleNotification
.WithAlert(itemsToProcess[0].MobileDeviceNotificationText.Text)
.WithSound("default")
.WithBadge(0)
.WithCustomItem("View", itemsToProcess[0].Value.ToString()));
//Stop and wait for the queues to drains
service.StopAllServices(true);
然后我尝试向 2 台设备发送 3 条通知。只有第一个设备得到了它们(问题与设备无关,因为我分别尝试了它们)。 紧接着在PushChannelBase class 中抛出一个 OperationCanceledException。所以我不知道怎么了。有什么想法吗?
【问题讨论】:
-
嗨。这可以在单个请求中将单个推送消息发送到多个设备。我进行了研发并实现了代码,并在 5-10 台设备上进行了测试。
标签: push-notification apple-push-notifications pushsharp