@quintype/framework 7.33.6-fcm-fix-2.4 → 7.33.6-fcm-fix-2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,8 @@ export function initializeFCM (firebaseConfig) {
3
3
  import(/* webpackChunkName: "firebase-app" */ 'firebase/app'),
4
4
  import(/* webpackChunkName: "firebase-messaging" */ 'firebase/messaging')
5
5
  ])
6
- .then(([firebase, m]) => {
7
- console.log('m-----------', m)
6
+ .then(async ([firebase, m]) => {
7
+ console.log('firebaseConfig=====', firebaseConfig)
8
8
  const app = firebase.initializeApp({
9
9
  messagingSenderId: firebaseConfig.messagingSenderId.toString(),
10
10
  projectId: firebaseConfig.projectId,
@@ -14,12 +14,14 @@ export function initializeFCM (firebaseConfig) {
14
14
  appId: firebaseConfig.appId
15
15
  })
16
16
  const messaging = m.getMessaging(app)
17
-
18
- // No need to refresh token https://github.com/firebase/firebase-js-sdk/issues/4132
19
- requestPermission(m, firebaseConfig, messaging)
20
-
17
+ console.log('messaging----', messaging)
18
+ await requestPermission(m, firebaseConfig, messaging)
19
+ return { m, messaging }
20
+ })
21
+ .then(({ m, messaging }) => {
22
+ console.log('abc---------', abc)
21
23
  m.onMessage(messaging, ({ notification }) => {
22
- console.log('========notification', messaging, notification)
24
+ console.log('notificaation-----', notification)
23
25
  new Notification(notification.title, {
24
26
  body: notification.body,
25
27
  icon: notification.icon
@@ -27,25 +29,30 @@ export function initializeFCM (firebaseConfig) {
27
29
  })
28
30
  })
29
31
  .catch(err => {
30
- console.log('fcm initialization error---------', err)
31
- console.error(err)
32
+ console.error(`FCM subscription failed ${err}`)
32
33
  })
33
34
  }
34
35
 
35
36
  async function requestPermission (m, firebaseConfig, messaging) {
36
- console.log('request oermission------')
37
- //requesting permission using Notification API
38
37
  const permission = await Notification.requestPermission()
39
-
38
+ console.log('permission------', permission)
40
39
  if (permission === 'granted') {
41
- const token = await m.getToken(messaging, {
40
+ const token = m.getToken(messaging, {
42
41
  vapidKey: firebaseConfig.vapidKey
43
42
  })
44
-
45
- //We can send token to server
46
- console.log('Token generated : ', token)
43
+ return registerFCMTopic(token)
47
44
  } else if (permission === 'denied') {
48
- //notifications are blocked
49
- alert('You denied for the notification')
45
+ console.log('notifications are denied')
46
+ return
50
47
  }
51
48
  }
49
+
50
+ function registerFCMTopic (token) {
51
+ return fetch('/register-fcm-topic', {
52
+ method: 'post',
53
+ headers: {
54
+ 'Content-Type': 'application/json'
55
+ },
56
+ body: JSON.stringify({ token: token })
57
+ })
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quintype/framework",
3
- "version": "7.33.6-fcm-fix-2.4",
3
+ "version": "7.33.6-fcm-fix-2.5",
4
4
  "description": "Libraries to help build Quintype Node.js apps",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -1,38 +1,32 @@
1
- const { get } = require("lodash");
2
- const request = require("request-promise");
1
+ const { get } = require('lodash')
2
+ const request = require('request-promise')
3
+ const { initializeApp } = require('firebase/app')
4
+ const { getMessaging } = require('firebase/messaging')
3
5
 
4
- exports.registerFCMTopic = async function registerFCM(
6
+ exports.registerFCMTopic = async function registerFCM (
5
7
  req,
6
8
  res,
7
9
  next,
8
10
  { config, client, publisherConfig, fcmServerKey }
9
11
  ) {
10
- const token = get(req, ["body", "token"], null);
12
+ console.log('fcm server')
13
+ const token = get(req, ['body', 'token'], null)
11
14
  if (!token) {
12
- res.status(400).send("No Token Found");
13
- return;
15
+ res.status(400).send('No Token Found')
16
+ return
14
17
  }
15
18
 
16
- const serverKey = typeof fcmServerKey === "function" ? await fcmServerKey(config) : fcmServerKey;
17
-
18
- if (!serverKey) {
19
- res.status(500).send("Server Key is not available");
20
- return;
21
- }
22
- const url = `https://iid.googleapis.com/iid/v1/${token}/rel/topics/all`;
23
- try {
24
- await request({
25
- uri: url,
26
- method: "POST",
27
- headers: {
28
- Authorization: `key=${serverKey}`,
29
- "content-type": "application/json",
30
- },
31
- });
32
- res.status(200).send("Registration Done Suceessfuly");
33
- return;
34
- } catch (error) {
35
- res.status(500).send("FCM Subscription Failed");
36
- return;
19
+ const firebaseConfig = {
20
+ apiKey: 'AIzaSyDh-FPToRoj1b_XvL6hWnZ84jlb_AaWG1U',
21
+ authDomain: 'quintypeqa-d3e3f.firebaseapp.com',
22
+ projectId: 'quintypeqa-d3e3f',
23
+ storageBucket: 'quintypeqa-d3e3f.firebasestorage.app',
24
+ messagingSenderId: '919899876354',
25
+ appId: '1:919899876354:web:2ac17705ee7fc8364f4b1b',
26
+ measurementId: 'G-V72DFHSJVX'
37
27
  }
38
- };
28
+ const app = initializeApp(firebaseConfig)
29
+ console.log('initialze app')
30
+ const messaging = getMessaging(app)
31
+ console.log('firebaseMessaging==========', messaging)
32
+ }