How long do Apple retain notifications to be pushed to iOS devices until they expire?
If an app attempts to send a notification to an iOS device that is either powered off or has no network connectivity push notifications need to be kept in queue.
For how long does Apple retain notifications to allow the device time to contact the APNS servers?
What you are referring to is covered under Quality of Service component of Apple push notifications. It's addressed in the following linked Apple Developer documentation:
- Local and Remote Notification Programming Guide - Quality of Service, Store-and-Forward, and Coalesced Notifications
Apple Push Notification service includes a Quality of Service (QoS) component that performs a store-and-forward function. If APNs attempts to deliver a notification and the destination device is offline, APNs stores the notification for a limited period of time and delivers it when the device becomes available again. This component stores only the most recent notification per device and per app. If a device is offline, sending a notification request targeting that device causes the previous request to be discarded. If a device remains offline for a long time, all its stored notifications in APNs are discarded.
and
- Communicating with APNs - HTTP/2 Request to APNs
apns-expiration
A UNIX epoch date expressed in seconds (UTC). This header identifies the date when the notification is no longer valid and can be discarded.
If this value is nonzero, APNs stores the notification and tries to deliver it at least once, repeating the attempt as needed if it is unable to deliver the notification the first time. If the value is 0, APNs treats the notification as if it expires immediately and does not store the notification or attempt to redeliver it.
Referring the Local and Remote Notification Programming Guide should address your concerns related to Apple push notifications.
It depends on the service that sent the notification in the first place. The service (i.e. the backend system that supports an app such as Facebook or any other app) sends their notifications to Apple's push notification service over a HTTP/2-based API. In that API it sets a parameter known as "apns-expiration", which determines how long time Apple's push notification service will queue the notification to be delivered when the device is online again.
The "apns-expiration" parameter can be set to 0, which means that it won't be queued at all.
If set to a higher number than 0 that is the date and time (represented as the number of seconds since midnight on the 1st of January 1970 in UTC), the message will be removed from queued.
Finally, the service can choose not to send the parameter at all, or send it as -1 (negative). This means it should be queued for as long as possible.
The maximium queue time is not infinite, but is chosen by Apple - and seemingly not documented anywhere. However, PC World described in 2009 that the maximum at that time was 28 days:
https://www.pcworld.com/article/167652/the_limits_of_apples_push_notification_for_the_iphone.html