Procházet zdrojové kódy

push notifications wip (interrompido pela tarefa do s3 pois recebi acesso)

Gustavo Zanatta před 2 týdny
rodič
revize
b22534d6fd
5 změnil soubory, kde provedl 64 přidání a 0 odebrání
  1. 10 0
      package-lock.json
  2. 1 0
      package.json
  3. 1 0
      quasar.config.js
  4. 13 0
      src/api/deviceToken.js
  5. 39 0
      src/boot/push-notifications.js

+ 10 - 0
package-lock.json

@@ -12,6 +12,7 @@
         "@capacitor/device": "^7.0.1",
         "@capacitor/geolocation": "^8.2.0",
         "@capacitor/google-maps": "^8.0.1",
+        "@capacitor/push-notifications": "^8.1.1",
         "@quasar/cli": "^2.5.0",
         "@quasar/extras": "^1.17.0",
         "axios": "^1.9.0",
@@ -377,6 +378,15 @@
         "@capacitor/core": ">=7.0.0"
       }
     },
+    "node_modules/@capacitor/push-notifications": {
+      "version": "8.1.1",
+      "resolved": "https://registry.npmjs.org/@capacitor/push-notifications/-/push-notifications-8.1.1.tgz",
+      "integrity": "sha512-WqzjPKIbYbARMN+GC0XMAJcxJpUUzqgzS/Ny8RODLrro38pQhm3GXYwX2Mwd+LZlLY39rGImkCkrKyQSNfuikA==",
+      "license": "MIT",
+      "peerDependencies": {
+        "@capacitor/core": ">=8.0.0"
+      }
+    },
     "node_modules/@capacitor/status-bar": {
       "version": "7.0.1",
       "resolved": "https://registry.npmjs.org/@capacitor/status-bar/-/status-bar-7.0.1.tgz",

+ 1 - 0
package.json

@@ -22,6 +22,7 @@
     "@capacitor/device": "^7.0.1",
     "@capacitor/geolocation": "^8.2.0",
     "@capacitor/google-maps": "^8.0.1",
+    "@capacitor/push-notifications": "^8.1.1",
     "@quasar/cli": "^2.5.0",
     "@quasar/extras": "^1.17.0",
     "axios": "^1.9.0",

+ 1 - 0
quasar.config.js

@@ -24,6 +24,7 @@ export default defineConfig((ctx) => {
       "axios",
       "i18n",
       "defaultPropsComponents",
+      "push-notifications",
       // "socket.io",
     ],
 

+ 13 - 0
src/api/deviceToken.js

@@ -0,0 +1,13 @@
+import api from "src/api";
+
+export const registerDeviceToken = async (token, platform) => {
+  await api.post("/device-tokens", {
+    token,
+    platform,
+    app_type: "prestador",
+  });
+};
+
+export const removeDeviceToken = async (token) => {
+  await api.delete(`/device-tokens/${token}`);
+};

+ 39 - 0
src/boot/push-notifications.js

@@ -0,0 +1,39 @@
+import { defineBoot } from "#q-app/wrappers";
+import { Capacitor } from "@capacitor/core";
+import { PushNotifications } from "@capacitor/push-notifications";
+import { registerDeviceToken } from "src/api/deviceToken";
+
+const registerPushNotifications = async () => {
+  let permission = await PushNotifications.checkPermissions();
+
+  if (permission.receive === "prompt") {
+    permission = await PushNotifications.requestPermissions();
+  }
+
+  if (permission.receive !== "granted") {
+    return;
+  }
+
+  await PushNotifications.register();
+};
+
+export default defineBoot(() => {
+  if (!Capacitor.isNativePlatform()) {
+    return;
+  }
+
+  PushNotifications.addListener("registration", async (token) => {
+    const platform = Capacitor.getPlatform();
+    try {
+      await registerDeviceToken(token.value, platform);
+    } catch {
+      // falha silenciosa — não bloqueia o uso do app
+    }
+  });
+
+  PushNotifications.addListener("registrationError", () => {
+    // falha silenciosa
+  });
+
+  registerPushNotifications();
+});