0

I developing an iOS application that integrates with Firebase to deliver remote push notifications.

The application includes a login feature, and sending a notification using the FCM token sends it even though the user is not logged-in, and I get why that happened because the FCM token is an app-device combination not an app-user combination.

But is there a workaround to guarantee that the notification is only delivered when the user is logged-in?

Felwah
  • 65
  • 1
  • 9

2 Answers2

2

Push notification are not linked with user status. If you can, you should track user state on your server, checking a login token for example, to check if the user is logged in or not. But i think that you can't be completely sure for the user state.

Alessandro
  • 2,927
  • 1
  • 10
  • 14
2

Yes you can achieve this by subscribing the user with a topic.

In your case, as soon as user logged in to your app subscribe the user with 'login' (or whatever you like the) topic.

Example:

Messaging.messaging().subscribe(toTopic: "login") 

And when user logging out then unsubscribe that user from 'login' topic:

Messaging.messaging().unsubscribe(fromTopic: "login")

Now send notification to 'login' topic only so that all logged in will get your notifications.

Hope that will work for you

Antoine
  • 1,393
  • 4
  • 20
  • 26