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.

Swift:

  • // Instantiate an existing topic in the application scope.
    let topicName = "SendingAlert"
    let topic = Kii.topic(withName: topicName)
    
    do{
      // Subscribe to the topic.
      try KiiUser.current()!.pushSubscription().subscribeSynchronous(topic)
    }catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate an existing topic in the application scope.
    let topicName = "SendingAlert"
    let topic = Kii.topic(withName: topicName)
    
    // Subscribe to the topic.
    KiiUser.current()!.pushSubscription().subscribe(topic) { (subscription : KiiPushSubscription, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    }

Objective-C:

  • NSError *error = nil;
    
    // Instantiate an existing topic in the application scope.
    NSString *topicname = @"SendingAlert";
    KiiTopic *topic = [Kii topicWithName:topicname];
    
    // Subscribe to the topic.
    KiiPushSubscription *subscription = [[KiiUser currentUser].pushSubscription subscribeSynchronous:topic
                                                                                               error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate an existing topic in the application scope.
    NSString *topicname = @"SendingAlert";
    KiiTopic *topic = [Kii topicWithName:topicname];
    
    // Subscribe to the topic.
    [[KiiUser currentUser].pushSubscription subscribe:topic
                                                block:^(KiiPushSubscription *subscription, NSError *error) {
      if (error != nil) {
        // 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.

Swift:

  • // Instantiate an existing group.
    let group = KiiGroup(uri: groupURI)
    
    // Instantiate an existing topic in the group scope.
    let topicName = "GroupTopic"
    let topic = group.topic(withName: topicName)
    
    do{
      // Subscribe to the topic.
      // (The current user must be a group member)
      try KiiUser.current()!.pushSubscription().subscribeSynchronous(topic)
    }catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate an existing group.
    let group = KiiGroup(uri: groupURI)
    
    // Instantiate an existing topic in the group scope.
    let topicName = "GroupTopic"
    let topic = group.topic(withName: topicName)
    
    // Subscribe to the topic.
    // (The current user must be a group member)
    KiiUser.current()!.pushSubscription().subscribe(topic) { (subscription : KiiPushSubscription, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    }

Objective-C:

  • NSError *error = nil;
    
    // Instantiate an existing group.
    KiiGroup *group = [KiiGroup groupWithURI:groupUri];
    
    // Instantiate an existing topic in the group scope.
    NSString *topicname = @"GroupTopic";
    KiiTopic *topic = [group topicWithName:topicname];
    
    // Subscribe to the topic.
    // (The current user must be a group member)
    KiiPushSubscription *subscription = [[KiiUser currentUser].pushSubscription subscribeSynchronous:topic
                                                                                               error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate an existing group.
    KiiGroup *group = [KiiGroup groupWithURI:groupUri];
    
    // Instantiate an existing topic in the group scope.
    NSString *topicname = @"GroupTopic";
    KiiTopic *topic = [group topicWithName:topicname];
    
    // Subscribe to the topic.
    // (The current user must be a group member)
    [[KiiUser currentUser].pushSubscription subscribe:topic
                                                block:^(KiiPushSubscription *subscription, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

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

Swift:

  • // Instantiate an existing topic in the user scope.
    let user = KiiUser.current()!
    let topicName = "MyTODO"
    let topic = user.topic(withName: topicName)
    
    do{
      // Subscribe to the topic.
      try KiiUser.current()!.pushSubscription().subscribeSynchronous(topic)
    }catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate an existing topic in the user scope.
    let user = KiiUser.current()!
    let topicName = "MyTODO"
    let topic = user.topic(withName: topicName)
    
    // Subscribe to the topic.
    KiiUser.current()!.pushSubscription().subscribe(topic) { (subscription : KiiPushSubscription, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    }

Objective-C:

  • NSError *error = nil;
    
    // Instantiate an existing topic in the user scope.
    KiiUser* user = [KiiUser currentUser];
    NSString *topicname = @"MyTODO";
    KiiTopic *topic = [user topicWithName:topicname];
    
    // Subscribe to the topic.
    [[KiiUser currentUser].pushSubscription subscribeSynchronous:topic
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate an existing topic in the user scope.
    KiiUser* user = [KiiUser currentUser];
    NSString *topicname = @"MyTODO";
    KiiTopic *topic = [user topicWithName:topicname];
    
    // Subscribe to the topic.
    [[KiiUser currentUser].pushSubscription subscribe:topic
                                                block:^(KiiPushSubscription *subscription, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

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

  • Instantiates a target topic.
  • Calls the subscribeSynchronous:error: method while specifying the target topic.