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.

  • // Instantiate the target topic.
    var topicName = "SendingAlert";
    var topic = Kii.topicWithName(topicName);
    
    // Check if the current user is subscribed to the topic.
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().isSubscribed(topic, {
      success: function(thePushSubscription, theBucket, isSubscribed) {
        if(isSubscribed) {
          // The current user is subscribed to the topic.
        } else {
          // The current user is not subscribed to the topic.
        }
      },
      failure: function(theTopic, errorString) {
        // Handle the error.
      }
    });
  • // Instantiate the target topic.
    var topicName = "SendingAlert";
    var topic = Kii.topicWithName(topicName);
    
    // Check if the current user is subscribed to the topic.
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().isSubscribed(topic).then(
      function(params) {
        var thePushSubscription = params[0];
        var theTopic = params[1];
        var isSubscribed = params[2];
        if(isSubscribed) {
          // The current user is subscribed to the topic.
        } else {
          // The current user is not subscribed to the topic.
        }
      }
    ).catch(
      function(error) {
        // Handle the error.
        var thePushSubscription = error.target;
        var errorString = error.message;
      }
    );

Here's a description of the sample code:

  • Instantiates a topic.
  • Creates a KiiPushSubscription instance by calling the pushSubscription method.
  • Calls the isSubscribed method to check if the current user is already subscribed to the topic.