Checking the Subscription Status of a Topic
You might want to check if the current user is already subscribed to a topic. For example, your application may want to show a "SUBSCRIBE" button for users who have not yet subscribed to a topic and an "UNSUBSCRIBE" button instead for those users who are already subscribed to the topic.
Here is the sample code for checking the subscription status.
Swift:
// Instantiate the target topic.
let topic = Kii . topic ( withName : "SendingAlert" )
do {
// Check if the current user is subscribed to the topic.
try KiiUser . current () !. pushSubscription () . checkIsSubscribedSynchronous ( topic )
} catch ( let error as NSError ){
print ( "The current user is not subscribed to the topic." )
return
}
print ( "The current user is subscribed to the topic." )
// Instantiate the target topic.
let topic = Kii . topic ( withName : "SendingAlert" )
// Check if the current user is subscribed to the topic.
KiiUser . current () !. pushSubscription () . checkIsSubscribed ( topic ) { ( topic : KiiSubscribable , subscribed : Bool , error : Error ?) -> Void in
if error != nil {
// Handle the error.
return
}
if ( subscribed ) {
print ( "The current user is subscribed to the topic." )
} else {
print ( "The current user is not subscribed to the topic." )
}
}
Objective-C:
NSError * error = nil ;
// Instantiate the target topic.
KiiTopic * topic = [ Kii topicWithName : @"SendingAlert" ];
// Check if the current user is subscribed to the topic.
BOOL subscribed = [[ KiiUser currentUser ]. pushSubscription checkIsSubscribedSynchronous : topic
error : & error ];
if ( subscribed ) {
NSLog ( @"The current user is subscribed to the topic." );
} else {
if ( error . kiiHttpStatus == 404 ) {
NSLog ( @"The current user is not subscribed to the topic." );
} else {
// Handle the error.
return ;
}
}
// Instantiate the target topic.
KiiTopic * topic = [ Kii topicWithName : @"SendingAlert" ];
// Check if the current user is subscribed to the topic.
[[ KiiUser currentUser ]. pushSubscription checkIsSubscribed : topic
block : ^ ( id < KiiSubscribable > subscribable , BOOL subscribed , NSError * error ) {
if ( subscribed ) {
NSLog ( @"The current user is subscribed to the topic." );
} else {
if ( error . kiiHttpStatus == 404 ) {
NSLog ( @"The current user is not subscribed to the topic." );
} else {
// Handle the error.
return ;
}
}
}];
Here is a description of the sample code:
Instantiates a topic.
Calls the checkSubscriptionSynchronous:error:
method to see if the current user is already subscribed to the topic.