2

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.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
Fred Faust
  • 6,696
  • 4
  • 32
  • 55

4 Answers4

1

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

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
  • 1
    This provides the date of when the app was opened via the notification, not when the notification was actually received on the device. – Fred Faust May 18 '16 at 14:31
  • @thefredelement Updated my answer ;D – David Ansermot May 18 '16 at 14:34
  • Looks promising, I will update the payload and check it out and accept if it's working, thank you! – Fred Faust May 18 '16 at 14:35
  • @thefredelement If you want to measure latency then how are you accounting for clock differences on the devices? – trojanfoe May 18 '16 at 14:51
  • @trojanfoe I'm not sure what you mean? Like a timezone or do you mean if a clock is set wrong? – Fred Faust May 18 '16 at 15:03
  • @thefredelement The clock difference doesnt count as all date are generated on the device. – David Ansermot May 18 '16 at 15:05
  • If you are timing the latency between a server and another device then recording the time it was received is only valid if both the server and device clocks are set to the exact same time. Network latencies are in the order of milliseconds, so how do you propose to solve *that* problem. Recording and saving times is trivial in comparison. – trojanfoe May 18 '16 at 16:03
1

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.

  • You bring up some great points here, Initially i'm going to use it just in my own testing to come up with some metrics. I've noticed some significant delays using Parse and want to switch to native notifications that I fully control and see what the difference is. – Fred Faust May 18 '16 at 14:52
  • If the question is just for testing, pretend your app is doing VoIP, use a VoIP certificate and register with PushKit. All notifications will launch your app in background, and the PushKit equivalent of the didReceiveNotification delegate method will be called immediately, always, no user interaction involved. – Clement Barry May 21 '16 at 13:32
0

Create and initialize NSDate object in didReceiveRemoteNotification. NSDate *notificationDate = [NSDate date] should work.

Nutdesigns
  • 33
  • 5
  • 4
    That method is only called when the app is opened by tapping on the remote notification, not when it's actually received. – Fred Faust May 18 '16 at 14:28
0

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 –
drunknbass
  • 1,662
  • 1
  • 13
  • 19