iOS- Apple push notification resend from APNS - iphone

From Apple's document I understand that if the device is offline, the APNS holds the last notification and sends it to the device when the device is back online. Is there is any mechanism to avoid that resending?

The correct answer is YES
If you send notifications using the enhanced binary format (the one that includes message identifier and expiry), you can use the expiry parameter to prevent the notification from being stored in the APN server. This way the notification is delivered only if the device is online when the APN server first tries to deliver it.
Expiry
A fixed UNIX epoch date expressed in seconds (UTC) that identifies when the notification is no longer valid and can be discarded. The expiry value uses network byte order (big endian). If the expiry value is positive, APNs tries to deliver the notification at least once. Specify zero (or a value less than zero) to request that APNs not store the notification at all.

The answer is NO
Here is what apple says about it,
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.

I agree with Eran.
This part of your question "the APNS holds the last notification and sends it to the device when the device is back online" is only true if Expiration date is different of zero.
If you do want the message to be delivered when the phone gets online, the message will wait until "Expiration date" before be discarded.
APNS documentation:
Expiration date 4 bytes A UNIX epoch date expressed in seconds (UTC)
that identifies when the notification is no longer valid and can be
discarded. If this value is non-zero, APNs stores the notification
tries to deliver the notification at least once. Specify zero to
indicate that the notification expires immediately and that APNs
should not store the notification at all.
For more details, refer to https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html

Related

Push Notifications after 2-3 days?

I am developing an application which requires push notification.
I have provided Push Notification feature but I need to know what if a user has turned off his/her iDevice for 2-3 days or for whatever reason, the device is turned off for few hours/days/minutes etc.
If I send a push notification at that point of time, would the user receive the notification when he turns on his device.
Thanks
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.
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.
Still you have any query then you can refer the below link
http://developer.apple.com/library/ios/#technotes/tn2265/_index.html

Is there a way to include local time and timezone on APNS Push messages

