@quintype/framework 7.33.5 → 7.33.6-fcm-fix.1
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.
- package/client/impl/fcm.js +65 -21
- package/client/start.js +1 -1
- package/package.json +1 -1
- package/server/routes.js +0 -5
- package/server/handlers/fcm-registration-handler.js +0 -38
package/client/impl/fcm.js
CHANGED
|
@@ -1,35 +1,79 @@
|
|
|
1
|
-
export function initializeFCM(firebaseConfig) {
|
|
1
|
+
export function initializeFCM (firebaseConfig, config) {
|
|
2
|
+
console.log('opts--------', firebaseConfig)
|
|
2
3
|
Promise.all([
|
|
3
|
-
import(/* webpackChunkName: "firebase-app" */
|
|
4
|
-
import(/* webpackChunkName: "firebase-messaging" */
|
|
4
|
+
import(/* webpackChunkName: "firebase-app" */ 'firebase/app'),
|
|
5
|
+
import(/* webpackChunkName: "firebase-messaging" */ 'firebase/messaging'),
|
|
6
|
+
import(/* webpackChunkName: "firebase-auth" */ 'firebase/auth')
|
|
5
7
|
])
|
|
6
|
-
.then(([firebase, m]) => {
|
|
8
|
+
.then(([firebase, m, a]) => {
|
|
7
9
|
const app = firebase.initializeApp({
|
|
8
10
|
messagingSenderId: firebaseConfig.messagingSenderId.toString(),
|
|
9
11
|
projectId: firebaseConfig.projectId,
|
|
10
12
|
apiKey: firebaseConfig.apiKey,
|
|
11
13
|
storageBucket: firebaseConfig.storageBucket,
|
|
12
14
|
authDomain: firebaseConfig.authDomain,
|
|
13
|
-
appId: firebaseConfig.appId
|
|
14
|
-
})
|
|
15
|
-
const
|
|
16
|
-
|
|
15
|
+
appId: firebaseConfig.appId
|
|
16
|
+
})
|
|
17
|
+
const token = getToken(app, m, firebaseConfig?.vapidKey)
|
|
18
|
+
console.log("final token----", token);
|
|
19
|
+
const oauthToken = getOAuthToken(a)
|
|
20
|
+
console.log("final oauthToken----", oauthToken);
|
|
21
|
+
return { token: token, oauthToken: oauthToken }
|
|
17
22
|
// No need to refresh token https://github.com/firebase/firebase-js-sdk/issues/4132
|
|
18
23
|
})
|
|
19
|
-
.then((token) => {
|
|
20
|
-
return registerFCMTopic(token)
|
|
24
|
+
.then(({token, oauthToken}) => {
|
|
25
|
+
return registerFCMTopic(config, firebaseConfig?.serverKey, token, oauthToken)
|
|
21
26
|
})
|
|
22
|
-
.catch(
|
|
23
|
-
console.error(err)
|
|
24
|
-
})
|
|
27
|
+
.catch(err => {
|
|
28
|
+
console.error(err)
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function getToken (app, m, vapidKey) {
|
|
33
|
+
const messaging = m.getMessaging(app)
|
|
34
|
+
return vapidKey
|
|
35
|
+
? m.getToken(messaging, {
|
|
36
|
+
vapidKey: vapidKey
|
|
37
|
+
})
|
|
38
|
+
: m.getToken(messaging)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function getOAuthToken (a) {
|
|
42
|
+
const auth = a.getAuth() // Ensure Firebase is initialized
|
|
43
|
+
const user = auth.currentUser
|
|
44
|
+
console.log("user-------", user);
|
|
45
|
+
if (!user) {
|
|
46
|
+
console.error('User not signed in')
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Get the ID token from Firebase Auth
|
|
51
|
+
const idToken = await user.getIdToken()
|
|
52
|
+
console.log("idtoken--------", idToken);
|
|
53
|
+
return idToken
|
|
25
54
|
}
|
|
26
55
|
|
|
27
|
-
function registerFCMTopic(token) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
56
|
+
async function registerFCMTopic (config, fcmServerKey, token, oauthToken) {
|
|
57
|
+
if (!token) {
|
|
58
|
+
console.error('No Token Found')
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const serverKey = typeof fcmServerKey === 'function' ? await fcmServerKey(config) : fcmServerKey
|
|
63
|
+
console.log('serverKey-------', serverKey)
|
|
64
|
+
const url = `https://iid.googleapis.com/iid/v1/${token}/rel/topics/all`
|
|
65
|
+
try {
|
|
66
|
+
await fetch(url, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: {
|
|
69
|
+
Authorization: `Bearer ${oauthToken}`,
|
|
70
|
+
'content-type': 'application/json'
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
console.log('Registration Done Successfully')
|
|
74
|
+
return
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error('FCM Subscription Failed', error)
|
|
77
|
+
return
|
|
78
|
+
}
|
|
35
79
|
}
|
package/client/start.js
CHANGED
|
@@ -307,7 +307,7 @@ export function startApp(renderApplication, reducers, opts) {
|
|
|
307
307
|
const mssgSenderId = fcm.messagingSenderId;
|
|
308
308
|
const projectId = fcm.projectId;
|
|
309
309
|
const apiKey = fcm.apiKey;
|
|
310
|
-
if (mssgSenderId && projectId && apiKey) initializeFCM(fcm);
|
|
310
|
+
if (mssgSenderId && projectId && apiKey) initializeFCM(fcm, page);
|
|
311
311
|
}
|
|
312
312
|
}
|
|
313
313
|
|
package/package.json
CHANGED
package/server/routes.js
CHANGED
|
@@ -22,7 +22,6 @@ const { handleManifest, handleAssetLink } = require('./handlers/json-manifest-ha
|
|
|
22
22
|
const { redirectStory } = require('./handlers/story-redirect')
|
|
23
23
|
const { simpleJsonHandler } = require('./handlers/simple-json-handler')
|
|
24
24
|
const { makePickComponentSync } = require('../isomorphic/impl/make-pick-component-sync')
|
|
25
|
-
const { registerFCMTopic } = require('./handlers/fcm-registration-handler')
|
|
26
25
|
const { triggerWebengageNotifications } = require('./handlers/webengage-notifications')
|
|
27
26
|
const rp = require('request-promise')
|
|
28
27
|
const bodyParser = require('body-parser')
|
|
@@ -304,7 +303,6 @@ function getWithConfig (app, route, handler, opts = {}) {
|
|
|
304
303
|
* @param {boolean|function} shouldEncodeAmpUri If set to true, then for every story-page request the slug will be encoded, in case of a vernacular slug this should be set to false. Receives path as param (default: true)
|
|
305
304
|
* @param {number} sMaxAge Overrides the s-maxage value, the default value is set to 900 seconds. We can set `isomorphicRoutesSmaxage: 900` under `publisher` in publisher.yml config file that comes from BlackKnight or pass sMaxAge as a param.
|
|
306
305
|
* @param {number} maxAge Overrides the max-age value, the default value is set to 15 seconds. We can set `isomorphicRoutesMaxage: 15` under `publisher` in publisher.yml config file that comes from BlackKnight or pass maxAge as a param.
|
|
307
|
-
* @param {(string|function)} fcmServerKey FCM serverKey is used for registering FCM Topic.
|
|
308
306
|
* @param {string} appLoadingPlaceholder This string gets injected into the app container when the page is loaded via service worker. Can be used to show skeleton layouts, animations or other progress indicators before it is replaced by the page content.
|
|
309
307
|
* @param {boolean|function} enableExternalStories If set to true, then for every request an external story api call is made and renders the story-page if the story is found. (default: false)
|
|
310
308
|
* @param {string|function} externalIdPattern This string specifies the external id pattern the in the url. Mention `EXTERNAL_ID` to specify the position of external id in the url. Ex: "/parent-section/child-section/EXTERNAL_ID"
|
|
@@ -352,7 +350,6 @@ exports.isomorphicRoutes = function isomorphicRoutes (
|
|
|
352
350
|
sMaxAge = 900,
|
|
353
351
|
maxAge = 15,
|
|
354
352
|
appLoadingPlaceholder = '',
|
|
355
|
-
fcmServerKey = '',
|
|
356
353
|
webengageConfig = {},
|
|
357
354
|
externalIdPattern = '',
|
|
358
355
|
enableExternalStories = false,
|
|
@@ -469,8 +466,6 @@ exports.isomorphicRoutes = function isomorphicRoutes (
|
|
|
469
466
|
})
|
|
470
467
|
)
|
|
471
468
|
|
|
472
|
-
app.post('/register-fcm-topic', bodyParser.json(), withConfig(registerFCMTopic, { publisherConfig, fcmServerKey }))
|
|
473
|
-
|
|
474
469
|
if (webengageConfig.enableWebengage) {
|
|
475
470
|
app.post(
|
|
476
471
|
'/integrations/webengage/trigger-notification',
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
const { get } = require("lodash");
|
|
2
|
-
const request = require("request-promise");
|
|
3
|
-
|
|
4
|
-
exports.registerFCMTopic = async function registerFCM(
|
|
5
|
-
req,
|
|
6
|
-
res,
|
|
7
|
-
next,
|
|
8
|
-
{ config, client, publisherConfig, fcmServerKey }
|
|
9
|
-
) {
|
|
10
|
-
const token = get(req, ["body", "token"], null);
|
|
11
|
-
if (!token) {
|
|
12
|
-
res.status(400).send("No Token Found");
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
|
|
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;
|
|
37
|
-
}
|
|
38
|
-
};
|