Skip to content

Commit

Permalink
Merge pull request #28 from jphacks/feat/notifications
Browse files Browse the repository at this point in the history
プッシュ通知の処理
  • Loading branch information
Inlet-back authored Oct 26, 2024
2 parents 4ad28c5 + 0e1cf25 commit 50ce4b1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
46 changes: 43 additions & 3 deletions firebase/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,53 @@
* See a full list of supported triggers at https://firebase.google.com/docs/functions
*/

import {onRequest} from "firebase-functions/v2/https";
import { onRequest } from "firebase-functions/v2/https";
import * as functions from "firebase-functions/v2";
import * as logger from "firebase-functions/logger";

import * as admin from "firebase-admin";
import type { DocumentReference } from "firebase-admin/firestore";
// Start writing functions
// https://firebase.google.com/docs/functions/typescript

admin.initializeApp();

export const helloWorld = onRequest((request, response) => {
logger.info("Hello logs!", {structuredData: true});
logger.info("Hello logs!", { structuredData: true });
response.send("Hello from Firebase!");
});

export const pushNotification = functions.scheduler
.onSchedule("every 1 minutes", async () => {
const now = (() => {
let s = admin.firestore.Timestamp.now().seconds
s = s - s % 60
return new admin.firestore.Timestamp(s, 0)
})()
// リマインダーコレクションから現在時刻のリマインダーをクエリ
const db = admin.firestore()
const notifications = await Promise.all((await db.collection('notifications')
.where('datetime', '==', now)
.where("type", "==", "push")
.get())
.docs
.map(async doc => {
const event = await (doc.get("eventOrTaskRef") as DocumentReference).get();
const tokens = await db.collection("users").doc(doc.get("userId")).get().then(doc => doc.get("fcm-tokens")) as string[];
return {
title: event.get("title"),
description: event.get("description"),
tokens
}
}));

const messaging = admin.messaging();
for (const notification of notifications) {
await messaging.sendEachForMulticast({
tokens: notification.tokens,
notification: {
title: notification.title,
body: notification.description
}
})
}
});
3 changes: 2 additions & 1 deletion task_yell/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export interface Event {
importance: "high" | "medium" | "low";
location: FirebaseFirestore.GeoPoint;
reccurence: string[];
notifications: Notification[];
}

export interface Notification {
datetime: Date;
type: "call" | "push";
eventOrTaskRef: string;
userId: string;
}

0 comments on commit 50ce4b1

Please sign in to comment.