Added notification generation on SMS. Added FCM Token saving on login.

This commit is contained in:
Patrick Fic
2020-05-01 12:32:40 -07:00
parent 92aeb419a9
commit 636e979fd0
21 changed files with 3810 additions and 45 deletions

View File

@@ -8,6 +8,8 @@ admin.initializeApp({
});
//var defaultApp = admin.initializeApp(defaultAppConfig);
exports.admin = admin;
exports.sendNotification = (req, res) => {
console.log("Firebase Send.");
// const { ids } = req.body;
@@ -19,11 +21,11 @@ exports.sendNotification = (req, res) => {
//Dev
var registrationToken =
"dwsrcoeaIpwEmSzrVkE-_V:APA91bFurr0yCN-yXcaNrJvn8_f47I4vb4avxeS0NR5SCBPNADFB-gC79Hdmj7wqPccPmZCx0NzA_Dqi1lLYegpN-tFvANaK9I00oOSsFnOxv6KNZDLW0WguwFA0vQN8X50BaGuLTQqM";
"fqIWg8ENDFyrRrMWJ1sItR:APA91bHirdZ05Zo66flMlvala97SMXoiQGwP4oCvMwd-vVrSauD_WoNim3kXHGqyP-bzENjkXwA5icyUAReFbeHn6dIaPcbpcsXuY73-eJAXvZiu1gIsrd1BOsnj3dEMT7Q4F6mTPth1";
var message = {
notification: { title: "The Title", body: "The Body" },
data: {
title: "850",
body: "2:45",
jobid: "1234",
},
token: registrationToken,
};

View File

@@ -14,12 +14,20 @@ query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID(
exports.INSERT_MESSAGE = `
mutation INSERT_MESSAGE($msg: [messages_insert_input!]!) {
insert_messages(objects: $msg) {
returning {
id
insert_messages(objects: $msg) {
returning {
conversation {
bodyshop {
associations(where: {active: {_eq: true}}) {
user {
fcmtokens
}
}
}
}
}
}
}
`;
exports.UPDATE_MESSAGE_STATUS = `

View File

@@ -2,9 +2,11 @@ require("dotenv").config();
const client = require("../graphql-client/graphql-client").client;
const queries = require("../graphql-client/queries");
const phone = require("phone");
const admin = require("../firebase/firebase-handler").admin;
exports.receive = (req, res) => {
//Perform request validation
console.log("Twilio Receive Inbound");
if (
!!!req.body ||
!!!req.body.MessagingServiceSid ||
@@ -16,9 +18,9 @@ exports.receive = (req, res) => {
client
.request(queries.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID, {
mssid: req.body.MessagingServiceSid,
phone: phone(req.body.From)[0]
phone: phone(req.body.From)[0],
})
.then(response => {
.then((response) => {
//TODO Add logic for handling MMS.
let newMessage = { msid: req.body.SmsMessageSid, text: req.body.Body };
if (response.bodyshops[0]) {
@@ -29,8 +31,8 @@ exports.receive = (req, res) => {
newMessage.conversation = {
data: {
bodyshopid: response.bodyshops[0].id,
phone_num: phone(req.body.From)[0]
}
phone_num: phone(req.body.From)[0],
},
};
} else if (response.bodyshops[0].conversations.length === 1) {
//Just add it to the conversation
@@ -47,16 +49,52 @@ exports.receive = (req, res) => {
client
.request(queries.INSERT_MESSAGE, { msg: newMessage })
.then(r2 => {
.then((r2) => {
console.log("R2", JSON.stringify(r2));
const arrayOfAllUserFcmTokens = r2.insert_messages.returning[0].conversation.bodyshop.associations.map(
(a) => a.user.fcmtokens
);
const allTokens = [];
arrayOfAllUserFcmTokens.map((i) =>
Object.keys(i).map((k) => allTokens.push(k))
);
const uniqueTokens = [...new Set(allTokens)];
console.log("exports.receive -> uniqueTokens", uniqueTokens);
var message = {
notification: {
title: `New SMS From ${phone(req.body.From)[0]}`,
body: req.body.Body,
},
data: {
jobid: "1234",
},
tokens: uniqueTokens,
};
// Send a message to the device corresponding to the provided
// registration token.
admin
.messaging()
.sendMulticast(message)
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message:", error);
});
res.status(200).end();
})
.catch(e2 => {
.catch((e2) => {
console.log("e2", e2);
res.status(500).json(e2);
});
}
})
.catch(e1 => {
.catch((e1) => {
console.log("e1", e1);
res.status(500).json(e1);
});