This commit is contained in:
Rizky 2023-11-07 22:32:30 +07:00
parent b717300468
commit c31d9036b6
3 changed files with 81 additions and 12 deletions

View File

@ -33,6 +33,7 @@ export const initLive = async (p: PG, domain_or_siteid: string) => {
p.status = "loading"; p.status = "loading";
if (w.mobile && w.mobile.send) { if (w.mobile && w.mobile.send) {
w.mobile.bind(p);
w.mobile.send({ type: "ready" }); w.mobile.send({ type: "ready" });
} }
@ -48,6 +49,7 @@ export const initLive = async (p: PG, domain_or_siteid: string) => {
if ( if (
location.hostname === "prasi.app" || location.hostname === "prasi.app" ||
location.hostname === "prasi.web.andromedia.co.id" || location.hostname === "prasi.web.andromedia.co.id" ||
location.hostname.includes("ngrok") ||
location.hostname === "localhost" || location.hostname === "localhost" ||
location.hostname === "127.0.0.1" || location.hostname === "127.0.0.1" ||
location.hostname === "10.0.2.2" // android localhost location.hostname === "10.0.2.2" // android localhost

View File

@ -1,5 +1,9 @@
import { apiClient } from "web-utils";
import { PG } from "./global";
import { w } from "../../../utils/script/init-api";
type NOTIF_ARG = { type NOTIF_ARG = {
user_id: string; user_id: any;
body: string; body: string;
title: string; title: string;
data?: any; data?: any;
@ -10,37 +14,94 @@ export const registerMobile = () => {
register: (user_id: string) => {}, register: (user_id: string) => {},
send: (data: NOTIF_ARG) => {}, send: (data: NOTIF_ARG) => {},
onTap: (data: NOTIF_ARG) => {}, onTap: (data: NOTIF_ARG) => {},
onReceive: (data: NOTIF_ARG) => {},
}, },
}; };
if (window.parent) { if (window.parent) {
let config = { notif_token: "" }; let config = { notif_token: "", p: null as null | PG };
window.addEventListener("message", ({ data: raw }) => { window.addEventListener("message", ({ data: raw }) => {
if (typeof raw === "object" && raw.mobile) { if (typeof raw === "object" && raw.mobile) {
const data = raw as unknown as { const data = raw as unknown as
| {
type: "notification-token"; type: "notification-token";
token: string; token: string;
}; }
| { type: "notification-tap"; notif: NOTIF_ARG }
| { type: "notification-receive"; notif: NOTIF_ARG };
switch (data.type) { switch (data.type) {
case "notification-token": case "notification-token":
config.notif_token = data.token; config.notif_token = data.token;
console.log(config); break;
case "notification-tap":
notifObject.notif.onTap(data.notif);
break;
case "notification-receive":
notifObject.notif.onReceive(data.notif);
break; break;
} }
} }
}); });
return { const getP = () => {
const p = config.p;
if (p && p.site && p.site.api_url) {
const api = w.prasiApi[p.site.api_url];
if (
api &&
api.apiEntry &&
api.apiEntry._notif &&
p.script &&
p.script.api
) {
return p;
}
}
};
const notifObject = {
send: (msg: { type: "ready" }) => { send: (msg: { type: "ready" }) => {
window.parent.postMessage({ mobile: true, ...msg }, "*"); window.parent.postMessage({ mobile: true, ...msg }, "*");
}, },
bind(p: PG) {
config.p = p;
},
config, config,
notif: { notif: {
register: (user_id: string) => {}, register: async (user_id: any) => {
send: (data: NOTIF_ARG) => {}, const p = getP();
onTap: (data: NOTIF_ARG) => {}, if (p) {
return await p.script.api._notif("register", {
type: "register",
id: typeof user_id === "string" ? user_id : user_id.toString(),
token: config.notif_token,
});
}
},
send: async (data: NOTIF_ARG) => {
const p = getP();
if (p) {
return await p.script.api._notif("send", {
type: "send",
id:
typeof data.user_id === "string"
? data.user_id
: data.user_id.toString(),
body: data.body,
title: data.title,
data: data.data,
});
}
},
onTap: (data: null | NOTIF_ARG) => {
console.log("ontap", data);
},
onReceive: (data: NOTIF_ARG) => {
console.log("onreceive", data);
},
}, },
}; };
return notifObject;
} }
return { return {
...default_mobile, ...default_mobile,

View File

@ -66,7 +66,13 @@ const mobile: {
body: string, body: string,
data: any data: any
}) => void; }) => void;
onTap: (data: { onTap: (data: null |{
user_id: string,
title: string,
body: string,
data: any
}) => void | Promise<void>;
onReceive: (data: {
user_id: string, user_id: string,
title: string, title: string,
body: string, body: string,