apnsFailureReason function

String apnsFailureReason(
  1. DVHttpResponse response
)

A readable explanation for an APNS rejection.

Apple answers failures with {"reason": "BadDeviceToken"} and nothing else, which is precise and unhelpful in a log. The few reasons that mean something an operator should act on are spelled out; the rest are passed through so a new one is never swallowed.

Implementation

String apnsFailureReason(DVHttpResponse response) {
  final data = response.data;
  final reason = data is Map ? data['reason'] : null;
  if (reason is! String || reason.isEmpty) {
    return 'The push service rejected the notification.';
  }
  return switch (reason) {
    'BadDeviceToken' =>
      'BadDeviceToken: the token is malformed, or belongs to the other APNS '
          'environment than the one configured.',
    'Unregistered' =>
      'Unregistered: the app was uninstalled or the token expired. Stop '
          'sending to it.',
    'TopicDisallowed' =>
      'TopicDisallowed: apns-topic does not match the key\'s app.',
    'ExpiredProviderToken' =>
      'ExpiredProviderToken: the bearer JWT is older than one hour.',
    'InvalidProviderToken' =>
      'InvalidProviderToken: the JWT signature or key id was rejected.',
    'PayloadTooLarge' =>
      'PayloadTooLarge: APNS caps a payload at 4KB (5KB for VoIP).',
    _ => reason,
  };
}