プッシュ通知

Kii の Thing 機能では、Thing に対して専用の Thing スコープが作成されます。このスコープ内の Bucket およびトピックを使ってプッシュ通知を利用できます(Thing スコープの概要は Thing スコープ をご参照ください)。

Bucket やトピックを使ったプッシュ通知の利用方法は他のスコープの場合と同様です。詳しい利用方法についてはそれぞれ サーバー上変更のプッシュ通知(Push to App) および ユーザープッシュ通知 (Push to User) を参照してください。

サーバー上変更のプッシュ通知の例

Thing スコープの Bucket を作成し、ユーザーが購読する例を以下に挙げます。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Create a bucket.
      KiiBucket thingBucket = thing.bucket("thing_bucket");
    
      // Create and save a KiiObject in the bucket.
      thingBucket.object().save();
    
      // Subscribe to the bucket.
      KiiUser.getCurrentUser().pushSubscription().subscribe(thingBucket);
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Create a bucket.
        KiiBucket thingBucket = result.bucket("thing_bucket");
    
        // Create and save a KiiObject in the bucket.
        thingBucket.object().save(new KiiObjectCallBack() {
          @Override
          public void onSaveCompleted(int token, KiiObject object, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
    
            // Subscribe to the bucket.
            KiiUser.getCurrentUser().pushSubscription().subscribe(thingBucket, new KiiPushCallBack() {
              @Override
              public void onSubscribeCompleted(int taskId, KiiBucket target, Exception e) {
                if (e != null) {
                  // Handle the error.
                  return;
                }
              }
            });
          }
        });
      }
    });

iOS

Swift:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Create a bucket.
    let thingBucket = thing.bucket(withName: "thing_bucket")
    
    // Create a KiiObject in the bucket.
    let object = thingBucket.createObject()
    
    do{
      // Save the KiiObject.
      try object.saveSynchronous()
    
      // Subscribe to the bucket.
      try KiiUser.current()!.pushSubscription().subscribeSynchronous(thingBucket)
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Create a bucket.
      let thingBucket = thing!.bucket(withName: "thing_bucket")
    
      // Create and save a KiiObject in the bucket.
      let object = thingBucket.createObject()
      object.save { (retObject : KiiObject?, error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
    
        // Subscribe to the bucket.
        KiiUser.current()!.pushSubscription().subscribe(thingBucket, block: { (subscription : KiiPushSubscription?, error : Error?) -> Void in
          if error != nil {
            // Handle the error.
            return
          }
        })
      }
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                  error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Create a bucket.
    KiiBucket* thingBucket = [thing bucketWithName:@"thing_bucket"];
    
    // Create and save a KiiObject in the bucket.
    KiiObject *object = [thingBucket createObject];
    [object saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Subscribe to the bucket.
    [[KiiUser currentUser].pushSubscription subscribeSynchronous:thingBucket
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                     block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Create a bucket.
      KiiBucket* thingBucket = [thing bucketWithName:@"thing_bucket"];
    
      // Create and save a KiiObject in the bucket.
      KiiObject *object = [thingBucket createObject];
      [object saveWithBlock:^(KiiObject *object, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
    
        // Subscribe to the bucket.
        [[KiiUser currentUser].pushSubscription subscribe:thingBucket
                                                    block:^(KiiPushSubscription *subscription, NSError *error) {
          if (error != nil) {
            // Handle the error.
            return;
          }
        }];
      }];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Create a bucket.
    var thingBucket = thing.bucketWithName("thing_bucket");

    // Create a KiiObject in the bucket.
    var object = thingBucket.createObject();

    // Save the KiiObject.
    object.save({
      success: function(theObject) {
        // Subscribe to the bucket.
        KiiUser.getCurrentUser().pushSubscription().subscribe(thingBucket, {
          success: function(thePushSubscription, theBucket) {
            // Do something.
          },
          failure: function(errorString) {
            // Handle the error.
          }
        });
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });
  },
  failure: function(errorString) {
    // Handle the error.
  }
});

同様に Thing スコープの Bucket を作成し、Thing が購読することも可能です。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Create a bucket.
      KiiBucket thingBucket = thing.bucket("thing_bucket");
    
      // Create and save a KiiObject in the bucket.
      thingBucket.object().save();
    
      // Subscribe to the bucket.
      thing.pushSubscription().subscribe(thingBucket);
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Create a bucket.
        KiiBucket thingBucket = result.bucket("thing_bucket");
    
        // Create and save a KiiObject in the bucket.
        thingBucket.object().save(new KiiObjectCallBack() {
          @Override
          public void onSaveCompleted(int token, KiiObject object, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
    
            // Subscribe to the bucket.
            result.pushSubscription().subscribe(thingBucket, new KiiPushCallBack() {
              @Override
              public void onSubscribeCompleted(int taskId, KiiBucket target, Exception e) {
                if (e != null) {
                  // Handle the error.
                  return;
                }
              }
            });
          }
        });
      }
    });

