Can Apple Push Notifications be used as message queues? - ios

Lets say I have a chat application and I am messaging another user (User B), our messages are being received normally using sockets, however when the User B goes offline he is disconnected from the socket server however user A is continues to text him so my server issues a push notification to User B's device for every message User A sends. My question is will APNS act as a message broker and queue all the messages until User B logs back in and receives them? Or do I have to store unreceived messages elsewhere

The answer is readily found in Apple's documentation
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.
So, no, you can't use APN as a message broker.
You can use the push notification as a signal to wake up and sync with a server-side message queue. RabbitMQ or Kafka might be candidate brokers, and MQTT looks promising as a protocol. You will need to work out how and when you discard the contents of message queues that are not successfully delivered to a device.

Related

Why does APNS deliver only last queued notification instead of all queued notifications?

Problem Statement : I'm using voip notification to show local notification.I'm trying to send notification from Apns to a offline device. So, It queued all pending notifications. But, It only send last queued notification once device becomes
online.
As per apple documentation Link
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.
To allow the coalescing of similar notifications, you can include a
collapse identifier within a notification request. Normally, when a
device is online, each notification request that you send to APNs
results in a notification delivered to the device. However, when the
apns-collapse-id key is present in your HTTP/2 request header, APNs
coalesces requests whose value for that key is the same. For example,
a news service that sends the same headline twice could use the same
collapse identifier value for both requests. APNs would then coalesce
the two requests into a single notification for delivery to the
device.

iOS APNS - Missing Push Notifications

I am sending notifications to apns one after the other just to test it. For example I send 20 notifications. Each time I send, a counter is incremented and its value is added to the beginning of the title in order to track delivery. The phone receives the first 5 and the 16th but none else.
I am not getting an error for any of them from the apns. They all receive success response.
Why could this be?
EDIT (some extra info):
I am communicating directly with https://api.push.apple.com. I am using http2 and jwtToken. And as suggested, I am keeping the connection alive.
EDIT2:
Device is not offline during this period.
Well, if you send the notifications in a short time period, some may be discarded, especially if the device was temporarily offline when some of the notifications were sent.
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.
(Source)
Your app should never depend on all the notifications being delivered.

iOS. Send push to devices which are offline

I want to send push notification to app users. Users whom online will receive it immediately, right?. But what about users whom offline? Will they get notification after they become online?
Can you tell me more about than push notifications will de delivered?
Are messengers use pushes to send notifications about new messages?
Not possible in offline.
Check it: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
Apple Push Notification Service includes a default Quality of Service (QoS) component that performs a store-and-forward function. If APNs attempts to deliver a notification but the device is offline, the QoS stores the notification. It retains only one notification per application on a device: the last notification received from a provider for that application. When the offline device later reconnects, the QoS forwards the stored notification to the device. The QoS retains a notification for a limited period before deleting it.
If a device remains offline for a long time, all notifications that were being stored for it are discarded; when the device goes back online, none of the notifications are displayed.
Basically Apple will try to deliver the notification when the device will become online again (in a resonable time frame).
Apple Push Notification service includes a default Quality of Service (QoS) component that performs a store-and-forward function. If APNs attempts to deliver a notification but the destination device is offline, APNs stores the notification for a limited period of time and delivers it to the device when the device becomes available.
From https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9

didReceiveRemoteNotification is not called for every push notification

It seems that if I send multiple push notifications to the same device, at the same time, didReceiveRemoteNotification is not called for every notification sent. Lets say I send 6 notifications, didReceiveRemoteNotification is only called on average 3 times. That's if the app is currently running. BUT if I am outside the app and send 6 push notifications, all will be delivered to the notification center/lock screen. Is this the expected behavior?
That's the expected behavior:
Apple Push Notification service includes a default Quality of Service
(QoS) component that performs a store-and-forward function.
If APNs attempts to deliver a notification but the device is offline,
the notification is stored for a limited period of time, and delivered
to the device when it becomes available.
Only one recent notification for a particular application is stored.
If multiple notifications are sent while the device is offline, each
new notification causes the prior notification to be discarded. This
behavior of keeping only the newest notification is referred to as
coalescing notifications.
If the device remains offline for a long time, any notifications that
were being stored for it are discarded.
While in your case the device in online, the important thing to note is that only one notification is stored for your app for each device by APNs. Suppose you send 3 notifications at once. The APN server is delivering the first message to the device when the second message arrives. It stores the second message. Then the third message arrives while the first is still being delivered, so the third message overrides the second, and the second is never delivered.
Here another quote which you may find more convincing:
Some Notifications Received, but Not All
If you are sending multiple notifications to the same device or
computer within a short period of time, the push service will send
only the last one.
Here's why. The device or computer acknowledges receipt of each
notification. Until the push service receives that acknowledgment, it
can only assume that the device or computer has gone off-line for some
reason and stores the notification in the quality of service (QoS)
queue for future redelivery. The round-trip network latency here is of
course a major factor.
As described in the Local and Push Notification Programming Guide, the
QoS queue holds a single notification per app per device or computer.
If the service receives another notification before the one in the
queue is sent, the new notification overwrites the previous one.
All of this points out that the intent is that a notification
indicates to an app that something of interest has changed on the
provider, and the app should check in with the provider to get the
details. Notifications should not contain data which isn't also
available elsewhere, and they should also not be stateful.
Any push notification that isn't delivered immediately was queued for
future redelivery because your device was not connected to the
service. "Immediately" of course needs to take latency for your
connection into account. Outlying cases would be beyond 60 seconds as
APNs will time out at that point.
Source

What happens to push notifications after losing internet access?

What happens if an application is waiting for a push notification and the internet connection is unavailable?
For example, I tell my server to do task "A", and notify me via APNS when the task is ready. But, after I send the task to the server, the application loses internet connectivity.
Notification will be delivered when internet connection on iPhone becomes available again.
Apple Push Notification Service includes a default Quality of Service
(QoS) component that performs a store-and-forward function. If APNs
attempts to deliver a notification but the device is offline, the QoS
stores the notification. It retains only one notification per
application on a device: the last notification received from a
provider for that application. When the offline device later
reconnects, the QoS forwards the stored notification to the device.
The QoS retains a notification for a limited period before deleting
it.
See "Quality of Service" section of Local and Push Notification Programming Guide.
Be careful when sending PUSHes with own server API, and also take into account apns-expiration field in APNs Provider API payload.
This header identifies the date when the notification is no longer valid and can be discarded...
...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...
By Apple.
It can be the reason, that the push notification does not come after network connection was restored.

Resources