Skip to content

Commit

Permalink
fixed messages fixed devtokengeneration and
Browse files Browse the repository at this point in the history
deletion
  • Loading branch information
roranmorea committed Feb 14, 2020
1 parent c36d582 commit 842a328
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 207 deletions.
3 changes: 2 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
],
"source": "functions"
}
}
74 changes: 51 additions & 23 deletions functions/package-lock.json

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

70 changes: 70 additions & 0 deletions functions/src/cloud_Functions/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { DocumentSnapshot } from "@google-cloud/firestore";

const db = admin.firestore();

export class Messages {
async uploadMessage(data: any, context: functions.https.CallableContext) {
return db
.collection("messages")
.doc()
.set(data.message)
.then(response => console.log(response));
}

async sendNotificationForMessage(
data: any,
context: functions.https.CallableContext
) {
const title: string = data.title;
const snippet: string = data.snippet;
const receivers: string[] = data.receivers;
let registrationTokens: string[] = [];
for (let receiver of receivers) {
const group: DocumentSnapshot = await db
.collection("groups")
.doc(receiver)
.get();
const priviledgedList: string[] = Object.keys(
group.get("Priviledge")
);
for (let uid of priviledgedList) {
const userMap: DocumentSnapshot = await db
.collection("user")
.doc(uid)
.get();
const test = userMap.get("devtoken");
if (test !== undefined) {
const devtokensObject: { [key: string]: any } = test;
for (let token of Object.values(devtokensObject)) {
registrationTokens.push(token);
}
}
}
}
const message = {
notification: { title: title, body: snippet },
priority: "high",
data: { click_action: "FLUTTER_NOTIFICATION_CLICK" },
tokens: registrationTokens
};
console.log(registrationTokens);
return admin
.messaging()
.sendMulticast(message)
.then(response => {
if (response.failureCount > 0) {
const failedTokens: string[] = [];
response.responses.forEach((resp, idx) => {
if (!resp.success) {
failedTokens.push(registrationTokens[idx]);
}
});
console.log(
"List of tokens that caused failures: " + failedTokens
);
}
});
}
}
Loading

0 comments on commit 842a328

Please sign in to comment.