Bucket の購読
ある Bucket の更新に興味のあるユーザーは、この Bucket を講読することで、Bucket に何らかの更新があった際にプッシュ通知を受信できるようになります。
Bucket を購読して監視を開始する例を以下に挙げます。
Swift:
-
// If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud. // Instantiate a bucket. let bucket = Kii.bucket(withName: "_target_bucket_") // Create a KiiObject in the bucket. let obj1 = bucket.createObject() do{ // Save the KiiObject. try obj1.saveSynchronous() // Subscribe to the bucket. try KiiUser.current()!.pushSubscription().subscribeSynchronous(bucket) } catch let error as NSError { // Handle the error. return }
-
// If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud. // Instantiate a bucket. let bucket = Kii.bucket(withName: "_target_bucket_") // Create and save a KiiObject in the bucket. let obj1 = bucket.createObject() obj1.save { (retObject : KiiObject?, error : Error?) -> Void in if error != nil { // Handle the error. return } // Subscribe to the bucket. KiiUser.current()!.pushSubscription().subscribe(bucket, block: { (subscription : KiiPushSubscription, error : Error?) -> Void in if error != nil { // Handle the error. return } }) }
Objective-C:
-
// If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud. NSError *error = nil; // Instantiate a bucket. KiiBucket *bucket = [Kii bucketWithName:@"_target_bucket_"]; // Create and save a KiiObject in the bucket. KiiObject *obj1 = [bucket createObject]; [obj1 saveSynchronous:&error]; if (error != nil) { // Handle the error. return; } // Subscribe to the bucket. [[KiiUser currentUser].pushSubscription subscribeSynchronous:bucket error:&error]; if (error != nil) { // Handle the error. return; }
-
// If the bucket is new, save it on Kii Cloud by saving a KiiObject in the bucket and then // subscribe to the bucket. You cannot subscribe to a bucket if it does not exist on Kii Cloud. // Instantiate a bucket. KiiBucket *bucket = [Kii bucketWithName:@"_target_bucket_"]; // Create and save a KiiObject in the bucket. KiiObject *obj1 = [bucket createObject]; [obj1 saveWithBlock:^(KiiObject *object, NSError *error) { if (error != nil) { // Handle the error. return; } // Subscribe to the bucket. [[KiiUser currentUser].pushSubscription subscribe:bucket block:^(KiiPushSubscription *subscription, NSError *error) { if (error != nil) { // Handle the error. return; } }]; }];
多数の KiiObject を持つ Bucket の購読
Bucket への更新が 1 件あるごとに、プッシュ通知は 1 回行われるため、Bucket によってはプッシュ通知の回数が非常に多くなります。
たとえば、3 台のデバイスを持っているユーザーが、ある Bucket を購読しているとします。その Bucket に 100 件のオブジェクトがあり、それらすべてが更新されたとすると、プッシュ通知の回数は各デバイスごとに 100 回、合計 300 回のプッシュ通知となります。
あらかじめ、このように大量のプッシュ通知が発生することが予想できる場合は、サーバー上変更のプッシュ通知(Push to App)ではなく、ユーザープッシュ通知 (Push to User) を利用することができます。Bucket を更新した後、プログラムからトピックへのメッセージ送信の要求を出すことで、プッシュ通知の回数をデバイスごとに 1 回とすることができます。ただし、この場合は トランザクション に示すように、更新とトピックへのプッシュ要求の間に処理の中断要素があるため、プッシュが届かない可能性も考慮する必要があります。