5

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);
BIBD
  • 14,299
  • 24
  • 78
  • 130
user3335999
  • 363
  • 1
  • 2
  • 14
  • We have the same issue. What we did is send a batch of 10 or so notifications in the queue and call StopAllServices. We then reallocate a new PushBroker. This way the damage is limited to only losing a few of the notifications. We may have to move away from PushSharp and consider alternatives for precisely these types of issues. – Rudy May 27 '14 at 17:09

1 Answers1

1

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.

Community
  • 1
  • 1
BIBD
  • 14,299
  • 24
  • 78
  • 130