FCM Cloud messages
Introduction
This library is created for using web push
for firebase cloud messages, without firebase.
Found difficulty to use fcm messages in firebase, from one point of view there is firebase with full API,
but disadvantage, you have to load full firebase API libraries, for very small use case. Developers, usually
like to control, what they are doing. Ant not load unnecessary dependencies. I kept this library as small as possible.
Only for Server side push I use web-push
library, on client use native API. Uncompressed client has 5kb. I not added
compressed version, because developers usually use Webpack
or rollup
to optimise your code.
Installation
Run npm install fcm-messages
Server Setup
For Server need express
installed. Then need for Server.js
following setup. Full Example in /examples/service
; ; ; const vapidKeys = publicKey: '...' privateKey: '...' ; const messenger = ; const expressApp = ; expressApp; //Set your static Directory expressApp; // I provide in memory storage, but there can be any with Promise base set, get, delete interface. const subscriptions = ; // Adding subscription API in to REST expressApp; //Expose public key access via REST. expressApp; //Message content const message = { return JSON }; // Optional in this example, can send push notifications, via REST interface, usually want to set some cron job or similar. expressApp; expressApp;
Client setup
For client need setup Service worker ready for Push messaging, and on client side make it ready for push notifications.
In Service Worker
// Install and activate, probably you will do your own staff as well. self; self; self;
In Client javascript, need to make subscription.
//need configuration in rollup or webpack ; //... const initialiseUI = { //First Check if there is subscription already. ; // by toggling button, you can subscribe unsubscribe event. pushButton; } if 'serviceWorker' in navigator && 'PushManager' in window console; //Registering Service Worker and initialise subscriptionManager. navigatorserviceWorker ; else console; pushButtontextContent = 'Push Not Supported';
API
Client API
/** * Creates set of methods to manage push notifications. * @param * @param {rootURI,uid} options - Params for rootURI and uid (uid is mandatory param) * @return */const subscriptionManager = /** * @method Checking, if user have subscription for notifications * @return * */ { ... } /** * @method Subscribe user for notifications * @return * */ { ... } /** * @method remove user subscription for notifications * @return * */ { ... } /** * @method toggle user subscription for notifications * @return * */ { ... } /** * @method Same like checkSubscripton, but always returning successful Promise * @return * */ { ... } ;
Server API
/** * Creates subscriptionREST API using Express. * @param * In this library included in memory storage. * @return */ /** * @method Set subscription in storage * @return * */ { ... } /** * @method Test if subscription exists in storage * @return * */ { ... } /** * @method remove subscription exists from storage * @return * */ { ... } /** * @method Start REST service * @return {Express Router} * */ { ... } ;
Storage API | Interface
/** * Creates in Memory Storage, with interface. * @return */ /** * @method Set data in storage * @return * * Promise.then should look * { * status:"Error"|"Success", * message:{String}, * data: * } * */ { ... } /** * @method Get data from storage * @return * * Promise.then should look * { * status:"Error"|"Success", * message:{String}, * data: * } * */ async { ... } /** * @optional * @method Remove data from storage * @return * * Promise.then return Boolean * */ async { ... } /** * @method Remove data from storage * @return * * Promise.then should look * { * status:"Error"|"Success", * message:{String}, * } * */ { ... } ;
Push Notifications
/** * Creates Web Push Class. * @param * @optional * @return */ /** * Set new Keys for class. * @param * @optional */ ) ... /** * Set new Keys for class. * @return */ { ... } /** * Sending notification to fcm server * @param subscription * { * "endpoint":"https://fcm.googleapis.com/fcm/send/...", * "expirationTime":..., * "keys":{ * "p256dh":"...", * "auth":"..." * } * } * * Subscription data you getting from Service Worker `pushManager.subscribe` * @param * @return */ { ... }