@@ -19,60 +19,6 @@ const requestNotificationPermission = async () => {
1919 console . log ( 'Permission Status' , { authStatus, isEnabled } ) ;
2020} ;
2121
22- messaging . 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- }
27- const messageId = remoteMessage . data ?. id as string ;
28- if ( ! messageId ) {
29- return ;
30- }
31- const config = await AsyncStore . getItem < LoginConfig | null > (
32- '@stream-rn-sampleapp-login-config' ,
33- null ,
34- ) ;
35- if ( ! config ) {
36- return ;
37- }
38-
39- const client = StreamChat . getInstance ( config . apiKey ) ;
40-
41- const user = {
42- id : config . userId ,
43- image : config . userImage ,
44- name : config . userName ,
45- } ;
46-
47- await client . _setToken ( user , config . userToken ) ;
48- const message = await client . getMessage ( messageId ) ;
49-
50- // create the android channel to send the notification to
51- const channelId = await notifee . createChannel ( {
52- id : 'chat-messages' ,
53- name : 'Chat Messages' ,
54- } ) ;
55-
56- if ( message . message . user ?. name && message . message . text && ! remoteMessage . notification ) {
57- const { stream, ...rest } = remoteMessage . data ?? { } ;
58- const data = {
59- ...rest ,
60- ...( ( stream as unknown as Record < string , string > | undefined ) ?? { } ) , // extract and merge stream object if present
61- } ;
62- await notifee . displayNotification ( {
63- android : {
64- channelId,
65- pressAction : {
66- id : 'default' ,
67- } ,
68- } ,
69- title : 'New message from ' + message . message . user . name ,
70- body : message . message . text ,
71- data,
72- } ) ;
73- }
74- } ) ;
75-
7622const requestAndroidPermission = async ( ) => {
7723 if ( Platform . OS === 'android' && Platform . Version >= 33 ) {
7824 const result = await PermissionsAndroid . request (
@@ -148,7 +94,6 @@ export const useChatClient = () => {
14894 } ) ;
14995 // show notifications when on foreground
15096 const unsubscribeForegroundMessageReceive = messaging . onMessage ( async ( remoteMessage ) => {
151- const notificationType = remoteMessage . data ?. type ;
15297 const { stream, ...rest } = remoteMessage . data ?? { } ;
15398 const data = {
15499 ...rest ,
@@ -158,41 +103,24 @@ export const useChatClient = () => {
158103 id : 'foreground' ,
159104 name : 'Foreground Messages' ,
160105 } ) ;
161- if ( notificationType === 'message.new' ) {
162- const messageId = remoteMessage . data ?. id ;
163- if ( ! messageId ) {
164- return ;
165- }
166- const message = await client . getMessage ( messageId as string ) ;
167- if ( message . message . user ?. name && message . message . text ) {
168- // create the android channel to send the notification to
169- // display the notification on foreground
170- await notifee . displayNotification ( {
171- android : {
172- channelId,
173- pressAction : {
174- id : 'default' ,
175- } ,
176- } ,
177- body : message . message . text ,
178- title : 'New message from ' + message . message . user . name ,
179- data,
180- } ) ;
181- }
182- } else {
183- if ( data . body && data . title ) {
184- await notifee . displayNotification ( {
185- android : {
186- channelId,
187- pressAction : {
188- id : 'default' ,
189- } ,
106+ // create the android channel to send the notification to
107+ // display the notification on foreground
108+ const notification = remoteMessage . notification ?? { } ;
109+ const body = ( data . body ?? notification . body ) as string ;
110+ const title = ( data . title ?? notification . title ) as string ;
111+
112+ if ( body && title ) {
113+ await notifee . displayNotification ( {
114+ android : {
115+ channelId,
116+ pressAction : {
117+ id : 'default' ,
190118 } ,
191- body : data . body as string ,
192- title : data . title as string ,
193- data ,
194- } ) ;
195- }
119+ } ,
120+ body ,
121+ title ,
122+ data ,
123+ } ) ;
196124 }
197125 } ) ;
198126
0 commit comments