I need to send out an APNS Push Notification message that needs to include time in the alert message. Is there a way to display this time in the local timezone of the end user (based on current geographic location)?
If the time is part of the alert, there's no way to do that, since you have no control on the display of the alert (except for the language, which can be controlled by sending a loc-key parameter, which gets translated to an actual text in the device, based on the language the device uses).
If you pass the time as a custom parameter of the payload, you can process it in your app and display it however you want (assuming the application was already running, or that the user tapped the notification if it wasn't already running).
If the time is part of the alert text, you'll have to determine the timezone in the server and send the time as it should be displayed.

Does APNS add the device token to feedback server if the device is unreachable for a long time?

If I go abroad for a vacation for a month and my iPhone has no data/Wifi connection or if I have turned off my phone for a long period of time, in both case APNS will not be able to deliver push notification to my iPhone. My question is, will APNS think the device token is "bad", therefore adding it the the feedback server?
No it doesn't.
You send notifications to APNS
APNS sends notifications to each device
The device tells APNS if it rejects the notification (uninstalled app, ...)
From :
If a provider attempts to deliver a push notification to an application, but the application no longer exists on the device, the device reports that fact to Apple Push Notification Service. This situation often happens when the user has uninstalled the application. If a device reports failed-delivery attempts for an application, APNs needs some way to inform the provider so that it can refrain from sending notifications to that device. Doing this reduces unnecessary message overhead and improves overall system performance.

Is Apple's push notification service reliable?

I have an iOS app using push notification but once in a while I'm not getting a notification on my device when I expect to receive one. I would receive all the subsequent notifications. I confirmed with my backend to make sure that all the notifications were sent successfully.
So my question is: is APNs nearly 100% reliable or should I just expect to miss some notifications here and there because of intermittent 3G/wifi connection?
I would think that APNs works as a queueing system and retry if it wasn't successful within the first few times.
The APN service will queue messages up -- but Apple doesn't guarantee delivery of all messages. Only the last message from an application will be kept in the queue when the user is offline. Additionally, old messages may be deleted.
Local and Push Notification Programming Guide
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.
I have an azure website (and mobile service, service bus, db, active directory etc) that sends push notifications to a xamarin app on a windows phone and an iphone. The first notification is received by both. The second notification is only received by the windows phone, it doesnt make it to the iphone. If I send another notification, it is received by both. So I investigated the behaviour at bit more and found that if I machine gun a series of notifications (hand typed one per 2 seconds) the windows phone received them all but the iphone only receives the first one. But if I wait a while and send a notification it is received by both devices. The next test is to see if the notifications are always received with a 5 minute gap. Sent two messages with a five minutes gap in between, both windows phone and iphone received both notifications.

HOWTO remove device tokens received by Apple APNS feedback

I am successfully fetching Apple APNS feedback data via PHP. The structure that I am getting (after some processing) looks something like this:
timestamp
device token
My question is how to know which of the device tokens should I remove from my database and stop sending notifications to them.
Regardz,
Mladjo
The timestamp is the crucial element here. The timestamp sent by Apple indicates the last time the push service attempted to deliver a message to the device and found the app to be uninstalled. If the device has re-registered with your service since then there is no need to delete it.
Therefore, every single time your app loads and sends the token to your service, you should log the time in your data store. When you run feedback you should check the time from Apple and compare it to the time you last received an update from the app on the device. If the time Apple sends is newer then the time you received an updated then you should delete (or disable) the device. If the time from Apple is earlier then you do not need to delete it because the user has reinstalled the device since Apple last tried to deliver.
All devices given by feedback are 'failed' and should be removed. No feedback means no devices should be removed. It's covered over on the Apple Documentation:
Apple APN Documentation
neat explanation #argon, however I have another question about the timestamp.
Every time when an app enables push notification, the device token is sent to the server. Should I taken the timestamp from my server as to be persisted along with device token as didRegisterForRemoteNotificationsWithDeviceToken only gives deviceToken and not time. If my server runs in different timezone and APNS is running at different timezone, then the registration time stored( along with device token) cannot be compared with timestamp received from APNS feedback to check the sequence of register -> uninstall -> reregister.
I presume the APNS feedback timestamp is in UTC and the timestamp the server stores along with device token has to be converted to UTC before storing the ISO timestamp. This way both the timestamp will be in same timezone and diff check will be consistent.
please clarify
#fyasar,
So your recommendation is to store the device token against device id(or some key). When a feedback is received for a device token, remove that device token row from DB, right? If have understood right, that wouldn't work in scenario were a user installs app, uninstalls it and then installs it again all with in a short duration and the feedback service was queried only after all this happened. In this case, if timestamp in feedback is not considered, device token will be removed which is incorrect as user has again installed the app and reregistered for push notification.
My question is this, as suggested in apple doc and many blogs, on registration, when device token is persisted, timestamp has to be persisted along with it. What time zone's ISO time should be persisted or what is the time zone on which feedback service returns the timestamp.
A timestamp (as a four-byte time_t value) indicating when APNs
determined that the app no longer exists on the device. This value,
which is in network order, represents the seconds since 12:00 midnight
on January 1, 1970 UTC.
you can compare it with your table's last insert time and then remove the invalid token from db, In my case i am using mysql and php for sever side
$sql="SELECT insert_time from device_tokens ORDER BY insert_time DESC LIMIT 1";
it will return last updated time from db and then i just convert it into epoch timestamp by using
$sql1="SELECT UNIX_TIMESTAMP(' $timestamp')";
and finally i just compare it with apns feedback timestamp like this
if($inactive_Timestamp>$dbTime_stamp)
{
foreach ($apnsfeedback_tokens as $key => $value) {
# code...
$inactive_Token=$value['devtoken'];
$sql= "DELETE FROM device_tokens WHERE device_token='$inactive_Token'";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
You should store devices with their device token data, and then you can find these devices according to their device tokens. You might use device token for identifier each device. Than would be easy to find and change their statuses into your db.