Hello, I am currently trying to make the notifications work on my application but I can not do it … I have advanced a lot, so I use FCM, I get to receive the notifications by sending them a from firebase but as I try to make a method to sent a notification from the application it does not work … I do not know where it came from … I leave you the code of the method below
Thank you in advance
envoieNotif(){
let headers = new Headers({ 'Authorization': 'key=ID', 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let notification = {
"notification": {
"title": "test",
"body": "Blabla",
"click_action": "FCM_PLUGIN_ACTIVITY",
"sound": "default"
}, "data": {
//OPTIONAL PARAMS
},
"to": "TOKEN",
"priority": "high"
}
let url = 'https://fcm.googleapis.com/fcm/send';
this.http.post(url, notification, options)
}
Okay, so I use this method when I click a button (to test) on my own token but still I do not receive the notification! It does not seem sent … No error, if I put a console log just after that goes well by the method … I’m looking at the import level if I’m not wrong some pars but I do not find of error …
I just want to point out that you should really consider routing these requests through your backend, and not do it directly from the app. You’re exposing some information which can be abused by attackers to send their own notifications.
You can set up Firebase Cloud Functions to listen to database changes and send notifications accordingly, that way all of the notification logic happens server side.
I don’t really know how to help you, I’m not sure if you can even get that working all client side, check Firebase Cloud Functions docs for messaging, you’ll see that the SDK is easy to use and there’s already an example built for it there.
I think the core issue is you are likely just starting out in Ionic/Angular programming. The issue here is probably not related to FCM, but your Angular readiness.
I’m not going to give you direct code answer because if you cut paste that, you will see the error, but likely get stuck in the next step. So I’ll put some thoughts for you to investigate:
HTTP operations are asynchronous. That means the response is not returned immediately. Read up on subscribe for HTTP (google for “ionic2 HTTP response”)
Your code has http.post twice. Do it only once and console.log the returned asynchronous payload
I’d recommend you get to a point where you are able to see a successful or error response via console.log and see if the return message gives any hints.