Question

I am writing an application that utilizes Apple's Push Notification Service. Some of the push notifications are based on the users location and should only be delivered if the user is a certain distance from an object. I don't want to continually update the user's position to my server and do the check that way, first, because of security reasons and second, to cut down on the network usage. Is there a way, when the push notification is received by the device, to do a check before the user is notified, and if it doesn't meet the criteria, discard the notification? Thanks for the help!

Was it helpful?

Solution

Nope, sadly you can't execute any code on the client side without the user clicking on the notification when the app is not launched. You'll have to do your check server-side to decide wether or not sending a push.

OTHER TIPS

Push notifications that are received while you app is not running (the most typical case) is outside your control. Once they are sent, they will be received and shown to the user (assuming the user has granted permissions)

You do have control over push notifications if they are received while your app is running.

Maybe you could use local notifications (notifications that are generated and received from user's device) instead. You would have full control over when they are generated.

Bear in mind, background processing in apps is disallowed except for 4 things

  1. Location tracking (You can subscribe to trigger code when user changes geolocation)
  2. Alarms
  3. Playing music
  4. Voice over IP

Anything outside these cannot be executed in the background.

Starting with iOS7, your code can be run, I quote, "roughly the same time that the user sees the notification":

Multitasking Enhancements

Apps that use push notifications to notify the user that new content is available can fetch the content in the background. To support this mode, include the UIBackgroundModes key with the remote-notification value in your app’s Info.plist file. You must also implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method in your app delegate.

See more info here:

https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW10

and here:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW57

I haven't tried it yet but I wonder if you send a push notification payload which doesn't have the standard "alert", "badge" values in "apns" but in a custom value, it would still call your code but without displaying the push notification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top