upd: firebase api
Deskripsi; - test firebase push notifikasi pada api mobile No Issues
This commit is contained in:
43
src/app/api/mobile/route.ts
Normal file
43
src/app/api/mobile/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import admin from 'firebase-admin';
|
||||
import { getApps, initializeApp, getApp } from 'firebase-admin/app';
|
||||
|
||||
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 firebaseConfig = {
|
||||
apiKey: "AIzaSyB2hbsW91J3oRQx4_jgrCCNY0tNt5-21e8",
|
||||
authDomain: "googleapis.com",
|
||||
projectId: "mobile-darmasaba",
|
||||
storageBucket: "mobile-darmasaba.appspot.com",
|
||||
messagingSenderId: "867439221179",
|
||||
appId: "1:867439221179:android:4509f77478c8dce99b0c9e",
|
||||
databaseURL: "https://mobile-darmasaba-default-rtdb.asia-southeast1.firebasedatabase.app/",
|
||||
measurementId: "G-1015652111565"
|
||||
}
|
||||
|
||||
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
|
||||
|
||||
const message = {
|
||||
notification: {
|
||||
title: 'Dari API',
|
||||
body: 'Hello World',
|
||||
},
|
||||
token: 'fPfBeTn4R4KsHhDw-8Edj0:APA91bGBjguuZsMhisJua_Wa3m7z7vBCE08vjyDBScmN0eIRgpfINlDx4SI6-upn-rr6tTAcoPxeQkxxbEsohcUbbV5DjUyIG4xR6wuvKOJMp3Mr4rNWFv8',
|
||||
};
|
||||
|
||||
const response = await admin.messaging().send(message);
|
||||
console.log(response)
|
||||
|
||||
return NextResponse.json({ success: true, message: "Sukses" }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user