npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

capacitor-background-fcm

v0.0.8

Published

Background FCM handling

Downloads

20

Readme

capacitor-background-fcm npm version

Capacitor plugin to enable features from Firebase Cloud Messaging

Notice

This plugin is intended to be used together with the capacitor api for Push Notifications and FCM Notifications.

API

  • setAdditionalData
  • addListener('pushNotificationActionPerformed')

Usage


import { Plugins } from "@capacitor/core";
const { PushNotifications, BackgroundFCM } = Plugins;

//
// Add additional data which will be available in your BackgroundFCMHandler
const value = {
    translations: this.translateService.translations[this.translateService.currentLang]
};
BackgroundFCM.setAdditionalData({value: JSON.stringify(value)});

// Need to listen for click action on the notification
BackgroundFCM.addListener(
    'pushNotificationActionPerformed',
    (notification: PushNotificationActionPerformed) => {
        console.log('Tapped', notification);
    }
);

iOS setup

Currently not implemented

Android setup

  • npm install --save capacitor-background-fcm
  • npx cap sync android
  • npx cap open android
  • add google-services.json to your android/app folder
  • [extra step] in android case we need to tell Capacitor to initialise the plugin:

on your MainActivity.java file add import com.digaus.capbackgroundfcm.BackgroundFCM; and then inside the init callback add(BackgroundFCM.class);

Now you can implement the BackgroundFCMHandlerInterface where you can handle all notifications which only have a data payload. The file must be named BackgroundFCMHandler.java and located next to the MainActivity.java

package com.home.shelly;

import android.content.Context;
import android.util.Log;

import com.digaus.capbackgroundfcm.BackgroundFCMData;
import com.digaus.capbackgroundfcm.BackgroundFCMRemoteMessage;
import com.digaus.capbackgroundfcm.BackgroundHandlerInterface;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class BackgroundFCMHandler implements BackgroundHandlerInterface {

    private static String TAG = "BackgroundFCMHandler";
    private Context context;
    private JSONObject additionalData;

    public void setContext(Context context){
        this.context = context;
    }
    public void setAdditionalData(JSONObject additionalData){
        this.additionalData = additionalData;
    }

    public BackgroundFCMData handleNotification(BackgroundFCMRemoteMessage remoteMessage) {
       if (remoteMessage.getData() != null && remoteMessage.getData().getString("type") != null) {
           if (remoteMessage.getData().getString("type").equals("device-update2")) {
               return this.handleDeviceUpdate(remoteMessage, this.additionalData);
           } else if (remoteMessage.getData().getString("type").equals("app-update2")) {
               return this.handleAppUpdate(remoteMessage, this.additionalData);
           }
       }
       return null;
    }

    // show a translated message based on the translation which we passed with setAdditionalData
    private BackgroundFCMData handleAppUpdate(BackgroundFCMRemoteMessage remoteMessage, JSONObject obj) {
        try {
            JSONObject translations = obj.getJSONObject("translations");
            String title = translations.getString("app.shelly-home.app-update.update.label");
            String body = translations.getString("app.shelly-home.app-update.update-installed.label");
            return new BackgroundFCMData(title, body);
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
        return null;
    }

    // show a translated message based on the translation which we passed with setAdditionalData and some information about devices
    private BackgroundFCMData handleDeviceUpdate(BackgroundFCMRemoteMessage remoteMessage, JSONObject obj) {
        try {
            JSONObject translations = obj.getJSONObject("translations");
            List<String> filteredDevices = this.checkFirmware(obj);
            if (filteredDevices.size() > 0) {
                String title = translations.getString("app.shelly-home.device-update.update.label");
                String body = translations.getString("app.shelly-home.device-update.available.label").replace("{{count}}", filteredDevices.size() + "");
                return new BackgroundFCMData(title, body);
            }
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
        return null;
    }

    private List<String> checkFirmware(JSONObject obj) {
        // Do some logic to check firmware of devices
        return filteredDevices;
    }

    // example http request to fetch some information for the notification
    private JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException {
        HttpURLConnection urlConnection;
        URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(5000 /* milliseconds */ );
        urlConnection.setConnectTimeout(5000 /* milliseconds */ );
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();

        String jsonString = sb.toString();
        System.out.println("JSON: " + jsonString);

        return new JSONObject(jsonString);
    }
}

Tip: every time you change a native code you may need to clean up the cache (Build > Clean Project | Build > Rebuild Project) and then run the app again.

License

MIT