Skip to content

Commit

Permalink
Push Notifications (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeargie1238 authored Aug 28, 2024
1 parent 6c93c7e commit 8467c45
Show file tree
Hide file tree
Showing 14 changed files with 2,503 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "the-heart-center-app"
}
}
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id "com.android.application"
id 'com.google.gms.google-services'
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}
Expand Down
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pluginManagement {
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "com.google.gms.google-services" version "4.3.15" apply false
}

include ":app"
41 changes: 41 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"flutter": {
"platforms": {
"android": {
"default": {
"projectId": "the-heart-center-app",
"appId": "1:902613553839:android:b3cb24b376a4cfc264ba5a",
"fileOutput": "android/app/google-services.json"
}
},
"ios": {
"default": {
"projectId": "the-heart-center-app",
"appId": "1:902613553839:ios:4724e712e57cda2864ba5a",
"uploadDebugSymbols": false,
"fileOutput": "ios/Runner/GoogleService-Info.plist"
}
},
"macos": {
"default": {
"projectId": "the-heart-center-app",
"appId": "1:902613553839:ios:4724e712e57cda2864ba5a",
"uploadDebugSymbols": false,
"fileOutput": "macos/Runner/GoogleService-Info.plist"
}
}
}
},
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
}
]
}
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
82 changes: 82 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Import function triggers from their respective submodules:
*
* const {onCall} = require("firebase-functions/v2/https");
* const {onDocumentWritten} = require("firebase-functions/v2/firestore");
*
* See a full list of supported triggers at https://firebase.google.com/docs/functions
*/

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started

exports.sendEventNotifications = functions.pubsub.schedule('every 10 minutes').onRun(async (context) => {
const now = admin.firestore.Timestamp.now();
const oneHourLater = new Date(now.toDate().getTime() + 60 * 60 * 1000);
const eventsRef = db.collection('scheduled_streams');

const snapshot = await eventsRef
.where('date', '<=', oneHourLater)
.where('date', '>=', now)
.get();

if (snapshot.empty) {
console.log('No upcoming events found');
return null;
}

const messages = [];
snapshot.forEach(async doc => {
const event = doc.data();
const eventId = doc.id;
const signupsRef = eventsRef.doc(eventId).collection('signups');
const signupSnapshot = await signupsRef.get();

if(!signupSnapshot.empty){
signupSnapshot.forEach(async userDoc => {
const signupData = userDoc.data();
const notified = signupData.notified;
const signupId = userDoc.id;
const userRef = db.collection('users').doc(signupId);
const userSnapshot = await userRef.get();
if (!userSnapshot.empty) {
const fcmToken = userSnapshot.data().fcmToken;
const notify = userSnapshot.data().notify;
if (fcmToken && notify === true && (notified === false || notified === undefined)) {
const message = {
notification: {
title: event.title,
body: `Your event is starting in 1 hour!`,
},
token: fcmToken,
};
messages.push(admin.messaging().send(message));
}
await signupsRef.doc(signupId).update({
notified: true
});
} else {
console.log("No such user found!");
}
});

} else {
console.log('No Signups found');
return null;
}

});

try {
const response = await admin.messaging().send(message);
console.log('Successfully sent message:', response);
} catch (error) {
console.log('Error sending message:', error);
}
return null;
});
Loading

0 comments on commit 8467c45

Please sign in to comment.