@quintype/framework 7.33.6-fcm-fix.0 → 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 -39
- package/package.json +1 -1
package/client/impl/fcm.js
CHANGED
|
@@ -1,53 +1,79 @@
|
|
|
1
|
-
export function initializeFCM(firebaseConfig, config) {
|
|
2
|
-
console.log(
|
|
1
|
+
export function initializeFCM (firebaseConfig, config) {
|
|
2
|
+
console.log('opts--------', firebaseConfig)
|
|
3
3
|
Promise.all([
|
|
4
|
-
import(/* webpackChunkName: "firebase-app" */
|
|
5
|
-
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')
|
|
6
7
|
])
|
|
7
|
-
.then(([firebase, m]) => {
|
|
8
|
+
.then(([firebase, m, a]) => {
|
|
8
9
|
const app = firebase.initializeApp({
|
|
9
10
|
messagingSenderId: firebaseConfig.messagingSenderId.toString(),
|
|
10
11
|
projectId: firebaseConfig.projectId,
|
|
11
12
|
apiKey: firebaseConfig.apiKey,
|
|
12
13
|
storageBucket: firebaseConfig.storageBucket,
|
|
13
14
|
authDomain: firebaseConfig.authDomain,
|
|
14
|
-
appId: firebaseConfig.appId
|
|
15
|
-
})
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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 }
|
|
20
22
|
// No need to refresh token https://github.com/firebase/firebase-js-sdk/issues/4132
|
|
21
23
|
})
|
|
22
|
-
.then((token) => {
|
|
23
|
-
return registerFCMTopic(
|
|
24
|
+
.then(({token, oauthToken}) => {
|
|
25
|
+
return registerFCMTopic(config, firebaseConfig?.serverKey, token, oauthToken)
|
|
24
26
|
})
|
|
25
|
-
.catch(
|
|
26
|
-
console.error(err)
|
|
27
|
-
})
|
|
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
|
|
28
54
|
}
|
|
29
55
|
|
|
30
|
-
async function registerFCMTopic(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
56
|
+
async function registerFCMTopic (config, fcmServerKey, token, oauthToken) {
|
|
57
|
+
if (!token) {
|
|
58
|
+
console.error('No Token Found')
|
|
59
|
+
return
|
|
60
|
+
}
|
|
35
61
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
}
|
|
79
|
+
}
|