@progressive-development/pd-spa-helper 0.3.44 → 0.3.45

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 CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Webcomponent pd-spa-helper following open-wc recommendations",
4
4
  "license": "MIT",
5
5
  "author": "pd-spa-helper",
6
- "version": "0.3.44",
6
+ "version": "0.3.45",
7
7
  "main": "dist/src/index.js",
8
8
  "module": "dist/src/index.js",
9
9
  "exports": {
@@ -4,8 +4,6 @@ import { property } from 'lit/decorators.js';
4
4
 
5
5
  import { Subscription } from 'rxjs';
6
6
 
7
- import { FirebaseApp } from 'firebase/app';
8
-
9
7
  import '@progressive-development/pd-page/pd-menu.js';
10
8
  import '@progressive-development/pd-page/pd-footer.js';
11
9
 
@@ -44,7 +42,6 @@ const TOAST_DURATION = 6000;
44
42
  /**
45
43
  * Transformed routes, generated during startInit.
46
44
  */
47
- let app:FirebaseApp | undefined;
48
45
  let transformedRoutes:Array<any>;
49
46
  let navigationConfig:NavigationConfig;
50
47
 
@@ -1,57 +1,42 @@
1
1
  import { FirebaseApp } from "firebase/app";
2
- import { getMessaging, getToken, Messaging, onMessage } from "firebase/messaging";
2
+ import { getMessaging, getToken } from "firebase/messaging";
3
3
 
4
4
  import { MessagingConfig } from "../service-provider-model.js";
5
5
 
6
- let messagingRef: Messaging;
6
+ import { pdStore } from "../../store/mini-rx.store.js";
7
+ import { updateNewNotificationToken } from "../../store/spa-app-actions.js";
7
8
 
8
- function requestPermission() {
9
- console.log('Requesting permission...');
10
- Notification.requestPermission().then((permission) => {
11
- if (permission === 'granted') {
12
- console.log('Notification permission granted.');
13
- }
14
- })
9
+ export const NOTIFICATION_TOKEN = "pd.spa.helper.notification.token";
10
+
11
+ // Funktion zur Anfrage der Berechtigung
12
+ async function ensureNotificationPermission(): Promise<"default" | "denied" | "granted"> {
13
+ if (Notification.permission === "default") {
14
+ return Notification.requestPermission();
15
+ }
16
+ return Notification.permission;
15
17
  }
16
18
 
17
- /**
18
- * During start/load application, initialize Messaging.
19
- *
20
- * @param {*} app - initialized app.
21
- */
22
- export const initFirebaseMessaging = (
19
+ export async function registerDevice(
23
20
  app: FirebaseApp,
24
21
  messageConfig: MessagingConfig
25
- ) => {
26
-
27
- messagingRef = getMessaging(app);
28
-
29
- // Add the public key generated from the console here.
30
- getToken(messagingRef,
31
- {
32
- vapidKey: messageConfig.apidKey
33
- }
34
- ).then((currentToken) => {
35
- if (currentToken) {
36
-
37
- // Send the token to your server and update the UI if necessary
38
- // ...
39
- console.log("My current Token")
40
-
22
+ ) {
23
+ try {
24
+ const messaging = getMessaging(app);
25
+ const permission = await ensureNotificationPermission();
26
+ if (permission === "granted") {
27
+ const currentToken = await getToken(messaging, { vapidKey: messageConfig.apidKey });
28
+ const storedToken = localStorage.getItem(NOTIFICATION_TOKEN);
29
+ if (currentToken && storedToken !== currentToken) {
30
+ pdStore().dispatch(updateNewNotificationToken({
31
+ messagingToken: currentToken
32
+ }));
33
+ } else {
34
+ console.warn("No token received - no new token was generated.");
35
+ }
41
36
  } else {
42
- // Show permission request UI
43
- console.log('No registration token available. Request permission to generate one.');
44
- requestPermission();
37
+ console.warn("Notifications were either not allowed or permanently denied.");
45
38
  }
46
- }).catch((err) => {
47
- console.log('An error occurred while retrieving token. ', err);
48
- // ...
49
- });
50
-
51
- // TODO: Invalid if above code not successfully => refcator avoid call without permissions
52
- onMessage(messagingRef, (payload) => {
53
- console.log('Message received. ', payload);
54
- // ...
55
- });
56
-
57
- }
39
+ } catch (error) {
40
+ console.error("Error during device registration:", error);
41
+ }
42
+ }
@@ -12,7 +12,7 @@ import { callFunctionMock, initMockResponse } from "./mock/function-client.js";
12
12
  import { ServiceCallController } from "../service-call-controller2.js";
13
13
  import { deleteFirestorageDocument, downloadFileFirestorage, getFirestorageFile, getFirestorageFileList, initFirestorage, uploadFirestorageFile } from "./firebase/firestorage-client.js";
14
14
  import { downloadFileMock, getMockFileList, initStorageMock, uploadStorageFileMock } from "./mock/storage-client.js";
15
- import { initFirebaseMessaging } from "./firebase/messagingFirebaseClient.js";
15
+ import { registerDevice } from "./firebase/messagingFirebaseClient.js";
16
16
 
17
17
  let provider: ServiceProviderConfiguration | undefined;
18
18
  let controller: ServiceCallController;
@@ -57,8 +57,9 @@ const initFirebaseApplicationServices = (
57
57
  }
58
58
 
59
59
  if (config.messagingConfig) {
60
- initFirebaseMessaging(firebaseApp, config.messagingConfig);
61
- console.debug("Init provider messaging");
60
+ registerDevice(firebaseApp, config.messagingConfig)
61
+ .then(() => console.debug(`complet registerDevice, right is ${Notification.permission}`));
62
+
62
63
  }
63
64
 
64
65
  console.debug("Firebase initialized");
@@ -1,7 +1,7 @@
1
1
  import { configureStore, Store, LoggerExtension, ReduxDevtoolsExtension, ImmutableStateExtension } from 'mini-rx-store';
2
2
  import { StoreConfig } from '../service-provider/service-provider-model.js';
3
3
  import { spaAppReducer } from '../store/spa-app-reducer.js';
4
- import { appRouteEffect } from '../store/spa-app-effects.js';
4
+ import { appRouteEffect, registerNotificationTokenSuccessEffect } from '../store/spa-app-effects.js';
5
5
 
6
6
  let store: Store;
7
7
 
@@ -9,7 +9,8 @@ const registerEffects = (effects: any[]) => {
9
9
  if (store) {
10
10
  // Register default app route effect
11
11
  store.effect(appRouteEffect);
12
-
12
+ store.effect(registerNotificationTokenSuccessEffect);
13
+
13
14
  // Register configured effects
14
15
  effects.forEach(effect => store.effect(effect));
15
16
  } else {
@@ -14,4 +14,13 @@ export const changeSubTask = action('SPA_APP_CHANGE_SUB_LOADING', payload<{
14
14
  loadingActionId: string,
15
15
  newTaskState: LoadingSubTask
16
16
  }>());
17
- export const removeLoadingState = action('SPA_APP_REMOVE_LOADING', payload<string>());
17
+ export const removeLoadingState = action('SPA_APP_REMOVE_LOADING', payload<string>());
18
+
19
+
20
+ export const updateNewNotificationToken = action('UPDATE_NOTIFICATION_TOKEN', payload<{
21
+ messagingToken: string
22
+ }>());
23
+ export const updateNewNotificationTokenSuccess = action('UPDATE_NOTIFICATION_TOKEN_SUCCESS', payload<{
24
+ messagingToken: string
25
+ }>());
26
+ export const updateNewNotificationTokenFail = action('UPDATE_NOTIFICATION_TOKEN_FAIL', payload<Error>());
@@ -4,7 +4,8 @@ import { tap } from 'rxjs/operators';
4
4
 
5
5
  import { ofType } from 'ts-action-operators';
6
6
 
7
- import { routeAction } from './spa-app-actions.js';
7
+ import { routeAction, updateNewNotificationTokenSuccess } from './spa-app-actions.js';
8
+ import { NOTIFICATION_TOKEN } from '../service-provider/firebase/messagingFirebaseClient.js';
8
9
 
9
10
 
10
11
  let routeElement: HTMLElement;
@@ -34,3 +35,14 @@ export const appRouteEffect = createEffect(
34
35
  dispatch: false
35
36
  }
36
37
  );
38
+
39
+ export const registerNotificationTokenSuccessEffect = createEffect(
40
+ actions$.pipe(
41
+ ofType(updateNewNotificationTokenSuccess),
42
+ tap((action) => {
43
+ localStorage.setItem(NOTIFICATION_TOKEN, action.payload.messagingToken);
44
+ })
45
+ ), {
46
+ dispatch: false
47
+ }
48
+ );