How to send chat notifications ionic +4 firebase

I have a chat app that save messages in an array in firebase but I want to generate notifications with every message sent.

I already did push notifications from firebase console with cordova and capacitor, but I need to send them from my app.

I need help, I have almost a week trying to do this…

hola Jorge and welcome to the Ionic community!

could it be that your app can call a firebase function that will ultimately send the notifications from the cloud?

Saludos,
Gustavo.

¿Hablas español?

I don’t know how to do that function, I think it can be with http but I don’t really know

the structure in Node is like this:

var message = {

        to: 'userToken', 
        collapse_key: 'storekey',
        
        notification: {
            title: 'Hi', 
            body: 'Notification from node',
            click_action: 'FCM-PLUGIN-ACTIVITY'             
        },
        
        data: { 
            user: 'name',
            email: 'name@gmail.com'
        }
    };

so in Ionic it must be something like that but with http request and response, I don’t know

hola,
I never did something like that but I would try my luck with your “tocayo” Jorge Vergara.
His site is here and I’m hoping he wrote about firebase and push notifications.
I found this one but I think is the other way of what you need, perhaps?

Buena suerte,
Gustavo.
PS: si, hablo español :argentina:

EDIT: can this one help?

1 Like

I solved already with this function:

send(){
    let topic = {
    "to": "/topics/yourtopic",
    "notification": {
    "title": "Breaking News",
    "body": "New news story available."
     }

    let token = {
    "to": "token",
    "notification": {
    "title": "Hola",
    "body": "New news story available."
    }
    }
    fetch("https://fcm.googleapis.com/fcm/send", {
    method: 'POST',
    body: JSON.stringify(token),
    headers:{
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + "key"
    }
    }).then(res => res.json())
    .catch(error => console.error('Error:', error))
    .then(response => console.log('Success:', response));
  }

que buena noticia!
Saludos
Gustavo.