10

I successfully set FCM in my Website and it is working perfectly. I am using JavaScript to send notification to a 'movies' topic by subscribing it.

fetch('https://iid.googleapis.com/iid/v1/'+tokenz+'/rel/topics/movies', {
          method: 'POST',
          headers: new Headers({
            'Authorization': 'key=****'
          })

I don't find any documentation to unsubscribe a topic in web using JS. What is the best way to unsubscribe a single topic and also is there a way to unsubscribe to all topics?

AL.
  • 36,815
  • 10
  • 142
  • 281
deadpool
  • 143
  • 2
  • 6

1 Answers1

13

Good question. It never really hit me that there wasn't an advised way/guide to unsubscribe from topics for FCM Web in the documentation.

As you already know, it is stated that in order to subscribe to a topic, you'll have to make use of the Instance ID API. So I figured that you should use the same. I've gone through the docs, but there isn't anything mentioned which to use when unsubscribing a single token from a single/multiple topic(s).

With all that said, what I would suggest for now is to use batchRemove to unsubscribe your specific token from a topic. Example from the docs:

https://iid.googleapis.com/iid/v1:batchRemove
Content-Type:application/json
Authorization:key=API_KEY
{
   "to": "/topics/<YOUR TOPIC NAME HERE>",
   "registration_tokens": ["<YOUR TOKEN HERE>"]
}

I also tried the DELETE API, but it deletes the registration token itself (i.e. invalidates it).

AL.
  • 36,815
  • 10
  • 142
  • 281
  • As you mentioned instead of posting on subscribe I used delete and it works. So, all I have to do is change method: 'POST' to method:'DELETE' and send it to unsubscibe. – deadpool Mar 24 '17 at 06:45
  • Do keep in mind that `DELETE` will *invalidate* the token. You'll have to re-generate a new one. The recommended way is to use `batchRemove` only. – AL. Mar 24 '17 at 09:27
  • 1
    Good point from AL. The DELETE API works because it deletes the IID token itself. Although the device would not receive push notifications from the desired topic anymore, it wouldn't receive any notifications as well, which might not be what you want. – CristianoYL Jun 20 '17 at 14:01
  • Ridiculous that Google hasn't provided a documented way of doing this. – Johann Jun 18 '18 at 05:41
  • you can see this link from google https://developers.google.com/instance-id/reference/server#delete_push_subscriptions – Hamzeh Hirzallah Jul 02 '18 at 13:20
  • This method seems to be with odds at the advice on the topic messaging page: "Do not ever send this type of request from the client, because of the sensitivity of the server key." https://firebase.google.com/docs/cloud-messaging/js/topic-messaging This is exactly what I want to do (subscribe and unsubscribe a client) – jamis0n Dec 10 '18 at 04:15
  • @jamis0n You probably need to create a custom API that your client could call, which would then trigger the subscribe/unsubscribe from your server. That way, the server key would still stay secure. – AL. Dec 10 '18 at 07:38
  • @AL. I can do that no problem. Im still trying to figure out what tokens are needed. The API_KEY is my application's APIKey. What is the IID key in the path? `(iid/v1/'+tokenz+'/rel/topics/)` – jamis0n Dec 10 '18 at 19:24
  • 1
    @jamis0n The Instance ID token is the [registration token](https://stackoverflow.com/a/37671576/4625829) – AL. Dec 11 '18 at 15:15