0

I built a shopping app using React Native. We have a lot of stores and restaurantes from all over the country registered on our app.

We send messages to our users using FCM when a specific store publishes a new offer, based on user interest and the publisher. If a restaurant posted an offer, we will send to all users registered on the topic "/restaurants". This behavior is bad because maybe the restaurant is too far away from the user, and this offer isn't important to him.

That's why I want to create topics based on a location, so if a restaurant from Chicago posts an offer, I would send just to users registered on "/restaurants/chicago".

Is this the best way to do it? Do firebase offer something similar out-of-box? And how would I keep track of users to know when they go from another city from another?

Gabriel Bessa
  • 468
  • 1
  • 4
  • 8
  • 1
    That's a rather broad topic. First off, you'll have to store the location of each restaurant in your database. Then you'll have to [know the user's location](https://www.google.com/search?q=site:stackoverflow.com+react+native+access+user+location). And finally, you'll have to [query for restaurants close to the user](https://stackoverflow.com/questions/43357990/query-for-nearby-locations). – Frank van Puffelen May 10 '18 at 14:59

1 Answers1

1

TL;DR: Text in Bold.

It is possible to create a background service that will (once an hour) get the device location. https://developer.android.com/about/versions/oreo/background-location-limits

Best way to get user GPS location in background in Android

Don't add location background service if you can get away with not having one. It drains the battery.


You should decouple the location from the FCM and use a little bit of prediction. Send a notification when they would start planning to visit the restaurant and resolve their location/group when they open your app.


I assume your app has location permissions enabled. What you should do for your user is store a list of their past locations in the Firebase Database (or the Cloud FireStore). Resolve these locations to a metropolitan area, zipcode, etc. Keep a counter to help weight this location (and to sort)

You should also store a list of restaurants they viewed from a map view...specifically their locations. What you are going to do is compute an approximate distance from a central location and create an average travel time.

E.g. I prefer to eat in a sub metropolitan area that is (on average) 30 minutes from where I live. I also like to eat around 6pm. I need to make a decision about where I want to eat around 5:15pm.

You should also keep track of what times they viewed your app for restaurants in the database To figure out when they start making decisions to eat.


Another concern is the day of the week. For instance, because I live 30 minutes from good places to eat, I will sometimes eat locally. These local places will be packed on certain days of the week.. Thursday they will be packed, Friday will be 1/2 of that, and Saturday is next to nothing. Then Sunday/Monday they pick back up to Friday levels. This is because people are more willing to drive to town on Friday/Saturday night, but they don't want to cook and will eat out on Thursday locally.


Now is when you start using Predictions. Firebase has a Predictions module, and/or you can write Cloud Functions to organize your database and schedule FCM.

First, you need to collect data. One method is to kick off a location service from an hour when they open your app. Hopefully, this will catch your user at which location they chose to eat at (we'll call this the Label, for analytics this will probably be a conversion event). The other trick is to kick off a notification in an hour to rate their dining experience. This doesn't require location background service, and if they open the notification, you can grab their location then.

Having in-App Coupons that the user can present at the restaurant can allow you to grab location.

Also, learn which days of the week they prefer to eat out and which areas they target on those specific days.

If the user is in a different metropolitan area, then waiting until they open the app will allow you to display relevant subscriptions. I flew to Seattle and was starving at 4pm.

Given as many data points (features) as possible (day of week, time of day, day of month, day of 2 weeks, average distance to local restaurants, average distance to metro-restaurants, is_holiday, average_notification_open, average_time_at_restaurant) And data labels (Eat_locally, eat_metro, Eat_home) you should be able to start classifying users into different groups.

So, 1 hour before your user's average eating time, you should perform some server side calculations to determine where your user is likely to eat. For example, if they eat_locally on Thursday night, you should schedule FCM according to the average distance of local restaurants.


Never funnel a specific user to a specific restaurant. Comply with GDPR even if you don't operate in the EU (it covers their citizens). Be transparent with your data collection policy and always try to anonymize/randomize.

James Poag
  • 2,320
  • 1
  • 13
  • 20