1.0.14 • Published 10 months ago

@eruka-tech/cnotify-sdk v1.0.14

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

CNotifySDK (web_cnotify_sdk) 🔔

Get started

  1. Install the SDK
$ npm i @eruka-tech/cnotify-sdk
  1. Import the SDK
import CNotifySDK from '@eruka-tech/cnotify-sdk';
  1. Initialize the SDK
const firebaseConfig = {
  apiKey: '<your_api_key>',
  authDomain: '<your_auth_domain>',
  projectId: '<your_project_id>',
  storageBucket: '<your_storage_bucket>',
  messagingSenderId: '<your_messaging_sender_id>',
  appId: '<your_app_id>',
  measurementId: '<your_measurement_id>',
};

// Initialize CNotifySDK from configs
const cnotify = CNotifySDK.getInstance(
  { apiKey: '<cnotify_api_key>' },
  {
    testing: true,
    firebaseConfig: firebaseConfig,
  }
);

// Or initialize CNotifySDK from an existing Firebase App
// const app = initializeApp(firebaseConfig);
// CNotifyconst cnotify = SDK.getInstance(
//   { apiKey: '<cnotify_api_key>' },
//   {
// testing: true,
// firebaseApp: app,
//   }
// );
  1. Request notification permissions

Permissions will be requested automatically when initializing the SDK, but there are some browsers that require permissions to be requested after a user gesture on the screen. To request notification permissions on demand, the SDK provides the following method:

cnotify.requestPermissions().then((permission) => {
  if (permission === 'granted') {
    console.log('Notification permission granted ✅');
  } else {
    console.log('Notification permission denied ❌');
  }
});
  1. It is necessary to create a firebase-messaging-sw.js file in /public (or wherever appropriate), the purpose is to make the /firebase-messaging-sw.js url available. The content of the file is as follows:
// Scripts for firebase and firebase messaging
importScripts('https://www.gstatic.com/firebasejs/8.2.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.2.0/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing the generated config
const firebaseConfig = {
  apiKey: '<your_api_key>',
  authDomain: '<your_auth_domain>',
  projectId: '<your_project_id>',
  storageBucket: '<your_storage_bucket>',
  messagingSenderId: '<your_messaging_sender_id>',
  appId: '<your_app_id>',
  measurementId: '<your_measurement_id>',
};

firebase.initializeApp(firebaseConfig);

// Retrieve firebase messaging
const messaging = firebase.messaging();

messaging.onBackgroundMessage(function (payload) {
  console.log('Received background message ', payload);
});

Examples

1.0.11

10 months ago

1.0.10

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.2

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago