@@ -4,6 +4,7 @@ import { fileURLToPath } from "url";
44import fs from "fs" ;
55import path from "path" ;
66import RealtimeFeedUtilsV3 from "./RealtimeFeedUtilsV3.js" ;
7+ import LogUtils from "./LogUtils.js" ;
78
89const __filename = fileURLToPath ( import . meta. url ) ;
910const __dirname = path . dirname ( __filename ) ;
@@ -59,54 +60,86 @@ function deleteDelayNotification(tripID, stopID, deviceToken) {
5960
6061function sendNotifications ( ) {
6162 const rtfData = RealtimeFeedUtilsV3 . getRTFData ( ) ;
62-
63+
6364 if ( ! rtfData ) {
64- // no real-time data available yet
6565 return ;
6666 }
67-
67+
68+ const tokensToDelete = [ ] ;
69+
6870 for ( const id in rtfData ) {
6971 if ( id in notifRequests ) {
7072 for ( const stopID in notifRequests [ id ] ) {
7173 if ( stopID in rtfData [ id ] [ "stopUpdates" ] ) {
72- for ( const deviceToken of notifRequests [ id ] [ stopID ] ) {
73- sendNotification (
74- deviceToken ,
75- `The bus on ${ rtfData [ id ] [ "routeId" ] } is delayed` ,
76- "testBody"
77- ) ;
74+ //only send a notification if there is a delay
75+ if ( rtfData [ id ] [ "stopUpdates" ] [ stopID ] > 0 ) {
76+ for ( const deviceToken of notifRequests [ id ] [ stopID ] ) {
77+ const notifData = {
78+ title : "Delay Notification" ,
79+ body : `The bus on ${ rtfData [ id ] [ "routeId" ] } is delayed` ,
80+ } ;
81+
82+ sendNotification ( deviceToken , notifData ) ;
83+
84+ tokensToDelete . push ( { id, stopID, deviceToken } ) ;
85+ }
7886 }
7987 }
8088 }
8189 }
8290 }
91+
92+ for ( const { id, stopID, deviceToken } of tokensToDelete ) {
93+ if (
94+ notifRequests [ id ] &&
95+ notifRequests [ id ] [ stopID ] &&
96+ Array . isArray ( notifRequests [ id ] [ stopID ] )
97+ ) {
98+ notifRequests [ id ] [ stopID ] = notifRequests [ id ] [ stopID ] . filter (
99+ ( token ) => token !== deviceToken
100+ ) ;
101+ if ( notifRequests [ id ] [ stopID ] . length === 0 ) {
102+ delete notifRequests [ id ] [ stopID ] ;
103+ }
104+ }
105+ }
106+
83107 saveNotifs ( ) ;
84108}
85109
86- function sendNotification ( deviceToken , data , notification ) {
110+ async function sendNotification ( deviceToken , notif ) {
87111 const message = {
88- data : {
89- data,
90- notification,
91- } ,
92112 token : deviceToken ,
113+ notification : {
114+ title : notif . title ,
115+ body : notif . body ,
116+ } ,
93117 } ;
118+
94119 if ( ! message . token ) {
95120 throw new Error ( "Invalid device token" ) ;
96121 }
97- getMessaging ( )
98- . send ( message )
99- . then ( ( response ) => {
100- console . log ( response ) ;
101- } ) ;
122+
123+ try {
124+ const response = await getMessaging ( )
125+ . send ( message )
126+ . then ( ( response ) => {
127+ LogUtils . log ( { message : response } ) ;
128+ console . log ( response ) ;
129+ } ) ;
130+
131+ console . log ( "Notification sent successfully:" , response ) ;
132+ } catch ( error ) {
133+ console . error ( "Error sending notification:" , error . code , error . message ) ;
134+ }
102135}
103136
104137function waitForDeparture ( deviceToken , startTime ) {
105138 const startDate = new Date ( parseInt ( startTime ) * 1000 - 60000 * 10 ) ;
106139
107140 const notifData = {
108- data : "You should board your bus in 10 minutes" ,
109- notification : "Bording Reminder" ,
141+ body : "You should board your bus in 10 minutes" ,
142+ title : "Bording Reminder" ,
110143 } ;
111144
112145 const job = schedule . scheduleJob ( startDate , ( ) => {
@@ -121,13 +154,14 @@ function waitForDeparture(deviceToken, startTime) {
121154}
122155
123156function cancelDeparture ( deviceToken , startTime ) {
124- const startDate = new Date ( parseInt ( startTime ) * 1000 - 60000 * 10 ) ;
157+ let startDate = new Date ( parseInt ( startTime ) * 1000 - 60000 * 10 ) ;
158+ startDate = startDate . toString ( ) ;
125159
126160 if ( deviceToken in departures ) {
127161 if ( startDate in departures [ deviceToken ] ) {
128162 if ( departures [ deviceToken ] [ startDate ] ) {
129163 departures [ deviceToken ] [ startDate ] . cancel ( ) ;
130- console . log ( "Job canceled." ) ;
164+ LogUtils . log ( { message : "job canceled" } ) ;
131165 }
132166 }
133167 }
0 commit comments