Drupal Firebase

admin

Just for people who installed firebase via npm install -g firebase-tools typing which firebase and rm as in the first answer will not uninstall all components. The strength of Drupal is the community. If each of us gives at least a small contribution to the community, then we all help each other and ourselves. Adrian Cid Almaguer, Senior Drupal Developer at Drupiter and module maintainer. WS is a full-service Drupal agency providing end-to-end services including consultancy, visual and design support, hosting, SEO, SMM with 24x7 support.Partnering with Drupal 8 has turbocharged our result-focused expertise to help our ambitious clients, ISVs and business agencies deliver secure and scalable Drupal solutions. Firebase Authentication user login Access the permission page for each one, then enable it for anonymous users. How to test.Use the API Client of your preference to test the REST endpoints. To test the authentication provider, you need to configure any REST endpoint, with the firebaseauthentication as the authentication provider. Browse other questions tagged firebase drupal push-notification firebase-cloud-messaging or ask your own question. The Overflow Blog I followed my dreams and got demoted to software developer.

If you’re into JAMstack, you’d know that robust APIs are extremely important for a better developer experience!

Table of Contents

Introduction

For beginners and for small scale applications, building an API backend and deploying it on the web can be a tedious task sometimes. If you choose to deploy it in a Virtual Machine in any of the cloud services like Google Cloud, Azure, or AWS, it can be daunting to set up.

One more downside to this is that since a virtual machine is an Infrastructure as a Service (IaaS), you’d be charged by the hour. You would essentially be charged for the time the VM is running, regardless of whether you’re making API calls or not.🙁

Enter, Serverless Functions…

No, it’s not like you think. Serverless functions do run on a server. But they’re called so because it eliminates the need for managing the software and hardware of the server. It is as simple as copying and pasting code and hitting deploy.

Sounds amazing, right? Let’s build a basic serverless API and deploy it through the firebase CLI.🔥

Prerequisites and Installation

Let’s get started. First, please install Node from here.

  • Now, once you’re done installing, let’s initialize our project.
  • Create a new directory for your project.
  • Open up a terminal, cd to the project directory, and type npm init
  • For now, accept the default options and let the project be initialized.
  • Install firebase by typing, npm install -g firebase-tools
  • Awesome!

Login and Initialize

Login and Validate

  • Login to Firebase by entering this command, firebase login
  • It will open a window in the browser. You can log in there.
  • Return to the terminal once you’ve logged in.

Initialize

Let’s initialize a project in firebase.

  • Type firebase init
  • When it asks for what CLI features are needed, just choose Firebase Functions for now.
  • It will ask to connect to a project, create a new one, and drop in a name of your choice.
  • Choose JavaScript as the language for functions.
  • Choose to not use ESLint, as we don’t really need it now.
  • Now, our firebase project is ready!

Write the API

Now, let’s jump into the programming part.

In your project directory, the functions/index.js file is the entry point of your function. That’s the file that will always be executed. We have to edit only this file. So let’s start editing!

A brief explanation of this code

  • Lines 1-4 -> We import the needed packages
  • Lines 6-9 -> We initialize express, and set options for CORS handling.
  • Lines 11-14 -> This is our main api route, /hello. If we send a request to /hello, we’ll get a server response that says, “hello!'
  • Lines 16-19 -> This is our main api route, /world. If we send a request to /world, we’ll get a server response that says, “world!'
  • Line 21 -> This line is a Firebase Functions method that says that the function would be triggered if a HTTP Request is sent to the function URL.

Thus, we have a very basic API with two routes. Let’s go on and deploy it!

First though, we have to specify the function dependencies in our package.json. Now proceed to the package.json file within the functions folder, and append the following lines within the dependencies object.

Deploy

Let’s deploy our function! Enter the command, firebase deploy in the terminal.

You have successfully deployed the API! Yay! Now get your API URL from the Firebase Console.

Test, test, test

Now that we have deployed our API, we also need to test whether it behaves properly. For this, we can use the amazing API testing tool Postman, which is a GUI tool for API testing, or CURL, which is a CLI utility for sending HTTP requests.

Install and open Postman, and with the request type as GET, enter the URL of your function, and try sending a request to “/world”.

You’d get a response that looks something like this:

The API works really well. You could also try to send a request to “/hello” and check it out.

Note: There’s also a way by which you can deploy your API locally and test it before deploying it to the web by running the command, firebase emulators:start. Check these docs for more info -> Link

Conclusion

We have successfully written and deployed a super simple API onto Firebase! We could also make use of Firebase’s Firestore and other features and integrate them into functions. We can create powerful APIs and deploy them with the command or a click of a button!

About the author

Saiharsha Balasubramaniam

Saiharsha Balasubramaniam is a senior undergrad, majoring in Computer Science at Amrita Vishwa Vidyapeetham University, India. He is also a passionate software developer and an avid researcher. He designs and develops aesthetic websites, and loves blockchain technology. Currently, he is an SDE Intern at Flipkart and a Microsoft Learn Student Ambassador.

