Ionic send notifications method FCM

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)
}

Hey there, mind sharing a bit more info on this one? Like what error are you having, how is it not working? etc…

1 Like

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 …

Hmmmm, have you tried this: https://cordova-plugin-fcm.appspot.com/

It’s a testing server, make sure your app can receive notifications first, that way we can isolate the issue better.

1 Like

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. :slight_smile:

2 Likes

100% Support for this.

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.

1 Like

Ok, I had to change the API KEY by the old because it told me that it was unknown, so it worked, but still not from the method!

Thank you very much for the information on security however I do not know how to go about doing this server side …

@javebratt You know how i can do for resolve my problems ?

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.

Are you facing problems receiving notifications in Android or iOS (or both) ?

I can emulate only on android … I receive notification only when i pass with firebase console, when i use method above that doesnt work

I just want help for notification working, not server

What is the response to the post? Please print the output

I have no response, that pass into the method and do nothing visible …

That is not possible. All HTTP requests get a response message. How are you checking for the response? Please post the code.

I don’t know how to see the response

envoieNotif() {

    let headers = new Headers({ 'Authorization': 'key=KEY', 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    let notification = {
      "notification": {
        "title": "Titre",
        "body": "body",
        "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)
    console.log("remarche stp")
    console.log(this.http.post(url, notification, options))
  }

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:

  1. HTTP operations are asynchronous. That means the response is not returned immediately. Read up on subscribe for HTTP (google for “ionic2 HTTP response”)

  2. 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.

try this:

this.http.post(url, notification, options)
.subscribe(resp => {
console.log(resp);
});

Hi, finally! whether you got this and working?