@nitra/cap 1.0.0 → 1.0.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/README.md +6 -1
- package/app-update/README.md +23 -0
- package/app-update/index.js +49 -0
- package/index.js +2 -0
- package/package.json +10 -5
- package/save-token/README.md +84 -0
- package/save-token/firebase-messaging-sw.js +3 -0
- package/save-token/index.js +92 -0
package/README.md
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Nitra Cap components
|
|
2
|
+
|
|
3
|
+
| Папка | Описание |
|
|
4
|
+
|-----------------------------|--------------------------------------------------------|
|
|
5
|
+
| [app-update](./app-apdate/) | Проверка и установка новой версии приложения в маркете |
|
|
6
|
+
| [save-token](./save-token/) | Отримання та збереження push токену |
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Проверка и установка новой версии приложение в маркете
|
|
2
|
+
|
|
3
|
+
## API
|
|
4
|
+
|
|
5
|
+
- [`updateApp(...)`](#updateapp)
|
|
6
|
+
|
|
7
|
+
### updateApp(...)
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
updateApp(isImmediate: boolean | undefined, isMandatory: boolean | undefined ) => Promise<void>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Проверка и установка новой версии приложение в маркете
|
|
14
|
+
|
|
15
|
+
Для **Android**, через [In-app updates](https://developer.android.com/guide/playcore/in-app-updates)]
|
|
16
|
+
Для **IOS**, открываем App Store
|
|
17
|
+
|
|
18
|
+
| Параметр | Тип | Описание |
|
|
19
|
+
|-------------------|-----------|-------------------------------------------------------------------|
|
|
20
|
+
| **`isImmediate`** | `boolean` | только Android, обновление "в полный" экран, по-умолчанию - false |
|
|
21
|
+
| **`isMandatory`** | `boolean` | только Android, обязательно обновление, по-умолчанию - false |
|
|
22
|
+
|
|
23
|
+
---
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Capacitor } from '@capacitor/core'
|
|
2
|
+
import {
|
|
3
|
+
AppUpdate,
|
|
4
|
+
AppUpdateAvailability,
|
|
5
|
+
FlexibleUpdateInstallStatus,
|
|
6
|
+
AppUpdateResultCode
|
|
7
|
+
} from '@capawesome/capacitor-app-update'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Проверка наличия обновления, для Android установка обновления, IOS - открытие AppStore
|
|
11
|
+
* @async
|
|
12
|
+
* @function updateApp
|
|
13
|
+
* @param {boolean} isImmediate - только Android, обновление "в полный" экран, по-умолчанию - false
|
|
14
|
+
* @param {boolean} isMandatory - только Android, обязательно обновление, по-умолчанию - false
|
|
15
|
+
*/
|
|
16
|
+
export async function updateApp(isImmediate, isMandatory) {
|
|
17
|
+
if (!Capacitor.isNativePlatform() || !Capacitor.isPluginAvailable('AppUpdate')) {
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
const { updateAvailability } = await AppUpdate.getAppUpdateInfo()
|
|
21
|
+
if (updateAvailability !== AppUpdateAvailability.UPDATE_AVAILABLE) {
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
if (Capacitor.getPlatform() === 'ios') {
|
|
25
|
+
await AppUpdate.openAppStore()
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
if (Capacitor.getPlatform() === 'android') {
|
|
29
|
+
if (isImmediate) {
|
|
30
|
+
const appUpdateResult = await AppUpdate.performImmediateUpdate()
|
|
31
|
+
if (
|
|
32
|
+
isMandatory &&
|
|
33
|
+
(appUpdateResult.code === AppUpdateResultCode.CANCELED ||
|
|
34
|
+
appUpdateResult.code === AppUpdateResultCode.NOT_ALLOWED)
|
|
35
|
+
) {
|
|
36
|
+
await updateApp(isImmediate, isMandatory)
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
AppUpdate.addListener('onFlexibleUpdateStateChange', flexibleUpdateState => {
|
|
40
|
+
if (flexibleUpdateState.installStatus === FlexibleUpdateInstallStatus.DOWNLOADED) {
|
|
41
|
+
AppUpdate.removeAllListeners()
|
|
42
|
+
AppUpdate.completeFlexibleUpdate()
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
await AppUpdate.startFlexibleUpdate()
|
|
46
|
+
}
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
}
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nitra/cap",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Nitra capacitor components",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -8,13 +8,18 @@
|
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/nitra/
|
|
11
|
+
"url": "git+https://github.com/nitra/cap.git"
|
|
12
12
|
},
|
|
13
13
|
"author": "v@nitra.ai",
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"bugs": {
|
|
16
|
-
"url": "https://github.com/nitra/
|
|
16
|
+
"url": "https://github.com/nitra/cap/issues"
|
|
17
17
|
},
|
|
18
|
-
"homepage": "https://github.com/nitra/
|
|
19
|
-
"dependencies": {
|
|
18
|
+
"homepage": "https://github.com/nitra/cap",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@capacitor-firebase/messaging": "^6.1.0",
|
|
21
|
+
"@capacitor/core": "^6.1.2",
|
|
22
|
+
"@capawesome/capacitor-app-update": "^6.0.0",
|
|
23
|
+
"firebase": "^10.13.1"
|
|
24
|
+
}
|
|
20
25
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Receiving and Saving User Push Tokens
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
|
|
5
|
+
This library is designed to handle obtaining and saving Firebase push tokens on the client side. It supports both mobile platforms (via Capacitor) and web platforms. The main function, `saveToken`, retrieves a push token from Firebase, stores it locally, and sends it to your server.
|
|
6
|
+
|
|
7
|
+
This library interacts with Firebase for token management and Capacitor to check the platform and permissions. It ensures that push tokens are properly handled, regardless of whether the platform is iOS, Android, or Web.
|
|
8
|
+
|
|
9
|
+
## API
|
|
10
|
+
|
|
11
|
+
### `saveToken()`
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
saveToken() => Promise<void>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## usage with Vite
|
|
18
|
+
### Set vapid key : ``` import.meta.env.VITE_VAPID_KEY ```
|
|
19
|
+
### Set host url of the notify project: ``` import.meta.env.VITE_NOTIFY_URL ```
|
|
20
|
+
The ```notify project``` is a server-side component designed to handle API requests for saving user push tokens. It uses the Fastify framework and JWT-based security for authentication.
|
|
21
|
+
### Or set for local dev: ```import.meta.env.VITE_LOCALHOST```
|
|
22
|
+
|
|
23
|
+
### Set the name of your app: ```import.meta.env.VITE_APP```
|
|
24
|
+
|
|
25
|
+
## usage the library
|
|
26
|
+
### To use this package, you should add this code to your project where you want to provide push notifications
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
initializeApp({
|
|
30
|
+
apiKey: import.meta.env.VITE_API_KEY,
|
|
31
|
+
authDomain: import.meta.env.VITE_AUTH_DOMAIN,
|
|
32
|
+
projectId: import.meta.env.VITE_PROJECT_ID,
|
|
33
|
+
storageBucket: import.meta.env.VITE_STORAGE_BUCKET,
|
|
34
|
+
messagingSenderId: import.meta.env.VITE_MESSAGING_SENDER_ID,
|
|
35
|
+
appId: import.meta.env.VITE_APP_ID
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
### This code will initialize firebase
|
|
39
|
+
|
|
40
|
+
## example in node
|
|
41
|
+
### Using function after login
|
|
42
|
+
```jsx
|
|
43
|
+
<template>
|
|
44
|
+
<n-code @token-checked="saveToken" />
|
|
45
|
+
</template>
|
|
46
|
+
|
|
47
|
+
<script setup>
|
|
48
|
+
import NCode from '@nitra/abie-components/auth/NCode.vue'
|
|
49
|
+
import { saveToken } from '@nitra/cap'
|
|
50
|
+
|
|
51
|
+
await saveToken()
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
<route lang="yaml">
|
|
55
|
+
meta:
|
|
56
|
+
layout: blank
|
|
57
|
+
</route>
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Environment Variable Validation with Vite
|
|
62
|
+
### To ensure that required environment variables are set, add the following to your vite.config.js
|
|
63
|
+
```typescript
|
|
64
|
+
import { requireEnvVar } from '@nitra/vite-check-env'
|
|
65
|
+
|
|
66
|
+
export default {
|
|
67
|
+
plugins: [
|
|
68
|
+
requireEnvVar([
|
|
69
|
+
'VITE_API_KEY',
|
|
70
|
+
'VITE_AUTH_DOMAIN',
|
|
71
|
+
'VITE_PROJECT_ID'
|
|
72
|
+
'VITE_STORAGE_BUCKET',
|
|
73
|
+
'VITE_MESSAGING_SENDER_ID',
|
|
74
|
+
'VITE_APP_ID',
|
|
75
|
+
'VITE_VAPID_KEY',
|
|
76
|
+
'VITE_NOTIFY_URL',
|
|
77
|
+
'VITE_APP'])
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
### Install the plugin in the project directory
|
|
82
|
+
```typescript
|
|
83
|
+
yarn add -D @nitra/vite-check-env
|
|
84
|
+
```
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Capacitor } from '@capacitor/core'
|
|
2
|
+
import { FirebaseMessaging } from '@capacitor-firebase/messaging'
|
|
3
|
+
import { getToken } from '@nitra/vite-boot/token'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Save the Firebase push token for a given app and platform.
|
|
7
|
+
* @returns {Promise<void>}
|
|
8
|
+
*/
|
|
9
|
+
export async function saveToken() {
|
|
10
|
+
try {
|
|
11
|
+
if (localStorage.getItem('pushToken')) {
|
|
12
|
+
console.log('Push Token is already exists.')
|
|
13
|
+
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!Capacitor.isPluginAvailable('FirebaseMessaging')) {
|
|
18
|
+
console.error('FirebaseMessaging plugin is not available')
|
|
19
|
+
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let permissions = await FirebaseMessaging.checkPermissions()
|
|
24
|
+
|
|
25
|
+
if (permissions.receive !== 'granted') {
|
|
26
|
+
console.warn('Permissions are not granted, try to request permissions...')
|
|
27
|
+
|
|
28
|
+
let permissionsRequest = await FirebaseMessaging.requestPermissions()
|
|
29
|
+
|
|
30
|
+
if (permissionsRequest.receive !== 'granted') {
|
|
31
|
+
console.error('Permissions are not granted, exiting...')
|
|
32
|
+
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const pushToken = Capacitor.isNativePlatform()
|
|
38
|
+
? await FirebaseMessaging.getToken() // For iOS/Android
|
|
39
|
+
: await getWebPushToken()
|
|
40
|
+
|
|
41
|
+
if (!pushToken?.token && typeof pushToken?.token !== 'string') {
|
|
42
|
+
console.error('Token is undefined exiting...')
|
|
43
|
+
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await sendTokenToServer(pushToken.token)
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('Error while saving push token:', error)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the Firebase push token for web platform.
|
|
55
|
+
* @returns {Promise<object>} Web Push token
|
|
56
|
+
*/
|
|
57
|
+
async function getWebPushToken() {
|
|
58
|
+
const serviceWorkerRegistration = await navigator.serviceWorker.register('src/firebase-messaging-sw.js', {
|
|
59
|
+
type: 'module'
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
return FirebaseMessaging.getToken({
|
|
63
|
+
vapidKey: import.meta.env.VITE_VAPID_KEY,
|
|
64
|
+
serviceWorkerRegistration
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Send the Firebase push token to the server.
|
|
70
|
+
* @param {string} pushToken - The push token returned by Firebase Messaging.
|
|
71
|
+
* @returns {Promise<void>}
|
|
72
|
+
*/
|
|
73
|
+
async function sendTokenToServer(pushToken) {
|
|
74
|
+
const host = import.meta.env.VITE_NOTIFY_URL ?? import.meta.env.VITE_LOCALHOST
|
|
75
|
+
|
|
76
|
+
const response = await fetch(`${host}/save-user`, {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: {
|
|
79
|
+
'Content-Type': 'application/json',
|
|
80
|
+
authorization: `Bearer ${getToken()}`
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({
|
|
83
|
+
platform: Capacitor.getPlatform(),
|
|
84
|
+
app: import.meta.env.VITE_APP,
|
|
85
|
+
token: pushToken
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
if (response.ok) {
|
|
90
|
+
localStorage.setItem('pushToken', pushToken)
|
|
91
|
+
}
|
|
92
|
+
}
|