Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@firebase/messaging

Package Overview
Dependencies
Maintainers
4
Versions
3308
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@firebase/messaging

This is the Firebase Cloud Messaging component of the Firebase JS SDK.

  • 0.12.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
711K
decreased by-62.48%
Maintainers
4
Weekly downloads
 
Created

What is @firebase/messaging?

The @firebase/messaging package is part of the Firebase JavaScript SDK, providing web and mobile push messaging capabilities. It allows developers to send and receive messages across platforms reliably and at no cost. This package is specifically designed to integrate with Firebase Cloud Messaging (FCM), enabling the delivery of notifications and messages to users across devices.

What are @firebase/messaging's main functionalities?

Receiving messages

This feature allows your web app to receive messages sent from the Firebase Console or your server-side code. The `onMessage` function listens for messages when your web app is in the foreground.

import { getMessaging, onMessage } from '@firebase/messaging';

const messaging = getMessaging();
onMessage(messaging, (payload) => {
  console.log('Message received. ', payload);
  // Handle the received message here.
});

Requesting permission to send notifications

Before sending notifications to the user, your application needs to request permission. This code snippet demonstrates how to request a token, which implicitly asks the user for permission to receive notifications. If permission is granted, you receive a token that you can use to send messages to the user.

import { getMessaging, getToken } from '@firebase/messaging';

const messaging = getMessaging();
getToken(messaging, { vapidKey: 'your-public-vapid-key' }).then((currentToken) => {
  if (currentToken) {
    // Send the token to your server and update the UI if necessary
    // For example, enable a button that lets the user see the notification
    console.log(currentToken);
  } else {
    // Show permission request UI
    console.log('No registration token available. Request permission to generate one.');
  }
}).catch((err) => {
  console.log('An error occurred while retrieving token. ', err);
});

Background message handling

This feature enables the handling of messages when your web app or a web page is not active in the foreground. It's particularly useful for showing notifications that can engage or re-engage your user with your web app.

import { getMessaging, setBackgroundMessageHandler } from '@firebase/messaging/sw';

const messaging = getMessaging();
setBackgroundMessageHandler(messaging, (payload) => {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.',
    icon: '/firebase-logo.png'
  };

  return self.registration.showNotification(notificationTitle,
      notificationOptions);
});

Other packages similar to @firebase/messaging

FAQs

Package last updated on 13 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc