7. Handling Push Notification Errors
When working with push notifications, you should also handle the ErrorOccurred
event to deal with any exceptional conditions. You should always handle
this event because it will help you locate badly formatted messages and
low-battery issues that will stop push messages from working. You can
handle this event like any other event:
_myChannel.ErrorOccurred += channel_ErrorOccurred;
The event passes in an error argument that has an ErrorType
property that can be used to determine the type of error. You should
change the way you alert the user to this based on the type of error.
In addition, the handler should use the Dispatcher
to ensure that any reporting of these errors occurs on the UI thread (as the ErrorOccurred
event could be thrown on any thread):
void channel_ErrorOccurred(object sender,
NotificationChannelErrorEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
switch (e.ErrorType)
{
case ChannelErrorType.ChannelOpenFailed:
MessageBox.Show("Failed to open channel");
break;
case ChannelErrorType.NotificationRateTooHigh:
MessageBox.Show("Push Notifications are too frequent.");
break;
case ChannelErrorType.PayloadFormatError:
MessageBox.Show(@"XML or Headers were incorrect
for the Push Message");
break;
case ChannelErrorType.MessageBadContent:
MessageBox.Show("Live Tile Data is invalid.");
break;
case ChannelErrorType.PowerLevelChanged:
{
MessageBox.Show(@"Push Notifications are
affected by the current power level");
// Can get the power level from the event info
ChannelPowerLevel level =
(ChannelPowerLevel)e.ErrorAdditionalData;
switch (level)
{
case ChannelPowerLevel.NormalPowerLevel:
// All Push Messages are processed
break;
case ChannelPowerLevel.LowPowerLevel:
// Only Raw Push Messages are processed
break;
case ChannelPowerLevel.CriticalLowPowerLevel:
// No Push Messages are processed
break;
}
break;
}
}
});
}
The ChannelErrorType
is returned in the event arguments and can be used to determine the error types. These error types are detailed in Table 3.
TABLE 3 ChannelErrorType Enumeration
The PowerLevelChanged
enumeration’s values are detailed in Table 4.
TABLE 4 ChannelPowerLevel Enumeration