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.

  • try {
      // Instantiate an existing topic in the application scope.
      String topicName = "SendingAlert";
      KiiTopic topic = Kii.topic(topicName);
    
      // Subscribe to the topic.
      KiiUser user = KiiUser.getCurrentUser();
      KiiPushSubscription sub = user.pushSubscription();
      sub.subscribe(topic);
    } catch (IOException ioe) {
      // Handle the the error.
    } catch (AppException e) {
      // Handle the the error.
    }
  • // Instantiate an existing topic in the application scope.
    String topicName = "SendingAlert";
    KiiTopic topic = Kii.topic(topicName);
    
    // Subscribe to the topic.
    KiiUser user = KiiUser.getCurrentUser();
    KiiPushSubscription sub = user.pushSubscription();
    sub.subscribe(topic, new KiiPushCallBack() {
      @Override
      public void onSubscribeCompleted(int taskId, KiiSubscribable target, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

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.

  • try {
      // Instantiate an existing group.
      KiiGroup group = KiiGroup.createByUri(groupUri);
    
      // Instantiate an existing topic in the group scope.
      String topicName = "GroupTopic";
      KiiTopic topic = group.topic(topicName);
    
      // Subscribe to the topic.
      // (The current user must be a group member)
      KiiUser user = KiiUser.getCurrentUser();
      KiiPushSubscription sub = user.pushSubscription();
      sub.subscribe(topic);
    } catch (IOException ioe) {
      // Handle the the error.
    } catch (AppException e) {
      // Handle the the error.
    }
  • // Instantiate an existing group.
    KiiGroup group = KiiGroup.createByUri(groupUri);
    
    // Instantiate an existing topic in the group scope.
    String topicName = "GroupTopic";
    KiiTopic topic = group.topic(topicName);
    
    // Subscribe to the topic.
    // (The current user must be a group member)
    KiiUser user = KiiUser.getCurrentUser();
    KiiPushSubscription sub = user.pushSubscription();
    sub.subscribe(topic, new KiiPushCallBack() {
      @Override
      public void onSubscribeCompleted(int taskId, KiiSubscribable target, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

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

  • try {
      // Instantiate an existing topic in the user scope.
      String topicName = "MyTODO";
      KiiTopic topic = KiiUser.topic(topicName);
    
      // Subscribe to the topic.
      KiiUser user = KiiUser.getCurrentUser();
      KiiPushSubscription sub = user.pushSubscription();
      sub.subscribe(topic);
    } catch (IOException ioe ) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Instantiate an existing topic in the user scope.
    String topicName = "MyTODO";
    KiiTopic topic = KiiUser.topic(topicName);
    
    // Subscribe to the topic.
    KiiUser user = KiiUser.getCurrentUser();
    KiiPushSubscription sub = user.pushSubscription();
    sub.subscribe(topic, new KiiPushCallBack() {
      @Override
      public void onSubscribeCompleted(int taskId, KiiSubscribable target, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

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.