I'd like to be able to get the date of when a remote notification was received on an iOS device.
I'm attempting to determine the amount of latency between when my server fires a push notification to when it's received on a user's device.
I'd like to be able to get the date of when a remote notification was received on an iOS device.
I'm attempting to determine the amount of latency between when my server fires a push notification to when it's received on a user's device.
In application:didReceiveRemoteNotification:fetchCompletionHandler:
of you application delegate, create a NSDate
with the current date, then save it inside an array in NSUserSettings or directly inside.
With this you even have an history of received notification and can process them later.
application:didReceiveRemoteNotification:fetchCompletionHandler:
will call in the background only when you have added content-available
key with value 1 into the notification payload.
From Apple documentation :
For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app. https://developer.apple.com/library/prerelease/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
You could add a custom sent-date field to the notification payload, if you control the back-end connected to APNS, and parse for it in didReceiveRemoteNotification. Obviously this will let you get the date when a remote notification was sent, not received, but in general the difference is small.
The solution suggesting to use the 'content-available' key may not be what you want, as it launches the app in the background on such notifications and may lead to rank your app as top battery offender in Settings.app > Battery > Battery Usage. It would also stop working in Low Power Mode, or if the user disables Background App Refresh.
Background App Refresh and the 'content-available' key are meant for apps that download content in the background. If your app isn't doing that, don't use them.
Create and initialize NSDate object in didReceiveRemoteNotification. NSDate *notificationDate = [NSDate date] should work.
You can see when an Push Notification is received by looking at the system(not app) console. It seems newer iOS versions filter the log, but if you kill your app beforehand you'll see something like this
Jun 1 17:55:38 Aarons-iPhone-6-Plus SpringBoard[657] <Warning>: High Priority Push: net.test.test - App killed –