-
-
Save vhuytdt/f7ccecb6099e7e47edbdbb195d2c7514 to your computer and use it in GitHub Desktop.
Notification Api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { notificationDoc, tokenDoc, notiTemplateDoc } from "../api/rootRef"; | |
import firebaseClient from "../components/Notification/FirebaseClient"; | |
export const pushNotification = async (senderId,receiverId,message,photo,link, notificationTypeId=0,jobId,title="Tin nhắn mới") => { | |
notificationDoc.add({ | |
senderId, | |
receiverId, | |
notificationTypeId, | |
message, | |
photo, | |
link, | |
notificationTemplate:{}, | |
dateCreated:new Date(), | |
jobId, | |
title, | |
isRead :false | |
}) | |
} | |
export const pushNotificationTemplate = async (senderId,receiverId,message,photo,link, | |
notificationTypeId=0,jobId,title="Tin nhắn mới", | |
code="NO1",senderName = "",jobTitle = "", | |
option="ứng tuyển",receiverMail="", | |
receiverName="",codeEmail) => { | |
let notification = await convertToTemplate(code,{ | |
senderId, | |
receiverId, | |
notificationTypeId, | |
message, | |
photo, | |
link, | |
notificationTemplate:{}, | |
dateCreated:new Date(), | |
jobId, | |
title, | |
isRead :false, | |
senderName, | |
jobTitle, | |
option, | |
code, | |
receiverMail, | |
receiverName, | |
codeEmail | |
}); | |
notificationDoc.add(notification); | |
} | |
export const convertToTemplate = async (code,notification) => { | |
snapNotificationTemplate = await notiTemplateDoc.where("code","==",code).limit(1).get(); | |
snapNotificationTemplate.forEach( snapTemplate => { | |
const template = snapTemplate.data(); | |
notification.title = template.title.replace("[SenderName]",notification.senderName); | |
notification.title = notification.title.replace("[ReceiverName]",notification.receiverName); | |
notification.title = notification.title.replace("[Option]",notification.option); | |
notification.title = notification.title.replace("[JobTitle]",notification.jobTitle); | |
let message = template.message.replace("[Option]",notification.option); | |
message = message.replace("[JobTitle]",notification.jobTitle); | |
message = message.replace("[ReceiverName]",notification.receiverName); | |
message = message.replace("[SenderName]",notification.senderName); | |
notification.message = message; | |
notification.photo = template.photo.replace("[Photo]",notification.photo); | |
}) | |
return notification; | |
} | |
export const sendRemoteDataNotification = async (token,title,message) => { | |
let body = { | |
"to": token, | |
"notification":{ | |
"title":title, | |
"body": message, | |
"sound": "default" | |
}, | |
"data":{ | |
}, | |
"priority": "high" | |
} | |
firebaseClient.send(JSON.stringify(body), "notification-data"); | |
} | |
export const sendNotifications = async ( recevierId, title, message ) => { | |
const snapToken = await tokenDoc.where( "id", "==", recevierId ).get(); | |
snapToken.forEach( token => { | |
const valueToken = token.data(); | |
sendRemoteDataNotification(valueToken.token,title,message); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment