Good day folks, it is my first publication on the site, I apologize if I do not comply with all the publication rules because I do not know them in detail.
I will comment on my problem .. I have an app with ionic 3 and angular, which works with notifications between users when an action arises, I researched through the firebase forums and several other places and tried several codes but I could not fix it. The application sends data and notification to a specific token, the device registered by the token receives the data but not the notification. Not when it is with the application open, or when it is in the background or closed. This is the code you use:
1) To receive the token
this.fcm.getToken().then(token => {
alert('token' + JSON.stringify(token))
//Aquí guardo el token que recibo del dispositivo
})
This works .. I can get the device token.
2) To receive notification:
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
alert("Received in background")
} else {
alert("Received in foreground")
};
})
This also works, when I sent a notification from the firebase console I get it perfectly .. The problem is when I sent it from the server
3) Code for notification:
SendNotific()
{
let keytoken = 'fqN5yelN7gk:APA91bGT0......'
let keyserver = 'AAAA4oCrTNk:APA91bHLBWcyQimF....'
let headers = { 'Authorization': 'key='+keyserver,
'Content-Type': 'application/json' };
let url = 'https://fcm.googleapis.com/fcm/send';
let body = {
"notification":{
"title":"Notification title",
"body":"Notification body"
},
"data":{
"key1": "value1",
"key2": "value2"
},
"to" : keytoken,
"priority": "high",
"content_available": true
}
this.http.post(url, body, headers).then(data=>
{
alert(JSON.stringify(data))
}).then(error=>{
alert('error'+JSON.stringify(error))
})
}
If the application is in the foreground of the device , the alert becomes visible, that is, I receive the data that I sent in "Data". (For this case it would be "key1" and "key2") but I do not receive any notification. If the application is in the background or closed no notification is shown, and when I open the app again the alert messages are shown with the "Data" that arrived ..
As I said before, if you sent a notification from the firebase console works perfectly ..
Clearly I'm missing some data that I can not find it .. I saw several publications that talk about the same thing but I can not give the solution ..
Will I be missing specific data within the notification so I can be visible?
Does the user need to accept any permission to receive notification?
Since thank you very much and sorry if I did not know how to explain clearly
regards,
Julián.