Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When one PushSharp message fails, they all fail

I'm using the Push Sharp library to send push notifications to the Apple APN server. The code works great, I can send 1000s of notifications.

The problem is, if I attempt to send a notification with an invalid device token I receive a failure message from the push sharp framework pushsharp.apple.notificationfailureexception and every message queued after that point is not sent. Basically, PushSharp clears its queue if a single notification fails.

For example, if I queue 4 notifications (1,2,3,4) and notification 2 has an invalid device token, notification 1 will be sent, 2 will fail, and 3 and 4 are not sent (and no event is fired informing of this).

I understand that the notification with the invalid device token will not be sent but its not acceptable to drop the other N queued notifications on the floor.

Are there any workarounds for this?

Here's my code:

_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);
like image 356
user3335999 Avatar asked Nov 10 '22 08:11

user3335999


1 Answers

Ok. this answer and this answer seem to help.

Like you I've found in my testing that I'll get an OnNotificationFailed event when I have an Invalid Device Token, and that every subsequent Notice will fail as well.

Short story is that it seems that Apple silently drops the connection on an Invalid Device Token, and then everything that follows fails (same error).

All is not lost. In event handler for OnNotificationFailed, you can check the contents of the notification that's passed in to see which ones have failed.

The next logical step would be to walk them individually, re-sending the same notification. Then pruning out the ones that get a second failure as being invalid from future notifications and/or marking the ones that succeed as good.

like image 87
BIBD Avatar answered Jan 04 '23 03:01

BIBD