Search code examples
firebasebeaconeddystonegoogle-nearby

Enabling Google Nearby Messages API offline?


I'm receiving messages triggered by Eddystone beacons using the Nearby Messages API in my iOS app and it works well. I'm using Firebase's realtime database for the app's backend. Since Firebase's caching is pretty awesome, I'm able to get the app working really well offline.

My question is whether anyone knows/uses a technique (perhaps combined with Firebase) to cache the attachments associated with beacons in advance (e.g. cache all Eddystone attachments in a particular namespace) so that I can effectively get beacon attachments when the app has no connection to the internet.

Any advice or suggestions of areas to explore would be great.


Solution

  • One option is to use the Proximity Beacon API's beaconinfo:getforobserved method to proactively fetch the attachments for all of the beacons your app knows about. You can see documentation for that here.

    The app could make this call periodically (e.g. at startup or at other times when it has an internet connection) to get all attachment information for each known beacon. The response to the method call will return an array of BeaconInfo that has the attachment data. The application can then store this in Firebase or some other local data store keyed by the Eddystone namespace and instance id.

    Obviously, the app will need to know all beacon identifiers up-front for this to work. The web service call can be made from an an iOS app using an API_KEY.

    Once you have this information in cache, you still need a way to trigger the app to send messages to the user based on beacon detection. To my knowledge, the Nearby APIs for iOS don't do this offline when the attachment information is not available. You can still make this work by using an iOS Eddystone beacon scanning toolkit like the one I put together here. With this, you can do something like this:

    self.beaconScanner = [RNLBeaconScanner sharedBeaconScanner];
    [self.beaconScanner startScanning];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRangeBeacons:) name:@"didRangeBeacons" object:nil];    
    
    ...
    
    - (void) didRangeBeacons: (NSNotification *) notification {
        NSArray *beacons = notification.userInfo[@"beacons"];
        for (RNLBeacon *beacon in beacons) {
          NSString *eddystoneNamespace = beacon.id1;
          NSString *eddystoneInstance = beacon.id2;
          // TODO: look up cached attachments for this beacon in data store
        }
      }