Firebase / Firebase Cloud Messaging (FCM)

Getting Started with Firebase Cloud Messaging

This tutorial will guide you through the basics of Firebase Cloud Messaging (FCM). You'll learn how to set up FCM in your project, how to send your first notification, and how to …

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers sending push notifications to iOS, Android, and web applications using FCM.

1. Introduction

1.1 Goal

This tutorial aims to introduce you to Firebase Cloud Messaging (FCM), a cross-platform messaging solution that lets you reliably deliver messages at no cost. We'll get you up and running by setting up FCM in a project, sending a first notification, and handling these notifications in an application.

1.2 Learning Outcomes

By the end of this tutorial, you'll be able to:

  • Set up FCM in your project
  • Send push notifications via FCM
  • Handle incoming notifications in your application

1.3 Prerequisites

For this tutorial, you should have:

  • A basic understanding of JavaScript and Node.js
  • Firebase project setup

2. Step-by-Step Guide

2.1 Setting up FCM in your Project

Before you can send or receive messages, you need to set up FCM in your project. This involves creating a Firebase project, adding a Firebase configuration file, and initializing Firebase in your app.

2.2 Sending your First Notification

Once you've set up FCM, you can send your first notification. This involves creating a message, setting the target, and sending the message.

2.3 Handling Notifications

When a notification is received, your app needs to handle it. This can involve displaying the notification, updating the UI, or even performing a background task.

3. Code Examples

3.1 Setting up FCM

// Import the firebase module
const firebase = require("firebase");

// Initialize Firebase
var config = {
  apiKey: "<API_KEY>",
  authDomain: "<PROJECT_ID>.firebaseapp.com",
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
  projectId: "<PROJECT_ID>",
  storageBucket: "<BUCKET>.appspot.com",
  messagingSenderId: "<SENDER_ID>",
};

firebase.initializeApp(config);

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

3.2 Sending a Notification

// Define a message
var message = {
  data: {
    score: '850',
    time: '2:45',
  },
  topic: 'general',
};

// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

3.3 Handling a Notification

messaging.onMessage((payload) => {
  console.log('Message received. ', payload);
  // ...
});

4. Summary

In this tutorial, we went over setting up Firebase Cloud Messaging in your project, sending your first notification, and handling incoming notifications. The next steps would be to delve deeper into the capabilities of FCM, such as topic messaging, subscription management, and message targeting.

5. Practice Exercises

5.1 Exercise 1

Create a Firebase project and set up FCM. Send a test notification to a specific topic.

5.2 Exercise 2

Write a function to handle incoming FCM notifications and log the payload to the console.

5.3 Exercise 3

Expand your function from Exercise 2 to display a notification to the user when a message is received.

Solutions

5.1.1 Solution to Exercise 1

The solution to this exercise involves following the setup steps outlined in section 2.1 and the message sending steps in section 3.2.

5.2.1 Solution to Exercise 2

The solution to this exercise involves creating a function that uses the onMessage() method from the Firebase Messaging instance.

messaging.onMessage((payload) => {
  console.log('Message received. ', payload);
});

5.3.1 Solution to Exercise 3

The solution to this exercise involves expanding the function from Exercise 2 to create a notification. The exact implementation will vary depending on your application's UI library or framework.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Image Converter

Convert between different image formats.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Unit Converter

Convert between different measurement units.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help