upd: push notifikasi

This commit is contained in:
amel
2025-06-26 10:17:56 +08:00
parent 4fc2fa0455
commit 0b0ecc7791
7 changed files with 164 additions and 6 deletions

8
src/app/api/fcm/route.ts Normal file
View File

@@ -0,0 +1,8 @@
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 });
}

View File

@@ -1,3 +1,4 @@
import prisma from '@/lib/prisma';
import admin from 'firebase-admin';
import { NextResponse } from "next/server";
@@ -10,18 +11,63 @@ export async function GET(request: Request) {
});
}
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: 'Dari API',
body: 'Hello World',
title: 'New Update :)',
body: 'Check out the latest features in our app.',
},
data: {
category: 'value1',
content: 'value2',
},
token: 'fPfBeTn4R4KsHhDw-8Edj0:APA91bGBjguuZsMhisJua_Wa3m7z7vBCE08vjyDBScmN0eIRgpfINlDx4SI6-upn-rr6tTAcoPxeQkxxbEsohcUbbV5DjUyIG4xR6wuvKOJMp3Mr4rNWFv8',
};
const response = await admin.messaging().send(message);
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);
});
return NextResponse.json({ success: true, message: `Sukses ${admin.apps.length}--${response}` }, { status: 200 });
// 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 });