@nitra/cap 7.1.1 → 7.1.2
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/package.json +1 -1
- package/save-token/index.js +24 -16
package/package.json
CHANGED
package/save-token/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Capacitor } from '@capacitor/core'
|
|
2
2
|
import { FirebaseMessaging } from '@capacitor-firebase/messaging'
|
|
3
3
|
import { getToken } from '@nitra/vite-boot/token'
|
|
4
|
-
import { initializeApp } from 'firebase/app'
|
|
4
|
+
import { initializeApp, getApps } from 'firebase/app'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Save the Firebase push token for a given app and platform.
|
|
@@ -15,14 +15,17 @@ export async function saveToken() {
|
|
|
15
15
|
return
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
// Check if Firebase is already initialized
|
|
19
|
+
if (getApps().length === 0) {
|
|
20
|
+
initializeApp({
|
|
21
|
+
apiKey: import.meta.env.VITE_API_KEY,
|
|
22
|
+
authDomain: import.meta.env.VITE_AUTH_DOMAIN,
|
|
23
|
+
projectId: import.meta.env.VITE_PROJECT_ID,
|
|
24
|
+
storageBucket: import.meta.env.VITE_STORAGE_BUCKET,
|
|
25
|
+
messagingSenderId: import.meta.env.VITE_MESSAGING_SENDER_ID,
|
|
26
|
+
appId: import.meta.env.VITE_APP_ID
|
|
27
|
+
})
|
|
28
|
+
}
|
|
26
29
|
|
|
27
30
|
if (Capacitor.isNativePlatform() && !Capacitor.isPluginAvailable('FirebaseMessaging')) {
|
|
28
31
|
console.error('FirebaseMessaging plugin is not available')
|
|
@@ -65,14 +68,19 @@ export async function saveToken() {
|
|
|
65
68
|
* @returns {Promise<object>} Web Push token
|
|
66
69
|
*/
|
|
67
70
|
async function getWebPushToken() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
+
try {
|
|
72
|
+
const serviceWorkerRegistration = await navigator.serviceWorker.register('sw.js', {
|
|
73
|
+
type: 'module'
|
|
74
|
+
})
|
|
71
75
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
return await FirebaseMessaging.getToken({
|
|
77
|
+
vapidKey: import.meta.env.VITE_VAPID_KEY,
|
|
78
|
+
serviceWorkerRegistration
|
|
79
|
+
})
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error('Error getting web push token:', error)
|
|
82
|
+
throw error
|
|
83
|
+
}
|
|
76
84
|
}
|
|
77
85
|
|
|
78
86
|
/**
|