Files
barg-restuarant/server/plugins/pushNotif.js
T
hamid.zarghami1@gmail.com e644edbd65 transfer project in github
2024-05-12 15:29:27 +03:30

186 lines
4.2 KiB
JavaScript

const {admin} = require('./../fireBaseConfig')
// var objNotif = {
// title:"",
// body:""
// }
// const payload = {
// notification: objNotif,
// data: {
// routeName: result.id,
// type: "bazaar",
// routeType: "viewBazaar",
// },
// };
module.exports.notification = {
sendBatch : async (inputs) => {
const {data, payload} = inputs;
try {
// set variables
const failedTokens = [];
// Set message body
const obj = {
android: {
notification: {
sound: 'default',
default_sound: true,
default_vibrate_timings: true,
},
},
apns: {
payload: {
aps: {
badge: 12,
sound: 'default',
},
},
},
data: payload.data,
notification: {},
token: {},
};
// Set ttl if exists
if (payload?.android?.ttl) obj.android.ttl = payload.ttl;
// complete messages body
const messages = [];
data.map((item) => {
const newData = JSON.parse(JSON.stringify(obj));
newData.notification.title = item.title;
newData.notification.body = item.body;
newData.token = item.token;
messages.push(newData);
});
// run fcm
const response = await admin.messaging().sendAll(messages);
// operate on result
if (response.failureCount > 0) {
response?.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push({
message: resp.error.message,
cid: data[idx].cid,
});
}
});
}
return ;
} catch (e) {
console.log(e.message);
return exits.error(e.message);
}
},
sendMulti : async (inputs) => {
const {registrationTokens, payload} = inputs;
try {
// Set message main body
const message = {
android: {
notification: {
sound: 'default',
default_sound: true,
default_vibrate_timings: true,
},
},
apns: {
payload: {
aps: {
badge: 12,
sound: 'default',
},
},
},
notification: payload.notification,
data: payload.data,
tokens: registrationTokens,
};
// Run fcm
const response = await admin.messaging().sendMulticast(message);
// get some data from response
// result.failureCount = response.failureCount;
// result.successCount = response.successCount;
// if there was any failer in sending messsage
// if (response.failureCount > 0) {
// response.responses.forEach((resp, idx) => {
// if (!resp.success) {
// failedTokens.push({
// token: registrationTokens[idx],
// message: resp.error.message,
// cid: cid[idx],
// });
// }
// });
// }
// result.failedTokens = failedTokens;
// send response
return true;
} catch (e) {
console.log(e.message);
return false;
}
},
sendSingle : async (inputs) => {
const {registrationTokens, payload} = inputs;
try {
// Set message main body
var message = {
android: {
notification: {
sound: 'default',
default_sound: true,
default_vibrate_timings: true,
},
},
apns: {
payload: {
aps: {
badge: 12,
sound: 'default',
},
},
},
webpush:{
headers:{
TTL:"86400"
}
},
notification: payload.notification,
data: payload.data,
token: registrationTokens,
// priority: "high",
// timeToLive: 60 * 60 * 24
};
// Check if there is any ttl in payload
if (payload?.android?.ttl) message.android.ttl = payload.ttl;
// Run fcm
const response = await admin.messaging().send(message);
// Check response
if (!response) return false;
return true;
} catch (e) {
console.log(e.message);
return false;
}
}
}