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?
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.
You can send up to 240 messages/minute and 5,000 messages/hour to a single device.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With