ariesjchang
ariesjchang

Reputation: 301

Sending FCM Push Notification to topic but preventing duplicate notification

I am creating an app in which users can subscribe to multiple topics. Whenever a news article is shared within the app, it is assigned multiple topics. Users receive push notifications whenever a news article that is relevant to a topic they subscribed to is shared.

Say a user subscribed to topics A and B. A news article that belongs to both of these topics (A & B) gets shared. In this case, what happens in my code is,

for(const topic of topics) {
  ...
  admin.messaging().send(message);
  ...
}

So the user will receive two push notifications, one for topic A and one for topic B. Since I'm sending FCM push notifications not to specific devices but to topics, I cannot keep track of who received the push notifications, so I cannot prevent duplicate notifications manually.

Would there be a way for me to make sure the user does not receive the same fcm push notification twice (i.e. receive the push notification only for topicA and not for topic B)?

I am handling both the server-side (TypeScript) and client-side (Flutter), so a solution for either side will be greatly appreciated!

Upvotes: 0

Views: 744

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598668

What you want to use is a single call to send to send to all topics using a condition.

Given your example of topic A and topic B, that'd be:

const condition = '\'stock-GOOG\' in topics || \'industry-tech\' in topics';

const message = ;

admin.messaging().send({
  notification: {
    title: 'Your notification title',
    body: 'Your notification body'
  },
  condition: "'TopicA' in topics ||'TopicB' in topics"
})

Upvotes: 1

Related Questions