API notif dan penambahan package firebase-admin

Add:
- src/app/api/mobile/notifications/
- src/lib/firebase-admin.ts

### No Issue
This commit is contained in:
2025-12-15 17:47:59 +08:00
parent f06482a159
commit c0a9832c66
5 changed files with 283 additions and 8 deletions

View File

@@ -0,0 +1,44 @@
// app/api/test/notifications/route.ts
import { adminMessaging } from "@/lib/firebase-admin";
import { NextRequest, NextResponse } from "next/server";
export async function POST(request: NextRequest) {
try {
const { data } = await request.json();
const { fcmToken, title, body: notificationBody } = data;
console.log("Data Notifikasi >>", data);
if (!fcmToken || !title) {
return NextResponse.json(
{ error: "Missing fcmToken or title" },
{ status: 400 }
);
}
const message = {
token: fcmToken,
notification: {
title,
body: notificationBody || "",
},
data: {
sentAt: new Date().toISOString(), // ✅ Simpan metadata di data
// contoh: senderId, type, etc.
},
};
console.log("[MSG]", message);
const response = await adminMessaging.send(message);
console.log("✅ FCM sent:", response);
return NextResponse.json({ success: true, messageId: response });
} catch (error: any) {
console.error("❌ FCM error:", error);
return NextResponse.json(
{ error: error.message || "Failed to send FCM" },
{ status: 500 }
);
}
}