-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (76 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
admin.initializeApp();
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'yashbhange888@gmail.com',
pass: 'siddharth'
}
});
exports.email = functions.database.ref('/')
.onUpdate((snapshot, context) => {
const After1 = snapshot.after.val();
var Before = snapshot.before.val().user1.level;
var After = After1.user1.level;
var message1 = {
to: After1.profile.email,
from: 'yashbhange888@gmail.com',
sender: 'Team Auto-Life',
subject: " ALERT !!! ",
html: '<span> Hello </span>' +
After1.profile.name + '<span>,<span>' + '<br/><br/><br/>' + '<hr>' + '<br/>' +
'<span>Moisture Level : </span>' + After1.user1.moisture + '<br/><br/>' +
'<span>Status      : </span>' + After1.user1.status + '<br/><br/>' +
'<span>Mode        : </span>' + After1.user1.mode + '<br/><br/>' + '<hr>' +
'<br/><br/><br/><br/><br/>' + '<p align="right"> <b>From</b>      </p>' + '<p align="right"> <b>Team Auto-Life</b></p>'
};
if (Before !== After) {
return transporter.sendMail(message1, function(erro, info) {
if (erro) {
console.log(erro);
} else {
console.log("email sent successfully" + info.response);
}
});
} else {
return console.log("email else part executed");
}
});
exports.notification = functions.database.ref('/user1')
.onUpdate((snapshot, context) => {
var After1 = snapshot.after.val();
const temptoken = After1.token;
var Before = snapshot.before.val().level;
var After = After1.level;
var msgbody = "Moisture Level " + After + " Threshold";
var message = {
notification: {
title: ' AlERT !!! ',
body: msgbody
},
token: temptoken
};
if (Before !== After) {
admin.messaging().send(message).then((response) => {
console.log("Message sent successfully:", response);
return response;
})
.catch((error) => {
console.log("Error sending message: ", error);
});
} else {
return console.log("Notification else part");
}
});
exports.addMessage = functions.https.onRequest(async(req, res) => {
// Grab the text parameter.
//const original = req.query.text;
// Push the new message into Cloud Firestore using the Firebase Admin SDK.
//const writeResult = await admin.firestore().collection('messages').add({ original: original });
// Send back a message that we've succesfully written the message
// res.json({ result: `Message with ID: ${writeResult.id} added.` });
console.log("3rd fucntion call");
res.end();
});