Bucket の購読
ある Bucket の更新に興味のあるユーザーは、この Bucket を講読することで、Bucket に何らかの更新があった際にプッシュ通知を受信できるようになります。
Bucket を購読して監視を開始する例を以下に挙げます。
-
// 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 an existing bucket. var user = KiiUser.getCurrentUser(); var bucket = user.bucketWithName("_target_bucket_"); // Create a KiiObject in the bucket. var obj = bucket.createObject(); // Save the KiiObject. obj.save().then( function(theObject) { // Subscribe to the bucket. return user.pushSubscription().subscribe(bucket); } ).then( function(params) { var thePushSubscription = params[0]; var theBucket = params[1]; // Do something. } ).catch( function(error) { // Handle the error. // Get the KiiObject for the failed save() method. var theObject = error.target; // Get the subscription for the failed subscribe() method. var thePushSubscription = error.target; // Get the error message. var errorString = error.message; } );
-
// 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 an existing bucket. var user = KiiUser.getCurrentUser(); var bucket = user.bucketWithName("_target_bucket_"); // Create a KiiObject in the bucket. var obj = bucket.createObject(); // Save the KiiObject. obj.save({ success: function(theObject) { // Subscribe to the bucket. user.pushSubscription().subscribe(bucket, { success: function(thePushSubscription, theBucket) { // Do something. }, failure: function(errorString) { // Handle the error. } }); }, failure: function(theObject, errorString) { // Handle the error. } });
多数の KiiObject を持つ Bucket の購読
Bucket への更新が 1 件あるごとに、プッシュ通知は 1 回行われるため、Bucket によってはプッシュ通知の回数が非常に多くなります。
たとえば、3 台のデバイスを持っているユーザーが、ある Bucket を購読しているとします。その Bucket に 100 件のオブジェクトがあり、それらすべてが更新されたとすると、プッシュ通知の回数は各デバイスごとに 100 回、合計 300 回のプッシュ通知となります。
あらかじめ、このように大量のプッシュ通知が発生することが予想できる場合は、サーバー上変更のプッシュ通知(Push to App)ではなく、ユーザープッシュ通知 (Push to User) を利用することができます。Bucket を更新した後、プログラムからトピックへのメッセージ送信の要求を出すことで、プッシュ通知の回数をデバイスごとに 1 回とすることができます。ただし、この場合は トランザクション に示すように、更新とトピックへのプッシュ要求の間に処理の中断要素があるため、プッシュが届かない可能性も考慮する必要があります。