トピックの有無の確認

モバイルアプリのプログラムから、特定のトピックが購読済みかどうかを確認することができます。

以下に、ログイン中のユーザーがアプリケーションスコープのトピックを購読済みかどうかを確認する例を挙げます(グループスコープのトピックとユーザースコープのトピックについても、基本的な操作は同様です)。

Swift:

  • // Instantiate the target topic.
    let topic = Kii.topic(withName: "SendingAlert")
    
    do{
      // Check if the current user is subscribed to the topic.
      try KiiUser.current()!.pushSubscription().checkIsSubscribedSynchronous(topic)
    }catch(let error as NSError){
      print("The current user is not subscribed to the topic.")
      return
    }
    print("The current user is subscribed to the topic.")
  • // Instantiate the target topic.
    let topic = Kii.topic(withName: "SendingAlert")
    
    // Check if the current user is subscribed to the topic.
    KiiUser.current()!.pushSubscription().checkIsSubscribed(topic) { (topic : KiiSubscribable , subscribed : Bool, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
      if (subscribed) {
        print("The current user is subscribed to the topic.")
      } else {
        print("The current user is not subscribed to the topic.")
      }
    }

Objective-C:

  • NSError *error = nil;
    
    // Instantiate the target topic.
    KiiTopic *topic = [Kii topicWithName:@"SendingAlert"];
    
    // Check if the current user is subscribed to the topic.
    BOOL subscribed = [[KiiUser currentUser].pushSubscription checkIsSubscribedSynchronous:topic
                                                                                     error:&error];
    if (subscribed) {
      NSLog(@"The current user is subscribed to the topic.");
    } else {
      if (error.kiiHttpStatus == 404) {
        NSLog(@"The current user is not subscribed to the topic.");
      } else {
        // Handle the error.
        return;
      }
    }
  • // Instantiate the target topic.
    KiiTopic *topic = [Kii topicWithName:@"SendingAlert"];
    
    // Check if the current user is subscribed to the topic.
    [[KiiUser currentUser].pushSubscription checkIsSubscribed:topic
                                                        block:^(id<KiiSubscribable> subscribable, BOOL subscribed, NSError *error) {
      if (subscribed) {
        NSLog(@"The current user is subscribed to the topic.");
      } else {
        if (error.kiiHttpStatus == 404) {
          NSLog(@"The current user is not subscribed to the topic.");
        } else {
          // Handle the error.
          return;
        }
      }
    }];

ここでは次の処理を行っています。

  • 購読の有無を確認したいトピックのインスタンスを取得。
  • checkSubscriptionSynchronous:error: メソッドを実行し、ログイン中のユーザーに対するトピック購読の有無を確認。