5

我正在使用Push Sharp库向 Apple APN 服务器发送推送通知。代码效果很好,我可以发送 1000 条通知。

问题是,如果我尝试使用无效的设备令牌发送通知,我会收到来自 push sharp 框架的失败消息,pushsharp.apple.notificationfailureexception并且在该点之后排队的每条消息都不会发送。基本上,如果单个通知失败,PushSharp 会清除其队列。

例如,如果我将 4 个通知 (1,2,3,4) 排队并且通知 2 具有无效的设备令牌,则将发送通知 1,通知 2 将失败,并且不发送 3 和 4(并且不会触发任何事件通知这个的)。

我了解将不会发送带有无效设备令牌的通知,但将其他N个排队的通知丢弃在地板上是不可接受的。

有什么解决方法吗?

这是我的代码:

_appleSettings = new ApplePushChannelSettings(!NOTIFICATION_SERVICE_USE_DEVELOPMENT,
     NOTIFICATION_SERVICE_USE_DEVELOPMENT 
     ? SSL_CERTIFICATE_NAME_DEV : SSL_CERTIFICATE_NAME_PROD, 
     SSL_CERTIFICATE_PASSWORD);

_appleSettings.ConnectionTimeout = NOTIFICATION_SERVICE_CONNECTION_TIMEOUT;
_appleSettings.FeedbackIntervalMinutes = 0; /*WE WILL HANDLE THE FEEDBACK EXTERNALLY*/
_appleSettings.MaxConnectionAttempts = NOTIFICATION_SERVICE_RETRY_ATTEMPS;

_serviceSettings = new PushServiceSettings();
_serviceSettings.MaxAutoScaleChannels = NOTIFICATION_SERVICE_NUM_CONNECTIONS;

_pushBroker = new PushBroker();
_pushBroker.OnChannelCreated += _pushBroker_OnChannelCreated;
_pushBroker.OnChannelDestroyed += _pushBroker_OnChannelDestroyed;
_pushBroker.OnChannelException += _pushBroker_OnChannelException;
_pushBroker.OnDeviceSubscriptionChanged += _pushBroker_OnDeviceSubscriptionChanged;
_pushBroker.OnDeviceSubscriptionExpired += _pushBroker_OnDeviceSubscriptionExpired;
_pushBroker.OnNotificationFailed += _pushBroker_OnNotificationFailed;
_pushBroker.OnNotificationRequeue += _pushBroker_OnNotificationRequeue;
_pushBroker.OnNotificationSent += _pushBroker_OnNotificationSent;
_pushBroker.OnServiceException += _pushBroker_OnServiceException;


//now add those settings to the push broker
_pushBroker.RegisterAppleService(_appleSettings, _serviceSettings);


notification = new AppleNotification(notificationMessage.DeviceExtContext);
notification.Payload.Alert.Body = notificationMessage.Message;
notification.Payload.Sound = NOTIFICATION_SOUND;
// notification.Payload.Badge = 1;
notification.Tag = notificationMessage;

//attempt to queue the notification
_pushBroker.QueueNotification(notification);
4

1 回答 1

1

好的。这个答案这个答案似乎有帮助。

和你一样,我在测试中发现当我有一个无效的设备令牌时我会收到一个 OnNotificationFailed 事件,并且每个后续通知也会失败。

简短的故事是,Apple 似乎在 Invalid Device Token 上默默地断开了连接,然后接下来的一切都失败了(同样的错误)。

一切都没有丢失。在 OnNotificationFailed 的事件处理程序中,您可以检查传入的通知内容以查看哪些通知失败。

下一个合乎逻辑的步骤是单独遍历它们,重新发送相同的通知。然后将第二次失败的那些从未来的通知中删除为无效和/或将成功的那些标记为好。

于 2016-11-05T04:53:32.077 回答