Using onEdit trigger on Google Spreadsheets to send Firebase Cloud Messaging notification to Android apps
Normally apps use database and a backend to operate. Here I'd like to share another way of using Google Sheets as a database to replace SQL, Mongo and the likes. It is not very customizable or extendable but it is a very good way to prototype or for small scale services or internal tools. Whenever a change happens on the sheets, the app gets a notification so it can re-poll the sheets to get new data.
You need to go and register this script to the OnEdit trigger in your project dashboard. For more details on how to create Triggers on Google Sheets, check the documentation here.
sendFcm is a custom function that sends a payload to FCM. The GAS script to send payload to FCM is as follows.
A few points worth mentioning.
Using Google Sheets as a Database |
Overview
What is needed for this to work.
- Google account, Google Sheets file.
- Created Firebase project.
- Created android app and registered to use FCM.
- The app has access rights to the Google Sheets to read its contents. Reading a Google Sheets file has been covered many times before so I will just refer you to one of them here.
Flow of the application
Someone in charge of the data will make changes to the Google Sheets file and finally when everything is done, he/she will modify a single cell that acts as the "Apply button". For this example let's assume the cell F2 highlighted in the image is the Apply Button.
After you make changes, maybe add Meg Ryan to the list of cast, you just need to change the value of F2 into something else. It doesn't matter what as long as it changes.
The GAS (Google Apps Script) script to detect changes in a Sheets file is as follows. When a change happens on cell F2, sendFcm is fired.
function edited(e){ var range = e.range; var cellAddress = e.range.getA1Notation(); if (cellAddress === 'F2') { range.setNote('Last modified: ' + new Date()); sendFcm(); }; }
You need to go and register this script to the OnEdit trigger in your project dashboard. For more details on how to create Triggers on Google Sheets, check the documentation here.
sendFcm is a custom function that sends a payload to FCM. The GAS script to send payload to FCM is as follows.
function sendFcm(){ text = { "to":"/topics/all", "data": { "title":"Sheets Settings Modified", "message":"recheck" } } var response = UrlFetchApp.fetch('https://fcm.googleapis.com/fcm/send', { method: 'POST', contentType: 'application/json', headers: { Authorization: 'key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' }, payload: JSON.stringify(text), muteHttpExceptions: false }); console.log(response); }
A few points worth mentioning.
- /topics/all refers to the message group that the app needs to subscribe to.
- We need to subscribe the app to the appropriate topic. In kotlin this can be done with : subscribeToTopic("topicName")
- We are posting to the /send API provided by FCM
- Authorization key is the key provided when registering FCM to your project. For more information on authorizing the request please check here.
The app will receive a notification from FCM and you can then get the app to access sheets again to retrieve new data.
Drawbacks
There are several drawbacks to this method
- If you have several apps that need to reference the sheets file, they will all get the notification at around the same time. This may cause the Google Sheets file to receive many requests at the same time.
- The input validation has to be done completely on the app. It is very hard to predict invalid input when there is no limitation whatsoever on the database side. You may expect string or a number for a certain field but can never know what may come.
- Authorization may be a problem because each app is authorized to access directly to the sheets database. For more sensitive information, a good backend to filter the requests may be a better idea.
Conclusion
Google Sheets can be a quick and dirty way to setup a database without backend and it works pretty well too. But make sure to build a proper backend and database as soon as possible!
Update
Since v1 of the HTTP API, we need to get OAuth2 tokens to access FCM. I will update the contents in a short while but please take note that this method will only work with the legacy API.
Comments
Post a Comment