This topic is still relevant, still no API for this in the iOS SDK.
If your goal is to prevent a user from subscribing multiple times to the same topic and therefore getting notified multiple times for, say, a single comment in a group, my solution is a simple local cache using UserDefaults.
Pretty straightforward:
func subscribeTo(topic: String){
// first check that the user isn't already
// subscribed, or they get multiple notifications
let localFlag = UserDefaults.standard.bool(forKey: topic)
if localFlag == true {
print("user already subscribed to topic: \(topic)")
return
}
print("attempting to subscribe user to topic: \(topic)")
// Subscribe to comet chat push notifications through Firebase
Messaging.messaging().subscribe(toTopic: topic) { error in
if error == nil {
print("subscribed CometChat user to topic: \(topic)")
// set local flag as "already subbed"
UserDefaults.standard.setValue(true, forKey: topic)
return
}
print("attempt to subscribe CometChat user to topic \(topic) failed: \(error?.localizedDescription ?? "(no error provided)")")
}
}
The flow of my application logs a user in, and then automatically gets a list of topics associated with the user and auto-subscribes to the topic with each launch.
The reason for this is a high degree of assurance that the user is getting notified.
The flow:
User launches app-> topics retrieved-> iterate & pass topic to subscribe func -> block if topic == true-> pass through if topic != true
And then of course we assign nil
to the local bool at the topic key upon unsubscribe.
Unsubscribes are always successful without such blocking / checking, because it's better UX to be more conservative when a user does NOT want notifications.
Cheers.