iOS

Swift:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Create a bucket.
    let thingBucket = thing.bucket(withName: "thing_bucket")
    
    // Create a KiiObject in the bucket.
    let object = thingBucket.createObject()
    
    do{
      // Save the KiiObject.
      try object.saveSynchronous()
    
      // Subscribe to the bucket.
      try thing.pushSubscription().subscribeSynchronous(thingBucket)
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Create a bucket.
      let thingBucket = thing!.bucket(withName: "thing_bucket")
    
      // Create and save a KiiObject in the bucket.
      let object = thingBucket.createObject()
      object.save { (retObject : KiiObject?, error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
    
        // Subscribe to the bucket.
        thing!.pushSubscription().subscribe(thingBucket, block: { (subscription : KiiPushSubscription?, error : Error?) -> Void in
          if error != nil {
            // Handle the error.
            return
          }
        })
      }
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                  error:&error];
    
    // Create a bucket.
    KiiBucket* thingBucket = [thing bucketWithName:@"thing_bucket"];
    
    // Create and save a KiiObject in the bucket.
    KiiObject *object = [thingBucket createObject];
    [object saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Subscribe to the bucket.
    [thing.pushSubscription subscribeSynchronous:thingBucket
                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                     block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Create a bucket.
      KiiBucket* thingBucket = [thing bucketWithName:@"thing_bucket"];
    
      // Create and save a KiiObject in the bucket.
      KiiObject *object = [thingBucket createObject];
      [object saveWithBlock:^(KiiObject *object, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
    
        // Subscribe to the bucket.
        [thing.pushSubscription subscribe:thingBucket
                                    block:^(KiiPushSubscription *subscription, NSError *error) {
          if (error != nil) {
            // Handle the error.
            return;
          }
        }];
      }];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Create a bucket.
    var thingBucket = thing.bucketWithName("thing_bucket");

    // Create a KiiObject in the bucket.
    var object = thingBucket.createObject();

    // Save the KiiObject.
    object.save({
      success: function(theObject) {
        // Subscribe to the bucket.
        thing.pushSubscription().subscribe(thingBucket, {
          success: function(thePushSubscription, theBucket) {
            // Do something.
          },
          failure: function(errorString) {
            // Handle the error.
          }
        });
      },
      failure: function(theObject, errorString) {
        // Handle the error.
      }
    });
  },
  failure: function(errorString) {
    // Handle the error.
  }
});

ユーザープッシュ通知の例

Thing スコープのトピックを作成し購読する例を以下に挙げます。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Create a topic.
      KiiTopic thingTopic = thing.topic("thing_topic");
    
      // Save the topic.
      thingTopic.save();
    
      // Subscribe to the topic.
      KiiUser.getCurrentUser().pushSubscription().subscribe(thingTopic);
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Create a topic.
        KiiTopic thingTopic = result.topic("thing_topic");
    
        // Save the topic.
        thingTopic.save(new KiiTopicCallBack() {
          @Override
          public void onSaveCompleted(int taskId, KiiTopic target, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
    
            // Subscribe to the topic.
            KiiUser.getCurrentUser().pushSubscription().subscribe(target, new KiiPushCallBack() {
              @Override
              public void onSubscribeCompleted(int taskId, KiiBucket target, Exception e) {
                if (e != null) {
                  // Handle the error.
                  return;
                }
              }
            });
          }
        });
      }
    });

