Skip to content

Commit

Permalink
Automated Push
Browse files Browse the repository at this point in the history
  • Loading branch information
samarthagarwal committed Jun 29, 2018
1 parent 67fe238 commit a27d2e4
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 13 deletions.
1 change: 1 addition & 0 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@
<plugin name="cordova-plugin-camera" spec="~4.0.3" />
<engine name="ios" spec="4.5.4" />
<plugin name="cordova-plugin-firebase" spec="^1.0.5" />
<engine name="android" spec="~7.0.0" />
</widget>
46 changes: 41 additions & 5 deletions functions/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion functions/lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 47 additions & 4 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,46 @@ import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

// Start writing Firebase Functions
// https://firebase.google.com/docs/functions/typescript
const sendNotification = (owner_uid, type) => {

return new Promise((resolve, reject) => {
return admin.firestore().collection("users").doc(owner_uid).get().then((doc) => {
if(doc.exists && doc.data().token){

if(type === "new_comment"){
admin.messaging().sendToDevice(doc.data().token, {
data: {
title: "A new comment has been made on your post.",
sound: "default",
body: "Tap to Check"
}
}).then((sent) => {
resolve(sent)
}).catch((err) => {
reject(err)
})
} else if(type === "new_like"){
admin.messaging().sendToDevice(doc.data().token, {
data: {
title: "Someone liked your post on Feedly.",
sound: "default",
body: "Tap to Check"
}
}).then((sent) => {
resolve(sent)
}).catch((err) => {
reject(err)
});
}

}
})
})




}

export const updateLikesCount = functions.https.onRequest((request, response) => {

Expand All @@ -29,7 +67,12 @@ export const updateLikesCount = functions.https.onRequest((request, response) =>
updateData[`likes.${userId}`] = false;
}

admin.firestore().collection("posts").doc(postId).update(updateData).then(() => {
admin.firestore().collection("posts").doc(postId).update(updateData).then(async () => {

if(action == "like"){
await sendNotification(data.data().owner, "new_like");
}

response.status(200).send("Done")
}).catch((err) => {
response.status(err.code).send(err.message);
Expand All @@ -56,7 +99,7 @@ export const updateCommentsCount = functions.firestore.document('comments/{comme
"commentsCount": commentsCount
})

return true;
return sendNotification(doc.data().owner, "new_comment");;

} else {
return false;
Expand Down
214 changes: 214 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a27d2e4

Please sign in to comment.