トピックの購読

あるトピックに興味のあるユーザーは、このトピックを購読することでトピックに送信されたメッセージをプッシュ通知で受信できるようになります。

アプリケーションスコープのトピックを購読する場合、以下のようになります。この例では、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;
      }
    }];

グループスコープのトピックを購読する場合、以下のようになります。この例では、MyGroup グループの GroupTopic という既存のトピックを取得して、ログイン中のユーザーから購読しています。

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;
      }
    }];

ユーザースコープのトピックを購読する場合、以下のようになります。この例では、ログイン中のユーザーの 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;
      }
    }];

いずれの例においても、対象トピックを指定して subscribeSynchronous:error: メソッドを実行しています。