Subscribing to a Topic

Subscription to a topic is necessary to receive Push to User notifications. Once a user subscribes to a topic, all push messages sent to this topic will be delivered to the user.

The following sample code subscribes to an application-scope topic named SendingAlert.

  • // Instantiate an existing topic in the application scope.
    var topicName = "SendingAlert";
    var topic = Kii.topicWithName(topicName);
    
    // Subscribe to the topic.
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().subscribe(topic).then(
      function(params) {
        var thePushSubscription = params[0];
        var theTopic = params[1];
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var thePushSubscription = error.target;
        var errorString = error.message;
      }
    );
  • // Instantiate an existing topic in the application scope.
    var topicName = "SendingAlert";
    var topic = Kii.topicWithName(topicName);
    
    // Subscribe to the topic.
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().subscribe(topic, {
      success: function(thePushSubscription, theTopic) {
        // Do something.
      },
      failure: function(thePushSubscription, errorString) {
        // Handle the error.
      }
    });

The next sample code subscribes to a group-scope topic named GroupTopic. In this sample code, we are getting the existing topic with its URL.

  • // Instantiate an existing group.
    var group = KiiGroup.groupWithURI(groupUri);
    
    // Instantiate an existing topic in the group scope.
    var topicName = "GroupTopic";
    var topic = group.topicWithName(topicName);
    
    // Subscribe to the topic.
    // (The current user must be a group member)
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().subscribe(topic).then(
      function(params) {
        var thePushSubscription = params[0];
        var theTopic = params[1];
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var thePushSubscription = error.target;
        var errorString = error.message;
      }
    );
  • // Instantiate an existing group.
    var group = KiiGroup.groupWithURI(groupUri);
    
    // Instantiate an existing topic in the group scope.
    var topicName = "GroupTopic";
    var topic = group.topicWithName(topicName);
    
    // Subscribe to the topic.
    // (The current user must be a group member)
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().subscribe(topic, {
      success: function(thePushSubscription, theTopic) {
        // Do something.
      },
      failure: function(thePushSubscription, errorString) {
        // Handle the error.
      }
    });

The last sample code subscribes to a user-scope topic named MyTODO.

  • // Instantiate an existing topic in the user scope.
    var topicName = "MyTODO";
    var topic = KiiUser.getCurrentUser().topicWithName(topicName);
    
    // Subscribe to the topic.
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().subscribe(topic).then(
      function(params) {
        var thePushSubscription = params[0];
        var theTopic = params[1];
        // Do something.
      }
    ).catch(
      function(error) {
        // Handle the error.
        var thePushSubscription = error.target;
        var errorString = error.message;
      }
    );
  • // Instantiate an existing topic in the user scope.
    var topicName = "MyTODO";
    var topic = KiiUser.getCurrentUser().topicWithName(topicName);
    
    // Subscribe to the topic.
    var user = KiiUser.getCurrentUser();
    user.pushSubscription().subscribe(topic, {
      success: function(thePushSubscription, theTopic) {
        // Do something.
      },
      failure: function(thePushSubscription, errorString) {
        // Handle the error.
      }
    });

For all examples, here is a brief description of the sample code:

  • Instantiates a target topic.
  • Creates a KiiPushSubscription instance by calling the pushSubscription method.
  • Calls the subscribe method passing the target topic.