Thursday, June 22, 2023

FirebaseApp to Connect Google Apps Script with Firebase Realtime Database

FirebaseApp is a GitHub project by Romain Vialard that provides a Google Apps Script binding for the Firebase Realtime Database. This means that you can use Apps Script to interact with Firebase Realtime Database from within Google Sheets, Docs, Slides, and other Google Apps.

The FirebaseApp project is still under development, but it already includes a number of useful features, including:

  • The ability to create, read, update, and delete Firebase Realtime Database records.
  • The ability to listen for changes to Firebase Realtime Database records.
  • The ability to send push notifications to Firebase Realtime Database users.

The FirebaseApp project is a valuable resource for anyone who wants to use Firebase Realtime Database from within Google Apps Script. It is well-documented and easy to use, and it is constantly being updated with new features.

Here are some of the benefits of using FirebaseApp:

  • It allows you to use Firebase Realtime Database from within Google Apps, which can save you time and effort.
  • It is a well-documented and easy-to-use project.
  • It is constantly being updated with new features.

If you are interested in using Firebase Realtime Database from within Google Apps Script, I encourage you to check out the FirebaseApp project. It is a valuable resource that can help you save time and effort.


Install:

Best it to copy the content of this file in your Google Apps Script. 
Get project filehttps://github.com/RomainVialard/FirebaseApp/blob/master/src/Code.gs


You can also add it as a library, though this is not recommended.
Check here to add library https://developers.google.com/apps-script/guides/libraries 
Use this Library's script ID to add: 1VlYLzhwx0YEoxIe62eItLAZeobVt_l-GQUKt2MXXuBHFVsqBkl9C_yBB

 

Use: 

Need to use it your Firebase project Database URL (looks like  https://{your-project}.firebaseio.com/) & App Secret key.  

// Get the FirebaseApp object.

var firebaseApp = FirebaseApp.getDatabaseByUrl("https://my-project.firebaseio.com/"); 


An example :


function writeDataToFirebase() {
  // Get the FirebaseApp object.
  var firebaseApp = FirebaseApp.getDatabaseByUrl("https://my-project.firebaseio.com/");

  // Create a new record in the Firebase Realtime Database.
  var record = firebaseApp.database().ref("/my-record").set({
    name: "John Doe",
    age: 30,
  });

  // Listen for changes to the Firebase Realtime Database record.
  var listener = firebaseApp.database().ref("/my-record").on("value", function(snapshot) {
    // Do something with the updated record.
  });
} 

This code first gets the FirebaseApp object by specifying the URL of the Firebase Realtime Database. Then, it creates a new record in the Firebase Realtime Database and sets the name and age of the record. Finally, it listens for changes to the Firebase Realtime Database record.

To run this code, you would need to save it as a Google Apps Script project and then run it from within a Google Spreadsheet. For more information on how to do this, please see the Google Apps Script documentation: https://developers.google.com/apps-script/.

Here is another example code that shows how to use FirebaseApp to send push notifications to Firebase Realtime Database users:

function sendPushNotification() {
  // Get the FirebaseApp object.
  var firebaseApp = FirebaseApp.getDatabaseByUrl("https://my-project.firebaseio.com/");

  // Get the list of users who are subscribed to push notifications.
  var users = firebaseApp.database().ref("/users").list();

  // Send a push notification to all users.
  users.forEach(function(user) {
    var notification = firebaseApp.notifications().create(user.key, {
      title: "New notification",
      body: "This is a new notification.",
    });
    notification.send();
  });
}
This code first gets the FirebaseApp object and then gets the list of users who are subscribed to push notifications. Then, it sends a push notification to all users. 


Here are some additional resources that you may find helpful: