@saltcorn/mobile-app 1.3.0-beta.9 → 1.3.1-beta.0

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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@saltcorn/mobile-app",
3
3
  "displayName": "Saltcorn mobile app",
4
- "version": "1.3.0-beta.9",
4
+ "version": "1.3.1-beta.0",
5
5
  "description": "Saltcorn mobile app for Android and iOS",
6
6
  "main": "index.js",
7
7
  "scripts": {
@@ -7,6 +7,10 @@ import { router } from "../routing/index";
7
7
  import { getLastOfflineSession, deleteOfflineData, sync } from "./offline_mode";
8
8
  import { addRoute, replaceIframe } from "../helpers/navigation";
9
9
  import { showAlerts } from "./common";
10
+ import {
11
+ initPushNotifications,
12
+ unregisterPushNotifications,
13
+ } from "../helpers/notifications";
10
14
 
11
15
  async function loginRequest({ email, password, isSignup, isPublic }) {
12
16
  const opts = isPublic
@@ -70,6 +74,11 @@ export async function login({ email, password, entryPoint, isSignup }) {
70
74
  }
71
75
  }
72
76
  }
77
+ if (config.user.attributes?.notify_push) {
78
+ initPushNotifications();
79
+ } else {
80
+ await unregisterPushNotifications();
81
+ }
73
82
  alerts.push({
74
83
  type: "success",
75
84
  msg: i18next.t("Welcome, %s!", {
@@ -42,10 +42,10 @@ export function showAlerts(alerts, toast = true) {
42
42
  }
43
43
  const successIds = [];
44
44
  area.innerHTML = "";
45
- for (const { type, msg } of alerts) {
45
+ for (const { type, msg, title } of alerts) {
46
46
  if (toast) {
47
47
  const rndid = `tab${Math.floor(Math.random() * 16777215).toString(16)}`;
48
- area.innerHTML += saltcorn.markup.toast(type, msg, rndid);
48
+ area.innerHTML += saltcorn.markup.toast(type, msg, rndid, title);
49
49
  if (type === "success") successIds.push(rndid);
50
50
  } else area.innerHTML += saltcorn.markup.alert(type, msg);
51
51
  }
@@ -0,0 +1,86 @@
1
+ import { Capacitor } from "@capacitor/core";
2
+ import { apiCall } from "./api";
3
+ import { showAlerts } from "./common";
4
+
5
+ async function loadNotificationsPlugin() {
6
+ try {
7
+ const { PushNotifications } = await import("@capacitor/push-notifications");
8
+ return PushNotifications;
9
+ } catch (error) {
10
+ console.warn("Error loading PushNotifications plugin:", error);
11
+ return null;
12
+ }
13
+ }
14
+
15
+ async function loadDevicePlugin() {
16
+ try {
17
+ const { Device } = await import("@capacitor/device");
18
+ return Device;
19
+ } catch (error) {
20
+ console.warn("Error loading Device plugin:", error);
21
+ return null;
22
+ }
23
+ }
24
+
25
+ async function uploadFcmToken(token, deviceId) {
26
+ try {
27
+ const response = await apiCall({
28
+ method: "POST",
29
+ path: "/notifications/fcm-token",
30
+ body: { token, deviceId },
31
+ });
32
+ const data = response.data;
33
+ if (data.success.success === "ok")
34
+ console.log("Token uploaded successfully:", data);
35
+ else console.error("Unable to upload token:", data);
36
+ } catch (error) {
37
+ console.error("Error uploading token:", error);
38
+ }
39
+ }
40
+
41
+ export async function initPushNotifications() {
42
+ const PushNotifications = await loadNotificationsPlugin();
43
+ if (Capacitor.getPlatform() !== "web" && PushNotifications) {
44
+ const { Device } = await loadDevicePlugin();
45
+ const permStatus = await PushNotifications.requestPermissions();
46
+ if (permStatus.receive === "granted") {
47
+ await PushNotifications.register();
48
+ PushNotifications.addListener("registration", async (token) => {
49
+ const { identifier } = await Device.getId();
50
+ await uploadFcmToken(token.value, identifier);
51
+ });
52
+
53
+ PushNotifications.addListener("registrationError", (err) => {
54
+ console.error("Push registration error:", err);
55
+ });
56
+
57
+ PushNotifications.addListener(
58
+ "pushNotificationReceived",
59
+ (notification) => {
60
+ console.log("Push received in foreground:", notification);
61
+ showAlerts([
62
+ {
63
+ type: "info",
64
+ msg: notification.body,
65
+ title: notification.title,
66
+ },
67
+ ]);
68
+ }
69
+ );
70
+ } else {
71
+ console.warn("Push notification permission not granted");
72
+ }
73
+ }
74
+ }
75
+
76
+ export async function unregisterPushNotifications() {
77
+ const PushNotifications = await loadNotificationsPlugin();
78
+ if (Capacitor.getPlatform() !== "web" && PushNotifications) {
79
+ try {
80
+ await PushNotifications.unregister();
81
+ console.log("Push notifications unregistered successfully");
82
+ } catch (error) {
83
+ console.error("Error unregistering push notifications:", error);
84
+ }
85
+ }
86
+ }
package/src/index.js CHANGED
@@ -2,6 +2,7 @@ import { init } from "./init";
2
2
  import * as api from "./helpers/api";
3
3
  import * as auth from "./helpers/auth";
4
4
  import * as common from "./helpers/common";
5
+ import * as notifications from "./helpers/notifications";
5
6
  import * as fileSystem from "./helpers/file_system";
6
7
  import * as navigation from "./helpers/navigation";
7
8
  import * as offlineMode from "./helpers/offline_mode";
@@ -20,6 +21,7 @@ export const mobileApp = {
20
21
  api,
21
22
  auth,
22
23
  common,
24
+ notifications,
23
25
  fileSystem,
24
26
  navigation: { ...navigation, router },
25
27
  offlineMode,