Skip to content

Commit

Permalink
Firebase Functions Updated
Browse files Browse the repository at this point in the history
-(change, context) instead of event.
-Sanitized the notif text as well.
  • Loading branch information
divya21raj committed Sep 22, 2018
1 parent b6f9e77 commit ddf968a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 43 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ android {
applicationId 'garbagecollectors.com.unipool'
minSdkVersion 16
targetSdkVersion 27
versionCode 124
versionName "1.24"
versionCode 125
versionName "1.25"
resConfigs "en"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import garbagecollectors.com.unipool.R;
import garbagecollectors.com.unipool.activities.MessageListActivity;
import garbagecollectors.com.unipool.application.Globals;
import garbagecollectors.com.unipool.application.UtilityMethods;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService
{
Expand All @@ -23,8 +24,8 @@ public void onMessageReceived(RemoteMessage remoteMessage)

Log.d("D2R", remoteMessage.getNotification().getBody());

String notificationTitle = remoteMessage.getNotification().getTitle();
String notificationBody = remoteMessage.getNotification().getBody();
String notificationTitle = UtilityMethods.sanitizeName(remoteMessage.getNotification().getTitle());
String notificationBody = UtilityMethods.sanitizeName(remoteMessage.getNotification().getBody());

String clickAction = remoteMessage.getNotification().getClickAction();

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<string name="currentLoc">Getting your current location…</string>

<string name="version">v1.23</string>
<string name="version">v1.25</string>
<string name="create_entry_button">CREATE ENTRY</string>
<string name="uni_name">Shiv Nadar University, NH91, Tehsil Dadri, Gautam Buddha Nagar, Greater Noida, Uttar Pradesh</string>

Expand Down
60 changes: 22 additions & 38 deletions notificationFunctions/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ var db = admin.database();
var body;

exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
.onWrite(event =>
.onWrite((change, context) =>
{
console.log('deleteExpired function called!');

if(!event.data.val())
if(!change.after.val())
{
return console.log('Key deleted!');
}
Expand All @@ -29,10 +29,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
var ISTOffset = 330; // IST offset UTC +5:30
var ISTTime = new Date(currentDate.getTime() + (ISTOffset + currentOffset)*60000);

tripEntryRef.once('value', function(entrySnapshot)
{
entrySnapshot.forEach(function(entryChildSnapshot)
{
tripEntryRef.once('value', (entrySnapshot) => {
entrySnapshot.forEach((entryChildSnapshot) => {
var childKey = entryChildSnapshot.key;
console.log(`key = ${childKey}`);

Expand All @@ -59,10 +57,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')

//remove tripEntry from user
var userEntryRef = db.ref(`users/${childData.user_id}/userTripEntries`);
userEntryRef.once('value', function(userEntrySnap)
{
userEntrySnap.forEach(function(userEntryChildSnap)
{
userEntryRef.once('value', (userEntrySnap) => {
userEntrySnap.forEach((userEntryChildSnap) => {
if(userEntryChildSnap.val().entry_id === childKey)
{
console.log(`DELETE USERENTRY ${childKey}`);
Expand All @@ -76,10 +72,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
userReceivedReqRef.child(childKey).remove();

//remove sentRequest for other users
userRef.once('value', function(userSnap)
{
userSnap.forEach(function(userChildSnap)
{
userRef.once('value', (userSnap) => {
userSnap.forEach((userChildSnap) => {
console.log(`Deleting for ${userChildSnap.key}...`);
var userSentReqRef = db.ref(`users/${userChildSnap.key}/requestSent`);
console.log(`${userSentReqRef}`);
Expand All @@ -96,10 +90,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
});

//remove pairUps
pairUpRef.once('value', function(pairUpSnapshot)
{
pairUpSnapshot.forEach(function(pairUpChildSnapshot)
{
pairUpRef.once('value', (pairUpSnapshot) => {
pairUpSnapshot.forEach((pairUpChildSnapshot) => {
var pairUpChildData = pairUpChildSnapshot.val();

var puKey = pairUpChildData.pairUpId;
Expand Down Expand Up @@ -135,10 +127,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
//remove pairUp from both users
//removing from creator
var creatorPuRef = db.ref(`users/${creatorId}/pairUps`);
creatorPuRef.once('value', function(creatorPuSnap)
{
creatorPuSnap.forEach(function(creatorPuChildSnap)
{
creatorPuRef.once('value', (creatorPuSnap) => {
creatorPuSnap.forEach((creatorPuChildSnap) => {
if(creatorPuChildSnap.val().pairUpId === puKey)
{
console.log(`${puKey} to be removed from ${creatorId}`);
Expand All @@ -150,10 +140,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
//reomove pairUp from both users
//reomving from requester
var requesterPuRef = db.ref(`users/${requesterId}/pairUps`);
requesterPuRef.once('value', function(requesterPuSnap)
{
requesterPuSnap.forEach(function(requesterPuChildSnap)
{
requesterPuRef.once('value', (requesterPuSnap) => {
requesterPuSnap.forEach((requesterPuChildSnap) => {
if(requesterPuChildSnap.val().pairUpId === puKey)
{
console.log(`${puKey} to be removed from ${requesterId}`);
Expand All @@ -165,10 +153,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
//remove messages from both users
//removing from creator
var creatorMessageRef = db.ref(`messages/${creatorId}`);
creatorMessageRef.once('value', function(creatorMessageSnap)
{
creatorMessageSnap.forEach(function(creatorMessageChildSnap)
{
creatorMessageRef.once('value', (creatorMessageSnap) => {
creatorMessageSnap.forEach((creatorMessageChildSnap) => {
if(creatorMessageChildSnap.val().pairUpId === puKey)
{
creatorMessageRef.child(creatorMessageChildSnap.key).remove();
Expand All @@ -179,10 +165,8 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
//remove messages from both users
//removing from requester
var requesterMessageRef = db.ref(`messages/${requesterId}`);
requesterMessageRef.once('value', function(requesterMessageSnap)
{
requesterMessageSnap.forEach(function(requesterMessageChildSnap)
{
requesterMessageRef.once('value', (requesterMessageSnap) => {
requesterMessageSnap.forEach((requesterMessageChildSnap) => {
if(requesterMessageChildSnap.val().pairUpId === puKey)
{
requesterMessageRef.child(requesterMessageChildSnap.key).remove();
Expand All @@ -203,14 +187,14 @@ exports.deleteExpired = functions.database.ref('/deleteExpired/{user_id}')
});

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}')
.onWrite(event =>
.onWrite((change, context) =>
{
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have a notif to send to : ', user_id);

if(!event.data.val())
if(!change.after.val())
{
return console.log('A notif has been deleted from the DB : ', notification_id);
}
Expand Down

0 comments on commit ddf968a

Please sign in to comment.