@@ -20,6 +20,10 @@ const requestNotificationPermission = async () => {
2020} ;
2121
2222messaging . setBackgroundMessageHandler ( async ( remoteMessage ) => {
23+ if ( remoteMessage . data ?. type !== 'message.new' ) {
24+ // handled through the new push system, we don't need this
25+ return ;
26+ }
2327 const messageId = remoteMessage . data ?. id as string ;
2428 if ( ! messageId ) {
2529 return ;
@@ -145,34 +149,51 @@ export const useChatClient = () => {
145149 } ) ;
146150 // show notifications when on foreground
147151 const unsubscribeForegroundMessageReceive = messaging . onMessage ( async ( remoteMessage ) => {
148- const messageId = remoteMessage . data ?. id ;
149- if ( ! messageId ) {
150- return ;
151- }
152- const message = await client . getMessage ( messageId as string ) ;
153- if ( message . message . user ?. name && message . message . text ) {
154- // create the android channel to send the notification to
155- const channelId = await notifee . createChannel ( {
156- id : 'foreground' ,
157- name : 'Foreground Messages' ,
158- } ) ;
159- // display the notification on foreground
160- const { stream, ...rest } = remoteMessage . data ?? { } ;
161- const data = {
162- ...rest ,
163- ...( ( stream as unknown as Record < string , string > | undefined ) ?? { } ) , // extract and merge stream object if present
164- } ;
165- await notifee . displayNotification ( {
166- android : {
167- channelId,
168- pressAction : {
169- id : 'default' ,
152+ const notificationType = remoteMessage . data ?. type ;
153+ const { stream, ...rest } = remoteMessage . data ?? { } ;
154+ const data = {
155+ ...rest ,
156+ ...( ( stream as unknown as Record < string , string > | undefined ) ?? { } ) , // extract and merge stream object if present
157+ } ;
158+ const channelId = await notifee . createChannel ( {
159+ id : 'foreground' ,
160+ name : 'Foreground Messages' ,
161+ } ) ;
162+ if ( notificationType === 'message.new' ) {
163+ const messageId = remoteMessage . data ?. id ;
164+ if ( ! messageId ) {
165+ return ;
166+ }
167+ const message = await client . getMessage ( messageId as string ) ;
168+ if ( message . message . user ?. name && message . message . text ) {
169+ // create the android channel to send the notification to
170+ // display the notification on foreground
171+ await notifee . displayNotification ( {
172+ android : {
173+ channelId,
174+ pressAction : {
175+ id : 'default' ,
176+ } ,
177+ } ,
178+ body : message . message . text ,
179+ title : 'New message from ' + message . message . user . name ,
180+ data,
181+ } ) ;
182+ }
183+ } else {
184+ if ( data . body && data . title ) {
185+ await notifee . displayNotification ( {
186+ android : {
187+ channelId,
188+ pressAction : {
189+ id : 'default' ,
190+ } ,
170191 } ,
171- } ,
172- body : message . message . text ,
173- data,
174- title : 'New message from ' + message . message . user . name ,
175- } ) ;
192+ body : data . body as string ,
193+ title : data . title as string ,
194+ data,
195+ } ) ;
196+ }
176197 }
177198 } ) ;
178199
@@ -194,7 +215,7 @@ export const useChatClient = () => {
194215 try {
195216 if ( userId ) {
196217 await loginUser ( {
197- apiKey : 'yjrt5yxw77ev ' ,
218+ apiKey : 'pd67s34fzpgw ' ,
198219 userId : USERS [ userId ] . id ,
199220 userImage : USERS [ userId ] . image ,
200221 userName : USERS [ userId ] . name ,
0 commit comments