upd: test fcm tambah pengumuman
This commit is contained in:
@@ -100,17 +100,6 @@ export async function GET(request: Request) {
|
|||||||
createdAt: moment(v.createdAt).format("ll")
|
createdAt: moment(v.createdAt).format("ll")
|
||||||
}))
|
}))
|
||||||
|
|
||||||
await sendFCMNotificationMany({
|
|
||||||
token: [
|
|
||||||
'c89yuexsS_uc1tOErVPu5a:APA91bEb6tEKXAfReZjFVJ2mMyOzoW_RXryLSnSJTpbIVV3G0L_DCNkLuRvJ02Ip-Erz88QCQBAt-C2SN8eCRxu3-v1sBzXzKPtDv-huXpkjXsyrkifqvUo',
|
|
||||||
'cRz96GHKTRaQaRJ35e8Hxa:APA91bEUSxE0VPbqKSzseQ_zGhbYsDofMexKykRw7o_3z2aPM9YFmZbeA2enrmb3qjdZ2g4-QQtiNHAyaZqAT1ITOrwo9jVJlShTeABmEFYP5GLEUZ3dlLc',
|
|
||||||
'gagal_device1',
|
|
||||||
'gagal_device2'
|
|
||||||
],
|
|
||||||
title: "Judul Notifikasi Pengumuman api biasa",
|
|
||||||
body: "Pengumuman untuk perangkat api biasa",
|
|
||||||
data: { id: "cm1eqwkwj00067j2bzejyh9u7", category: "division", content: "cm1eqwkvu00017j2b8qnisp9g" }
|
|
||||||
})
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan pengumuman", data: allData, }, { status: 200 });
|
return NextResponse.json({ success: true, message: "Berhasil mendapatkan pengumuman", data: allData, }, { status: 200 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -185,6 +174,11 @@ export async function POST(request: Request) {
|
|||||||
select: {
|
select: {
|
||||||
subscription: true
|
subscription: true
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
TokenDeviceUser:{
|
||||||
|
select:{
|
||||||
|
token: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -193,10 +187,17 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
|
|
||||||
// mengirim notifikasi
|
// mengirim notifikasi
|
||||||
|
// dataFCM untuk push notifikasi mobile
|
||||||
// datanotif untuk realtime notifikasi
|
// datanotif untuk realtime notifikasi
|
||||||
// datapush untuk web push notifikasi ketika aplikasi tidak aktif
|
// datapush untuk web push notifikasi ketika aplikasi tidak aktif
|
||||||
|
const dataFCM = memberNotif.map((v: any) => ({
|
||||||
|
..._.omit(v, ["idUser", "User", "Subscribe", "TokenDeviceUser"]),
|
||||||
|
tokens: v.User.TokenDeviceUser.map((v: any) => v.token)
|
||||||
|
}))
|
||||||
|
const tokenDup = dataFCM.filter((v: any) => v.tokens.length > 0).map((v: any) => v.tokens).flat();
|
||||||
|
|
||||||
const dataNotif = memberNotif.map((v: any) => ({
|
const dataNotif = memberNotif.map((v: any) => ({
|
||||||
..._.omit(v, ["idUser", "User", "Subscribe"]),
|
..._.omit(v, ["idUser", "User", "Subscribe", "TokenDeviceUser"]),
|
||||||
idUserTo: v.idUser,
|
idUserTo: v.idUser,
|
||||||
idUserFrom: userId,
|
idUserFrom: userId,
|
||||||
category: 'announcement',
|
category: 'announcement',
|
||||||
@@ -205,12 +206,14 @@ export async function POST(request: Request) {
|
|||||||
desc: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.'
|
desc: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.'
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
const dataPush = memberNotif.map((v: any) => ({
|
const dataPush = memberNotif.map((v: any) => ({
|
||||||
..._.omit(v, ["idUser", "User", "Subscribe"]),
|
..._.omit(v, ["idUser", "User", "Subscribe", "TokenDeviceUser"]),
|
||||||
idUser: v.idUser,
|
idUser: v.idUser,
|
||||||
subscription: v.User.Subscribe?.subscription,
|
subscription: v.User.Subscribe?.subscription,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
if (userRoleLogin != "supadmin") {
|
if (userRoleLogin != "supadmin") {
|
||||||
const perbekel = await prisma.user.findFirst({
|
const perbekel = await prisma.user.findFirst({
|
||||||
where: {
|
where: {
|
||||||
@@ -224,10 +227,16 @@ export async function POST(request: Request) {
|
|||||||
select: {
|
select: {
|
||||||
subscription: true
|
subscription: true
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
TokenDeviceUser:{
|
||||||
|
select:{
|
||||||
|
token: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tokenDup.push(perbekel?.TokenDeviceUser.map((v: any) => v.token).flat())
|
||||||
|
|
||||||
dataNotif.push({
|
dataNotif.push({
|
||||||
idUserTo: perbekel?.id,
|
idUserTo: perbekel?.id,
|
||||||
@@ -245,6 +254,8 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const pushNotif = dataPush.filter((item) => item.subscription != undefined)
|
const pushNotif = dataPush.filter((item) => item.subscription != undefined)
|
||||||
|
|
||||||
const sendWebPush = await funSendWebPush({ sub: pushNotif, message: { title: 'Pengumuman Baru', body: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.' } })
|
const sendWebPush = await funSendWebPush({ sub: pushNotif, message: { title: 'Pengumuman Baru', body: 'Anda memiliki pengumuman baru. Silahkan periksa detailnya.' } })
|
||||||
@@ -252,6 +263,14 @@ export async function POST(request: Request) {
|
|||||||
data: dataNotif
|
data: dataNotif
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const tokenUnique = [...new Set(tokenDup)];
|
||||||
|
await sendFCMNotificationMany({
|
||||||
|
token: tokenUnique,
|
||||||
|
title: "Pengumuman Baru",
|
||||||
|
body: "Anda memiliki pengumuman baru. Silahkan periksa detailnya.",
|
||||||
|
data: { id: data.id, category: "announcement", content: data.id }
|
||||||
|
})
|
||||||
|
|
||||||
// create log user
|
// create log user
|
||||||
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data pengumuman baru', table: 'announcement', data: data.id, user: userMobile.id })
|
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data pengumuman baru', table: 'announcement', data: data.id, user: userMobile.id })
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
|
|||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
try {
|
try {
|
||||||
return NextResponse.json({ success: true, version: "1.5.3", tahap: "beta", update: "-percobaan firebase admin fcm many device" }, { status: 200 });
|
return NextResponse.json({ success: true, version: "1.5.4", tahap: "beta", update: "-percobaan firebase admin fcm many device (tambah pengumuman)" }, { status: 200 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(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 });
|
return NextResponse.json({ success: false, version: "Gagal mendapatkan version, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
|||||||
Reference in New Issue
Block a user