The Firebase Cloud Messaging module for DrupalGap.

  • Updated: 2016-11-28

Drupal Core:

  • 7.x

The Firebase Cloud Messaging module for DrupalGap helps users to send cloud messages from their Drupal website to devices.

Installation

Before getting started, make sure you're running the latest versions of:

  • PhoneGap/Cordova
  • Android SDK(s)
  • iOS/xCode

Push Notifications Module for DrupalGap

First, download and extract this module so it lives here:

app/modules/fcm

Add it to the settings.js file:

Best portable phone charger. Portable chargers are available with built-in Lightning ports for newer Apple devices, or you can find some iPhone cases that can accommodate a slim, lightweight power pack that snaps right onto your phone. These streamlined portable charger options make it easy to use your phone as you normally would, even with accessories like a selfie-stick.

Drupal.modules.contrib['fcm'] = {};

Google Firebase Cloud Messaging Cordova Push Plugin

Then install the Google Firebase Cloud Messaging Cordova Push Plugin

https://github.com/fechanique/cordova-plugin-fcm

Setting up a Platform(s)

Next, follow these steps for your desired platform(s):

Android

  1. Go to https://console.cloud.google.com/home/dashboard
  2. Create a new project (or use an existing one)
  3. On the Dashboard, click Enable and manage APIs
  4. Enable the Google Cloud Messaging for Android API
  5. Once enabled, click Go to Credentials
  6. Under Where will you be calling the API from? choose Web server
  7. Click What credentials do I need?
  8. Enter a Name for your web server
  9. Click Create API key
  10. Copy the API key and set it aside

iOS

High level overview

Push Notifications Module for Drupal

  1. Download and enable the Push Notifications module for Drupal: https://www.drupal.org/project/push_notifications
  2. In Drupal, go to admin/config/services/push_notifications/configure
  3. For Android, paste in the API key you generated earlier into this form
  4. Click Save configuration
  5. Go to admin/structure/services/list/drupalgap/resources
  6. Check the box next to push_notifications to enable its create and delete resources
  7. Click Save
  8. Flush all of Drupal's caches

Module Permissions in Drupal

  1. Go to admin/people/permissions in Drupal
  2. Go to the Push Notifications section
  3. Grant permission to Register device token and Remove device token to your desired user role(s), we recommend giving this permission to both anonymous and authenticated users

Next, we'll head back to the Google Cloud Platform API (if you're working with Android)..

Get an Android Sender ID

  1. Go to https://console.cloud.google.com
  2. On the Dashboard, click Enable and manage APIs
  3. Click on the Credentials button in the sidebar menu for your project
  4. Click the Create credentials button
  5. Select API key
  6. Click Android key
  7. Enter a Name for your key, e.g. example.com
  8. Click the + Add package name and fingerprint button
  9. Enter the Package name, which can be found as the value of the package attribute in the manifest element in the AndroidManifest.xml file
  10. Open a terminal window and navigate to the root of your cordova project
  11. Run this command: keytool -genkey -v -keystore example.keystore -alias example -keyalg RSA -keysize 2048 -validity 10000
  12. Follow all the prompts and take note of the password you enter, because you'll need it later
  13. Run this command: keytool -exportcert -alias example -keystore example.keystore -list -v
  14. Copy the SHA1 fingerprint
  15. Go back to the Google window and paste in the SHA1 fingerprint
  16. Click Create, then copy the API key that is shown

Next, get the senderID by..

  1. Go to https://console.developers.google.com/apis/library
  2. Under the drop down menu, click Manage all projects
  3. Click on your project's name
  4. Click on Settings in the left sidebar
  5. Copy the Project number, this will go into your settings.js file as the senderID for Android.

Android Quirks

You may have to run this command if you're having issues building for an Android device:

Drupal Firebase

iOS Quirks

You'll need to be running at least [email protected] to get the build to compile properly for iOS. To see your current version of the iOS platform:

Then if it isn't running at least 4.0.1, then run this command:

Usage

Sending a Push Notification

In Drupal, first go to admin/config/services/push_notifications and verify that a token has been registered for yourdesired device(s). If there is a token registered, then go to admin/config/services/push_notifications/message andfill out the form to send a push notification to your desired platform(s).

Receiving a Push Notification

To handle the receipt of a push notification, implement this hook in your custom DrupalGap module's .js file:

When a push notifications is received, developers can take action as they need. The example above simply shows the push notification in an alert dialog.

Real World Examples

Send a push notification to display a node

In this example, a Drupal admin will be able to send a push notification that when clicked by the end user will display a particular node in the app.

In a custom Drupal module, alter the push notifications form so you can include a node id in the message payload:

Now in a custom DrupalGap module, take action when the push notification is received and send the user to view the node in the app:

Drupal Base Dir

That's it! Now when a user clicks the push notification on their device, it'll open the app and display the node.