I programmed a cloud function to check for Write event and process accordingly.
我编写了一个云函数来检查Write事件并相应地处理。
Following is the data I'm writing
以下是我正在撰写的数据
[ {
"fromId" : 2750,
"fromName" : "TAKETHAN",
"m" : "DEAR PARENT,\nTHIS IS COMMON MESSAGING SYSTEM",
"to" : [ {
"mobileNumber" : "91854xx3x3xx",
"uuid" : "5b3b32eb67bfe0.55083386"
} ]
} ]
The following is the cloud function code.
以下是云功能代码。
exports.processMessageQueue = functions.database.ref('/messageQueue/{pushId}')
.onWrite((change, context) => {
var updateMessage = {};
for (var messageCounter in change.after.val()) {
var toMobileNumbers = change.after.child(messageCounter).child("to").val();
var message = change.after.child(messageCounter).child("m").val();
var messageFromID = change.after.child(messageCounter).child("fromId").val();
var messageTime = Date.now() * -1;
var messageFromName = change.after.child(messageCounter).child('fromName').val();
for (var toCounter in toMobileNumbers) {
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/chatThread/${messageFromID}/${messageTime}`] = message;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/fromName`] = messageFromName;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/message`] = message;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/time`]=messageTime;
// console.log(`To(${toCounter})`,`${toMobileNumbers[toCounter]}`);
var OSmessage = {
app_id: "xxxx",
contents: {"en": message},
headings:{"en":messageFromName},
data:{"uuid":toMobileNumbers[toCounter].uuid},
filters: [
{"field":"tag","key": `M${toMobileNumbers[toCounter].mobileNumber}`, "relation": "exists"}
]
};
console.log(OSmessage);
sendNotification(OSmessage);
}
// console.log("UM",updateMessage);
}
// console.log("UMM",updateMessage);
//do not fire when it's deleted
if (change.after.exists()) {
admin.database().ref().update(updateMessage)
.then(()=>change.after.ref.remove())
.then(()=>console.log(`M:${messageFromName} - ${message}`)).catch(function(error) {
alert("Data could not be updated." + error);
});
}
return "Completed!";
});
The sendNotification(OSmessage);
function I'm calling in the function is working fine and it works everytime a write event is happened. But the following code doesnot work everytime. It works more than 95% of the time. I'm not even getting a catched error in the log.
sendNotification(OSmessage);函数我在函数中调用工作正常,每次发生写事件时它都有效。但是下面的代码每次都不起作用。它的工作时间超过95%。我甚至没有在日志中收到错误信息。
var sendNotification = function(data) {
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Basic NGY5YTE4ODgtNWI"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
console.log("OneSignallingx:",data['filters'][0]['key']);
var https = require('https');
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log("OneSignalling Back",JSON.parse(data));
});
});
req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
});
req.write(JSON.stringify(data));
req.end();
};
1 个解决方案
#1
2
Try this way:
试试这种方式:
exports.processMessageQueue = functions.database.ref('/messageQueue/{pushId}').onWrite((change, context) => {
if (change.after.exists()) {
const promises=[];
var updateMessage = {};
for (var messageCounter in change.after.val()) {
var toMobileNumbers = change.after.child(messageCounter).child("to").val();
var message = change.after.child(messageCounter).child("m").val();
var messageFromID = change.after.child(messageCounter).child("fromId").val();
var messageTime = Date.now() * -1;
var messageFromName = change.after.child(messageCounter).child('fromName').val();
for (var toCounter in toMobileNumbers) {
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/chatThread/${messageFromID}/${messageTime}`] = message;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/fromName`] = messageFromName;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/message`] = message;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/time`] = messageTime;
var OSmessage = {
app_id: "xxxx",
contents: { "en": message },
headings: { "en": messageFromName },
data: { "uuid": toMobileNumbers[toCounter].uuid },
filters: [
{ "field": "tag", "key": `M${toMobileNumbers[toCounter].mobileNumber}`, "relation": "exists" }
]
};
promises.push(sendNotification(OSmessage));
}
}
promises.push(admin.database().ref().update(updateMessage));
promises.push(change.after.ref.remove());
return Promise.all(promises);
}
return null;
});
var sendNotification = function(data) {
return new Promise((resolve, reject) => {
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Basic NGY5YTE4ODgtNWI"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
var https = require('https');
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log("OneSignalling Back", JSON.parse(data));
});
});
req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
});
req.write(JSON.stringify(data));
req.end();
req.on('end', function(e) {
resolve();
});
})
};
#1
2
Try this way:
试试这种方式:
exports.processMessageQueue = functions.database.ref('/messageQueue/{pushId}').onWrite((change, context) => {
if (change.after.exists()) {
const promises=[];
var updateMessage = {};
for (var messageCounter in change.after.val()) {
var toMobileNumbers = change.after.child(messageCounter).child("to").val();
var message = change.after.child(messageCounter).child("m").val();
var messageFromID = change.after.child(messageCounter).child("fromId").val();
var messageTime = Date.now() * -1;
var messageFromName = change.after.child(messageCounter).child('fromName').val();
for (var toCounter in toMobileNumbers) {
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/chatThread/${messageFromID}/${messageTime}`] = message;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/fromName`] = messageFromName;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/message`] = message;
updateMessage[`/messaging/${toMobileNumbers[toCounter].mobileNumber}/inbox/${messageFromID}/time`] = messageTime;
var OSmessage = {
app_id: "xxxx",
contents: { "en": message },
headings: { "en": messageFromName },
data: { "uuid": toMobileNumbers[toCounter].uuid },
filters: [
{ "field": "tag", "key": `M${toMobileNumbers[toCounter].mobileNumber}`, "relation": "exists" }
]
};
promises.push(sendNotification(OSmessage));
}
}
promises.push(admin.database().ref().update(updateMessage));
promises.push(change.after.ref.remove());
return Promise.all(promises);
}
return null;
});
var sendNotification = function(data) {
return new Promise((resolve, reject) => {
var headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": "Basic NGY5YTE4ODgtNWI"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
var https = require('https');
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log("OneSignalling Back", JSON.parse(data));
});
});
req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
});
req.write(JSON.stringify(data));
req.end();
req.on('end', function(e) {
resolve();
});
})
};