iOS

Swift:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Create a topic.
    let thingTopic = thing.topic(withName: "thing_topic")
    
    do{
      // Save the topic.
      try thingTopic.saveSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
    
    do{
      // Subscribe to the topic.
      try KiiUser.current()!.pushSubscription().subscribeSynchronous(thingTopic)
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Create a topic.
      let thingTopic = thing!.topic(withName: "thing_topic")
    
      // Save the topic.
      thingTopic.save({ (thingTopic , error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
    
        // Subscribe to the topic.
        KiiUser.current()!.pushSubscription().subscribe(thingTopic, block: { (subscription : KiiPushSubscription?, error : Error?) -> Void in
          if error != nil {
            // Handle the error.
            return
          }
        })
      })
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Create a topic.
    KiiTopic* thingTopic= [thing topicWithName:@"thing_topic"];
    
    // Save the topic.
    [thingTopic saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Subscribe to the topic.
    [[KiiUser currentUser].pushSubscription subscribeSynchronous:thingTopic
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                              block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Create a topic.
      KiiTopic* thingTopic= [thing topicWithName:@"thing_topic"];
    
      // Save the topic.
      [thingTopic saveWithBlock:^(KiiTopic *topic, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
    
        // Subscribe to the topic.
        [[KiiUser currentUser].pushSubscription subscribe:topic
                                                    block:
            ^(KiiPushSubscription *subscription, NSError *error) {
          if (error != nil) {
            // Handle the error.
            return;
          }
        }];
      }];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Create a topic.
    thingTopic = thing.topicWithName("thing_topic");

    // Save the topic.
    thingTopic.save({
      success: function(theTopic) {
        // Subscribe to the topic.
        KiiUser.getCurrentUser().pushSubscription().subscribe(thingTopic, {
          success: function(pushSubscription) {
            // Do something.
          },
          failure: function(pushSubscription, error) {
            // Handle the error.
          }
        });
      },
      failure: function(theTopic, error) {
        // Handle the error.
      }
    });
  },
  failure: function(error) {
    // Handle the error.
  }
});

同様に Thing スコープのトピックを作成し、Thing が購読することも可能です。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Create a topic.
      KiiTopic thingTopic = thing.topic("thing_topic");
    
      // Save the topic.
      thingTopic.save();
    
      // Subscribe to the topic.
      thing.pushSubscription().subscribe(thingTopic);
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(final KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Create a topic.
        KiiTopic thingTopic = result.topic("thing_topic");
    
        // Save the topic.
        thingTopic.save(new KiiTopicCallBack() {
          @Override
          public void onSaveCompleted(int taskId, KiiTopic target, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
    
            // Subscribe to the topic.
            result.pushSubscription().subscribe(target, new KiiPushCallBack() {
              @Override
              public void onSubscribeCompleted(int taskId, KiiBucket target, Exception e) {
                if (e != null) {
                  // Handle the error.
                  return;
                }
              }
            });
          }
        });
      }
    });

iOS

