Radius Networks Developer Blog

Follow @radiusnetworks

Proximal Push Notifications

Scott Yoder and Christopher Sexton 13 Oct 2015 Permalink

Push notifications are a great feature on iOS, but they are a pretty blunt instrument. When you send them, they get delivered to recipients no matter where they are. What if you want to send a notification only to people that are in a specific location?

One option is to report their location to the server, and then only notifications to the people in the desired location. But this has privacy implications. It involves tracking users locations all the time, and that’s a little creepy. We were not willing to do that.

For Campaign Kit and came up with a solution that is both reliable, quick, and treats customer’s privacy with respect. This works by having our SDK in an App on the phone. The phone knows where it is and can use that information. The important part is to not report up those details unnecessary.

How do we do this? The solution we follow is:

  1. Send a silent Push Notification
  2. Check Recent Places
  3. Sync with the Server
  4. Range for Beacons
  5. Show Notification

Lets go over those steps in more detail.

1. Send a silent Push Notification

proximal-push-01

On iOS, Apple supports a “Silent” push notification, where the app will receive a callback in the background. You just have to implement the didReceiveRemoteNotification in your app delegate and set the remote-notification Background Mode in the plist.

- (void)         application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
      fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
{
  // Handle the callback
}

2. Check Recent Places

proximal-push-02

This is important to not overwhelm the back end and to have a low impact on devices not in proximity to a beacon. Check the internal list of places and make sure that the phone has been near one recently, perhaps within the last hour.

If we find that there was a recent and relevant location we will continue; otherwise, stop here.

3. Sync with the Server

proximal-push-03

Now that we know this phone is likely to be in proximity to a beacon we want to sync with the server. This will fetch any content as well as a list of beacons that will trigger the notification.

4. Range for Beacons

proximal-push-04

Now that we have updated data, range for a few seconds. If there is a beacon within range, you have found a match. The method we use for doing that looks something like this:

- (void)handleRemoteNotification:(NSDictionary *)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  NSLog(@"CampaignKit: handling remote notification.");
  if ([proximityKitDelegate placeDetectedRecently]) {
    // we've seen beacons/geofences recently, so we better sync and check campaigns
    [self syncWithCompletionHandler:completionHandler];

    // range for 5 seconds in the background
    __block UIBackgroundTaskIdentifier backgroundTask = \
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:
      ^{
        // if iOS tells us our task has expired, let's quit before we are killed
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
        backgroundTask = UIBackgroundTaskInvalid;
      }
    ];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, FIVE_SECONDS)), dispatch_get_main_queue(),
      ^{
        if (backgroundTask != UIBackgroundTaskInvalid) {
          [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
          backgroundTask = UIBackgroundTaskInvalid;
        }
      }
    );
  } else {
    // haven't seen any places recently, so let's assume this push isn't for us
    completionHandler(UIBackgroundFetchResultNoData);
  }
}

5. Show Notification

proximal-push-05

The fun part: notify the user. Send the local notification, display the content, whatever your app needs to do to inform the end-user. And only the people who would care about that information.


Campaign Kit: Engagement where it counts

Want this functionality in your app? Check out Campaign Kit, and we take care of these details for you. It’s an SDK that you can drop in to take advantage of beacon and geofence capabilities to deliver targeted campaigns to mobile devices within proximity of any place. Complete with simple content editor, campaign analytics, smart caching, and more.