-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed messages fixed devtokengeneration and
deletion
- Loading branch information
1 parent
c36d582
commit 842a328
Showing
5 changed files
with
416 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.