Swift:

  • let thing : KiiThing
    
    do{
      // Instantiate a thing by vendor thing ID.
      thing = try KiiThing.loadSynchronous(withVendorThingID: "rBnvSPOXBDF9r29GJeGS")
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    
    // Create a topic.
    let thingTopic = thing.topic(withName: "thing_topic")
    
    do{
      // Save the topic.
      try thingTopic.saveSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
    
    do{
      // Subscribe to the topic.
      try thing.pushSubscription().subscribeSynchronous(thingTopic)
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withVendorThingID: "rBnvSPOXBDF9r29GJeGS") { (thing , error) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Create a topic.
      let thingTopic = thing!.topic(withName: "thing_topic")
    
      // Save the topic.
      thingTopic.save({ (thingTopic , error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
    
        // Subscribe to the topic.
        thing!.pushSubscription().subscribe(thingTopic, block: { (subscription : KiiPushSubscription?, error : Error?) -> Void in
          if error != nil {
            // Handle the error.
            return
          }
        })
      })
    }

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing* thing = [KiiThing loadSynchronousWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Create a topic.
    KiiTopic* thingTopic= [thing topicWithName:@"thing_topic"];
    
    // Save the topic.
    [thingTopic saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Subscribe to the topic.
    [thing.pushSubscription subscribeSynchronous:thingTopic
                                           error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithVendorThingID:@"rBnvSPOXBDF9r29GJeGS"
                           block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Create a topic.
      KiiTopic* thingTopic= [thing topicWithName:@"thing_topic"];
    
      // Save the topic.
      [thingTopic saveWithBlock:^(KiiTopic *topic, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
    
        // Subscribe to the topic.
        [thing.pushSubscription subscribe:topic
                                    block:^(KiiPushSubscription *subscription, NSError *error) {
          if (error != nil) {
            // Handle the error.
            return;
          }
        }];
      }];
    }];

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Create a topic.
    thingTopic = thing.topicWithName("thing_topic");

    // Save the topic.
    thingTopic.save({
      success: function(theTopic) {
        // Subscribe to the topic.
        thing.pushSubscription().subscribe(thingTopic, {
          success: function(pushSubscription) {
            // Do something.
          },
          failure: function(pushSubscription, error) {
            // Handle the errror.
          }
        });
      },
      failure: function(theTopic, error) {
        // Handle the errror.
      }
    });
  },
  failure: function(error) {
    // Load thing failed.
  }
});

トピック一覧の取得例

Thing スコープの全てのトピックを一覧として取得することができます。トピックの件数が 50 件を超える場合は、ページネーションによって複数回にわけて一覧を取得する必要があります

以下に、Thing スコープに存在するトピックの一覧を取得する例を挙げます。

Android

  • try {
      // Instantiate a thing by vendor thing ID.
      KiiThing thing = KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS");
    
      // Get the first page of the topic list.
      KiiListResult<KiiTopic> result = thing.listTopics();
      for (KiiTopic topic : result.getResult()) {
        // Do something.
      }
    
      if (result.hasNext()) {
        // Get the next page of the topic list.
        result = thing.listTopics(result.getPaginationKey());
        for (KiiTopic topic : result.getResult()) {
          // Do something.
        }
      }
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(final KiiThing thing, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Get the first page of the topic list.
        thing.listTopics(new KiiCallback<KiiListResult<KiiTopic>>() {
          @Override
          public void onComplete(KiiListResult<KiiTopic> result, Exception e) {
            if (e != null) {
              // Handle the error.
              return;
            }
            for (KiiTopic topic : result.getResult()) {
              // Do something.
            }
    
            if (result.hasNext()) {
              // Get the next page of the topic list.
              thing.listTopics(result.getPaginationKey(), new KiiCallback<KiiListResult<KiiTopic>>() {
                @Override
                public void onComplete(KiiListResult<KiiTopic> result, Exception e) {
                  // Do something.
                }
              });
            }
          }
        });
      }
    });

ここでは以下の処理を実行しています。

  • listTopics メソッドを実行し、トピック一覧を取得します。
  • getResult メソッドを実行し、トピック一覧を List で取得します。
  • hasNext メソッドを実行して取得しきれなかったトピックがあるか確認し、ある場合は getPaginationKey メソッドで取得したキーを引数に再度、listTopics メソッドを実行します。

iOS

