You can use the data from a single Firebase application for multiple Push.Express applications. To do this, use the same firebaseConfig, Web Push certificate, and private-key.json for multiple Push.Express applications.
Go to Firebase Console and create a new project (or use existing one). You can use one project for all your apps.
Open Project Settings.
Go to General, copy firebaseConfig.
Go to Cloud Messaging, Web configuration, press Generate key pair and save key.
Go to Service accounts, press Generate new private key and save it to file private-key.json (you can use same key for all apps).
Creating an application in Push Express
Create new application.
Choose type of application Web Push.
Fill in the required Application name field *
Paste private-key.json file to Firebase Admin SDK private key textbox.
After creating the application, push_express_app_id will be available.
Add Push Express web sdk in your code
Add firebaseConfig, vapidKey, push_express_app_id in your project (where to get this data can be viewed here).
The url for the redirect after initializing the sdk. Leave it empty if a redirect is not requirednd the unique installation id here.
constoffer="";
The segment code. Use it to add a device to a segment. Segments can be created in the segments section of your personal account.
constwebmaster="";
To transfer devices to other users of the service, use the functionality of webmasters. You can add a webmaster in the personal section of your personal account.
If a redirect_url value is specified, a redirect will be performed after initialization.
[!IMPORTANT] Do not connect third-party Firebase libraries. The Push Express SDK automatically connects the firebase-messaging libraries and generates the FCM token. It takes 3 seconds to install service Worker and transfer all the necessary death to the server. Wait until the sdk is working and only then redirect the user to the landing page. Or use the const redirect_url parameter to automatically redirect the user after initializing the sdk.
Example code
<!DOCTYPE html><htmllang="en"> <head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width, initial-scale=1.0"> <linkrel="stylesheet"href="src/style.css"> </head> <body> <h1id="header"></h1> </body></html><script> function generateUUID() { // Public Domain/MIT var d =newDate().getTime();//Timestamp var d2 = ((typeof performance !== 'undefined') && performance.now && (performance.now() * 1000)) || 0;//Time in microseconds since page-load or 0 if unsupported
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function (c) {var r =Math.random() *16;//random number between 0 and 16if (d >0) {//Use timestamp until depleted r = (d + r) %16|0; d =Math.floor(d /16); } else {//Use microseconds since page-load if supported r = (d2 + r) %16|0; d2 =Math.floor(d2 /16); }return (c ==='x'? r : (r &0x3|0x8)).toString(16); }); } const firebaseConfig = { apiKey: "AIzaSyCwe-3H4-bcBRVMVUDh9OVN-JUC4AaoqJA", authDomain: "web-push-4e988.firebaseapp.com", projectId: "web-push-4e988", storageBucket: "web-push-4e988.appspot.com", messagingSenderId: "547479027002", appId: "1:547479027002:web:e71af6f41f1f753594e403", measurementId: "G-MFXVZH805X" }; const vapidKey = "BEOEqKl-ClK2syw76a6EX7Ee-sqIanSuGdSezgEErASzoqGZDgd4ZNHhZt485WcOUFYYgnUZxGWdUC50Sa6CAa0"; const push_express_app_id = "20605-2"; const external_id = generateUUID(); const sw_path = "" </script> <scriptsrc="https://sdk.push.express/js/v1.2.1/push-express-sdk.js"></script> <script>initPushExpressSdk();</script>
Create service worker file in your project. The file name must be as follows "push-express-sw.js".
Add firebaseConfig and push_express_app_id in push-express-sw.js