Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsubscribe from all topics at once from Firebase Messaging

Is there any way to unsubscribe from all topics at once?

I'm using Firebase Messaging to receive push notification from some topics subscribed, and somehow I need to unsubscribe from all topics without unsubscribing one by one. Is that possible?

like image 374
Lennon Spirlandelli Avatar asked Jul 05 '16 20:07

Lennon Spirlandelli


People also ask

How do I cancel my Firebase topic?

When a client app subscribes to a new topic name (one that does not already exist for your Firebase project), a new topic of that name is created in FCM and any client can subsequently subscribe to it. To unsubscribe, the client app calls Firebase Cloud Messaging unsubscribeFromTopic() with the topic name.

Is there a limit on Firebase messaging?

You can send up to 240 messages/minute and 5,000 messages/hour to a single device.


2 Answers

You can use Instance API to query all the available topics subscribed to given token and than call multiple request to unsubscribe from all the topics.

However, if you want to stop receiving from all the topics and then the token is not useful at all, you can call FirebaseInstanceId.getInstance().deleteInstanceId() (reference: deleteInstanceId() and surround with a try/catch for a potential IOException) that will reset the instance id and than again you can subscribe to new topics from the new instance id and token.

Hope this helps someone.

like image 165
kirtan403 Avatar answered Sep 24 '22 14:09

kirtan403


For Java users:

If you want to do it topic wise, refer others answers and If you want to stop recieving FCM push notification, do below:

new Thread(new Runnable() {     @Override     public void run() {         try {             FirebaseInstanceId.getInstance().deleteInstanceId();         } catch (IOException e) {             e.printStackTrace();         }     } }).start(); 

I have placed deleteInstanceId() in a separate thread to stop java.io.IOException: MAIN_THREAD W/System.err and wrapped with try / catch to handle IOException.

like image 36
Blasanka Avatar answered Sep 26 '22 14:09

Blasanka