upd: firebase admin
Deskripsi: - percobaan api elysia firebase admin fcm No Issues
This commit is contained in:
@@ -39,8 +39,10 @@
|
||||
"@tiptap/pm": "^2.11.7",
|
||||
"@tiptap/react": "^2.4.0",
|
||||
"@tiptap/starter-kit": "^2.4.0",
|
||||
"@types/bun": "^1.2.17",
|
||||
"@types/busboy": "^1.5.4",
|
||||
"@types/lodash": "^4.17.6",
|
||||
"@types/minimist": "^1.2.5",
|
||||
"@types/multer": "^1.4.12",
|
||||
"@types/web-push": "^3.6.3",
|
||||
"busboy": "^1.6.0",
|
||||
@@ -56,6 +58,7 @@
|
||||
"iron-session": "^8.0.2",
|
||||
"jose": "^5.9.2",
|
||||
"lodash": "^4.17.21",
|
||||
"minimist": "^1.2.8",
|
||||
"moment": "^2.30.1",
|
||||
"multer": "^1.4.5-lts.2",
|
||||
"next": "14.2.4",
|
||||
@@ -71,6 +74,7 @@
|
||||
"rrule": "^2.8.1",
|
||||
"sharp": "^0.33.5",
|
||||
"supabase": "^1.192.5",
|
||||
"tsx": "^4.20.3",
|
||||
"web-push": "^3.6.7",
|
||||
"wibu-cli": "^1.0.91",
|
||||
"wibu-pkg": "^1.0.63",
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { sendFCM } from "../../../../xfcm/send";
|
||||
|
||||
export const GET = () => {
|
||||
const token = "cRz96GHKTRaQaRJ35e8Hxa:APA91bEUSxE0VPbqKSzseQ_zGhbYsDofMexKykRw7o_3z2aPM9YFmZbeA2enrmb3qjdZ2g4-QQtiNHAyaZqAT1ITOrwo9jVJlShTeABmEFYP5GLEUZ3dlLc";
|
||||
sendFCM(token, "Test dari Local", "Ini hanya percobaan notifikasi dari script.");
|
||||
return NextResponse.json({ success: true, message: "Sukses" }, { status: 200 });
|
||||
}
|
||||
21
src/app/api/mobile/fcm/[[...slug]]/route.ts
Normal file
21
src/app/api/mobile/fcm/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { sendFCM } from "@/lib/firebase/fcm";
|
||||
import elysia from "elysia";
|
||||
|
||||
|
||||
const ApiV2 = new elysia({
|
||||
prefix: "/api/mobile/fcm"
|
||||
})
|
||||
.get("/", async () => {
|
||||
const token = [
|
||||
'c89yuexsS_uc1tOErVPu5a:APA91bEb6tEKXAfReZjFVJ2mMyOzoW_RXryLSnSJTpbIVV3G0L_DCNkLuRvJ02Ip-Erz88QCQBAt-C2SN8eCRxu3-v1sBzXzKPtDv-huXpkjXsyrkifqvUo',
|
||||
'cRz96GHKTRaQaRJ35e8Hxa:APA91bEUSxE0VPbqKSzseQ_zGhbYsDofMexKykRw7o_3z2aPM9YFmZbeA2enrmb3qjdZ2g4-QQtiNHAyaZqAT1ITOrwo9jVJlShTeABmEFYP5GLEUZ3dlLc'
|
||||
]
|
||||
|
||||
sendFCM(token, "Test dari Production STG", "Ini hanya percobaan notifikasi dari script.");
|
||||
return {
|
||||
data: "success elysia api"
|
||||
};
|
||||
})
|
||||
|
||||
|
||||
export const GET = ApiV2.handle;
|
||||
@@ -1,76 +0,0 @@
|
||||
import prisma from '@/lib/prisma';
|
||||
import admin from 'firebase-admin';
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
if (!admin.apps.length) {
|
||||
const serviceAccount = require("../../../../service_key.json");
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
});
|
||||
}
|
||||
|
||||
const token = [
|
||||
// 'fPfBeTn4R4KsHhDw-8Edj0:APA91bGBjguuZsMhisJua_Wa3m7z7vBCE08vjyDBScmN0eIRgpfINlDx4SI6-upn-rr6tTAcoPxeQkxxbEsohcUbbV5DjUyIG4xR6wuvKOJMp3Mr4rNWFv8',
|
||||
// 'c89yuexsS_uc1tOErVPu5a:APA91bEb6tEKXAfReZjFVJ2mMyOzoW_RXryLSnSJTpbIVV3G0L_DCNkLuRvJ02Ip-Erz88QCQBAt-C2SN8eCRxu3-v1sBzXzKPtDv-huXpkjXsyrkifqvUo',
|
||||
'cRz96GHKTRaQaRJ35e8Hxa:APA91bEUSxE0VPbqKSzseQ_zGhbYsDofMexKykRw7o_3z2aPM9YFmZbeA2enrmb3qjdZ2g4-QQtiNHAyaZqAT1ITOrwo9jVJlShTeABmEFYP5GLEUZ3dlLc'
|
||||
]
|
||||
|
||||
|
||||
const message = {
|
||||
notification: {
|
||||
title: 'New Update :)',
|
||||
body: 'Check out the latest features in our app.',
|
||||
},
|
||||
data: {
|
||||
id: 'cm1eqwkwj00067j2bzejyh9u7',
|
||||
category: 'division',
|
||||
content: 'cm1eqwkvu00017j2b8qnisp9g',
|
||||
},
|
||||
};
|
||||
|
||||
admin.messaging().sendEachForMulticast({
|
||||
tokens: token,
|
||||
...message,
|
||||
})
|
||||
.then(async (response) => {
|
||||
// Handle successful sends and failed tokens
|
||||
if (response.failureCount > 0) {
|
||||
const failedTokens: any[] = [];
|
||||
response.responses.forEach((resp, idx) => {
|
||||
if (!resp.success) {
|
||||
failedTokens.push(token[idx]);
|
||||
}
|
||||
});
|
||||
const deleteToken = await prisma.tokenDeviceUser.deleteMany({
|
||||
where: {
|
||||
token: {
|
||||
in: failedTokens
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error sending message:', error);
|
||||
});
|
||||
|
||||
// const message = {
|
||||
// notification: {
|
||||
// title: 'Dari API Localhost Coba',
|
||||
// body: 'Hello World Coba',
|
||||
// },
|
||||
|
||||
// token: 'c89yuexsS_uc1tOErVPu5a:APA91bEb6tEKXAfReZjFVJ2mMyOzoW_RXryLSnSJTpbIVV3G0L_DCNkLuRvJ02Ip-Erz88QCQBAt-C2SN8eCRxu3-v1sBzXzKPtDv-huXpkjXsyrkifqvUo',
|
||||
|
||||
// };
|
||||
|
||||
// const response = await admin.messaging().send(message);
|
||||
|
||||
return NextResponse.json({ success: true, message: `Sukses ${admin.apps.length}` }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
return NextResponse.json({ success: true, version: "1.5.0", tahap: "beta", update: "- update api mobile; - firebase admin" }, { status: 200 });
|
||||
return NextResponse.json({ success: true, version: "1.5.1", tahap: "beta", update: "-percobaan firebase admin fcm" }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, version: "Gagal mendapatkan version, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
|
||||
66
src/lib/firebase/fcm.ts
Normal file
66
src/lib/firebase/fcm.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import admin from "firebase-admin";
|
||||
|
||||
// Inisialisasi admin (hanya sekali)
|
||||
if (!admin.apps.length) {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
projectId: process.env.GOOGLE_PROJECT_ID,
|
||||
privateKey: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
||||
clientEmail: process.env.GOOGLE_CLIENT_EMAIL,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Kirim push notifikasi ke FCM token
|
||||
*/
|
||||
export async function sendFCM(token: string[], title: string, body: string) {
|
||||
const message = {
|
||||
notification: {
|
||||
title,
|
||||
body,
|
||||
},
|
||||
data: {
|
||||
id: 'cm1eqwkwj00067j2bzejyh9u7',
|
||||
category: 'division',
|
||||
content: 'cm1eqwkvu00017j2b8qnisp9g',
|
||||
},
|
||||
apns: {
|
||||
payload: {
|
||||
aps: {
|
||||
contentAvailable: true,
|
||||
alert: {
|
||||
title,
|
||||
body,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
token,
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await admin.messaging().sendEachForMulticast({
|
||||
tokens: token,
|
||||
...message,
|
||||
})
|
||||
console.log("✅ FCM berhasil dikirim:", result);
|
||||
if (result.failureCount > 0) {
|
||||
const failedTokens: any[] = [];
|
||||
result.responses.forEach((resp, idx) => {
|
||||
if (!resp.success) {
|
||||
failedTokens.push(token[idx]);
|
||||
}
|
||||
});
|
||||
// const deleteToken = await prisma.tokenDeviceUser.deleteMany({
|
||||
// where: {
|
||||
// token: {
|
||||
// in: failedTokens
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("❌ Gagal mengirim FCM:", err);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import admin from "firebase-admin";
|
||||
|
||||
if (!admin.apps.length) {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
projectId: process.env.GOOGLE_PROJECT_ID,
|
||||
privateKey: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
||||
clientEmail: process.env.GOOGLE_CLIENT_EMAIL,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export default admin;
|
||||
38
xfcm/send.ts
38
xfcm/send.ts
@@ -1,38 +0,0 @@
|
||||
import admin from "firebase-admin";
|
||||
|
||||
// Inisialisasi admin (hanya sekali)
|
||||
if (!admin.apps.length) {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
projectId: process.env.GOOGLE_PROJECT_ID,
|
||||
privateKey: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
||||
clientEmail: process.env.GOOGLE_CLIENT_EMAIL,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Kirim push notifikasi ke FCM token
|
||||
*/
|
||||
export async function sendFCM(token: string, title: string, body: string) {
|
||||
const message = {
|
||||
token,
|
||||
notification: {
|
||||
title,
|
||||
body,
|
||||
},
|
||||
data: {
|
||||
tested: "true"
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const result = await admin.messaging().send(message);
|
||||
console.log("✅ FCM berhasil dikirim:", result);
|
||||
} catch (err) {
|
||||
console.error("❌ Gagal mengirim FCM:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// const token = "cRz96GHKTRaQaRJ35e8Hxa:APA91bEUSxE0VPbqKSzseQ_zGhbYsDofMexKykRw7o_3z2aPM9YFmZbeA2enrmb3qjdZ2g4-QQtiNHAyaZqAT1ITOrwo9jVJlShTeABmEFYP5GLEUZ3dlLc";
|
||||
// sendFCM(token, "Test dari Local", "Ini hanya percobaan notifikasi dari script.");
|
||||
Reference in New Issue
Block a user