Swift:

  • var resultObj : KiiListResult
    
    do{
      // Instantiate a thing by vendor thing ID.
      let aThing = try KiiThing.loadSynchronous(withThingID: "th.id123455")
    
      // Get the first page of the topic list.
      resultObj = try aThing.listTopicsSynchronous()
    }catch(let error as NSError){
      // Handle the error.
      return
    }
    for topic in (resultObj.results as! [KiiTopic]){
      // Do something.
    }
    
    if resultObj.hasNext {
      // Get the next page of the topic list.
      resultObj = try! Kii.listTopicsSynchronous(resultObj.paginationKey!, error: ())
      for topic in (resultObj.results as! [KiiTopic]){
        // Do something.
      }
    }
  • // Instantiate a thing by vendor thing ID.
    KiiThing.load(withThingID: "th.id123455", block: { (thing : KiiThing?, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Get the first page of the topic list.
      thing!.listTopics { (resultObj : KiiListResult?, callerObject : Any?, error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
        for topic in (resultObj!.results as! [KiiTopic]){
          // Do something.
        }
    
        if resultObj!.hasNext {
          // Get the next page of the topic list.
          thing!.listTopics(resultObj!.paginationKey, block: { (resultObj : KiiListResult?, callerObject : Any?, error : Error?) -> Void in
            if error != nil {
              // Handle the error.
              return
            }
           for topic in (resultObj!.results as! [KiiTopic]){
              // Do something.
            }
          })
        }
      }
    })

Objective-C:

  • NSError* error = nil;
    
    // Instantiate a thing by vendor thing ID.
    KiiThing *aThing = [KiiThing loadSynchronousWithThingID:@"th.id123455" error:&error];
    
    // Get the first page of the topic list.
    KiiListResult *resultObj = [aThing listTopicsSynchronous:&error];
    if(error){
      // Handle the error.
      return;
    }
    [resultObj.results enumerateObjectsUsingBlock:^(KiiTopic* topic, NSUInteger idx, BOOL *stop) {
      // Do something.
    }];
    
    if(resultObj.hasNext){
      // Get the next page of the topic list.
      resultObj = [aThing listTopicsSynchronous:resultObj.paginationKey error:&error];
      [resultObj.results enumerateObjectsUsingBlock:^(KiiTopic* topic, NSUInteger idx, BOOL *stop) {
        // Do something.
      }];
    }
  • // Instantiate a thing by vendor thing ID.
    [KiiThing loadWithThingID:@"th.id123455"
                        block:^(KiiThing *thing, NSError *error) {
      if(error){
        // Handle the error.
        return;
      }
    
      // Get the first page of the topic list.
      [thing listTopics:^(KiiListResult *resultObj, id callerObject, NSError *error) {
        if(error){
          // Handle the error.
          return;
        }
        [resultObj.results enumerateObjectsUsingBlock:^(KiiTopic* topic, NSUInteger idx, BOOL *stop) {
          // Do something.
        }];
    
        if(resultObj.hasNext){
          // Get the next page of the topic list.
          [thing listTopics:^(KiiListResult *resultObj, id callerObject, NSError *error) {
            [resultObj.results enumerateObjectsUsingBlock:^(KiiTopic* topic, NSUInteger idx, BOOL *stop) {
              // Do something.
            }];
          }];
        }
      }];
    }];

ここでは、以下の処理を実行しています。

  • listTopicsSynchronous メソッドを実行し、トピック一覧を取得します。
  • [KiiListResult results] メソッドを実行し、トピック一覧を NSArray で取得します。
  • [KiiListResult hasNext] メソッドを実行して取得しきれなかったトピックがあるか確認し、ある場合は getPaginationKey メソッドで取得したキーを引数に、再度 listTopicsSynchronous メソッドを実行します。

JavaScript

// Instantiate a thing by vendor thing ID.
KiiThing.loadWithVendorThingID("rBnvSPOXBDF9r29GJeGS", {
  success: function(thing) {
    // Get the first page of the topic list.
    thing.listTopics({
      success: function(topicList, nextPaginationKey) {
        for(var i=0; i<topicList.length; i++){
          // Do something.
          var topic = topicList[i];
        }
        if (nextPaginationKey != null) {
          // Get the next page of the topic list.
          thing.listTopics({
            success: function(topicList, nextPaginationKey) {
              // Do something.
            },
            failure: function(errorString) {
              console.log("Error listing topics: " + errorString);
            }
          }, nextPaginationKey);
        }
      },
      failure: function(errorString) {
        console.log("Error listing topics: " + errorString);
      }
    );
  },
  failure: function(error) {
    // Handle the error.
  }
});

ここでは以下の処理を実行しています。

  • loadWithVendorThingID メソッドで KiiThing のインスタンスを取得します。
  • listTopics メソッドを実行し、トピック一覧を取得します。
  • nextPaginationKey をチェックして取得しきれなかったトピックがあるか確認し、ある場合は nextPaginationKey を引数に再度、listTopics メソッドを実行します。