'Subscribing to firebase topic do not filter push message
Context
I am developping an Angular web application using firebase messaging.
I configured firebase successfully (I guess) and I now subscribe to a "users" topic.
To do so I use the subscribeToTopic('users') from 'firebase-admin' library on server side (NestJS application) and it always prints the success message.
await firebaseAdmin
.messaging()
.subscribeToTopic(firebaseIdToken, topic)
.then((response) => {
console.log('Successfully subscribed to topic:', response);
response.errors.forEach((err) => console.log(err.error));
})
.catch((error) => {
console.log('Error subscribing to topic:', error);
});
On client side, I use AngularFireMessaging.messages to listen the message push.
To test the push message, I use postman and the firebase api https://fcm.googleapis.com/fcm/send
with following body
{
"topic": "users",
"notification": {
"title": "notif title",
"body": "Hello world"
},
"data": {
"action": "CREATE",
},
"to": "{{firebase.id_token}}"
}
where firebase.id_token is the id token generated by firebase with
AngularFireMessaging.getToken function
I successfully receive the push message on my client app.
Issue 1
But now, if I change the "topic" key in the request body by any topic name, I still receive the push messages on client side !! And this is not what I expected I wonder what I'm doing wrong with the subscription or configuration making the client side not filtering the push messages according to topic subscription.
Issue 2
I tryied to implement my own sendPush method on server side with the following code
@Post('send-push')
async sendPushMessage(@Body() { topic, payload }: FirebasePush): Promise<void> {
console.log('sendPushMessage', topic, payload); // print the right values
await firebaseAdmin
.messaging()
.sendToTopic(topic, payload)
.then((response) => {
console.log('Successfully sent message:', response); // this is printed with the message id
})
.catch((error) => {
console.log('Error sending message:', error);
});
}
if I call this endpoint with postman using the following body
{
"topic": "users",
"payload": {
"data": {
"action": "CREATE",
}
}
}
the message seems to be send correctly because the message id is printed in the console but I never receive any message push on client side and I wonder why as well
I hope someone can help me with theses issues, thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
