https://github.com/techins-software/ezfirebase
A library that allows you to bootstrap firebase FMC message and display them as browser notifications.
https://github.com/techins-software/ezfirebase
Last synced: 11 days ago
JSON representation
A library that allows you to bootstrap firebase FMC message and display them as browser notifications.
- Host: GitHub
- URL: https://github.com/techins-software/ezfirebase
- Owner: TechIns-Software
- License: other
- Created: 2024-05-16T15:04:38.000Z (about 2 years ago)
- Default Branch: dev
- Last Pushed: 2024-06-10T12:46:25.000Z (about 2 years ago)
- Last Synced: 2025-03-11T12:51:56.216Z (over 1 year ago)
- Language: JavaScript
- Size: 514 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.MD
Awesome Lists containing this project
README
# Ez push notification JS lib
An api wrapper around firebase aiming to offer a trouble-free usage of firebase upon legacy es-5 projects.
# Usage via CDN
## Step 1: Load Js Scripot through CDN
```
```
## Step 2: Create `firebase-messaging-sw.js`
### Ensure that `firebase-messaging-sw.js` is servesd through `/` and via https.
In order for the firebase to work you must create a file `firebase-messaging-sw.js`. The file should be accessible via **https** at:
```
https://^your_domain^/firebase-messaging-sw.js
```
Keep in mind that `firebase-messaging-sw.js` **does not** work if not served in http as mentioned above.
## `firebase-messaging-sw.js` content
Its content should be:
```
importScripts("https://cdn.jsdelivr.net/npm/@techins/pushnotiflib@latest/dist/firebase-messaging-sw.js");
const firebaseConfig = {
// Replace firebase config from firebase console
};
self.initServiceWorker(firebaseConfig);
```
The `firebaseConfig` is the config generated from the firebase console.
## Step 3 Use the library:
The library can be used like this:
```
const firebaseConfig = {
// Firebase config
};
const vapidKey = ""; // Also generated from firebase console
// Initialize Firebase
firebaseLib.pushNotificationInit(firebaseConfig,vapidKey)
```
Both `firebaseConfig` and `vapidKey` are retrieved from firebase console.
# Further token reiceival handling:
The function `pushNotificationInit` recieves the folloing arguments (with rthe order provided):
* `firebaseConfig` that is the configuration received from firebase console
* `vapidKey` that also received via firebase console
* `tokenNotificationCallback` As documented bellow (optional)
* `workerAsModule` that indicates whether worker should be loaded as a module or not.
Upon token receival you can offer to be processed futher via providing a callback function at `tokenNotificationCallback`.
The function provided should be like this:
```
function(token,proceedCallback)
{
// Send token via ajax or do stuff
}
```
In order to receive messages you must call `proceedCallback` function. In order to indicate that the process sucessfully has been ended you should do:
```
function(token,proceedCallback)
{
// Send token via ajax or do stuff
proceedCallback() // Success
}
```
Otherwise you should provide the error as an argument:
```
function(token,proceedCallback)
{
// Send token via ajax or do stuff
proceedCallback() // Success
}
```
A full working example is:
```
const firebaseConfig = {
// Firebase config
};
const vapidKey = ""; // Also generated from firebase console
function handleToken(token,proceedCallback)
{
// Send token via ajax or do stuff
proceedCallback()
}
// Initialize Firebase
firebaseLib.pushNotificationInit(firebaseConfig,vapidKey,handleToken)
```