トピックの購読
あるトピックに興味のあるユーザーは、このトピックを購読することでトピックに送信されたメッセージをプッシュ通知で受信できるようになります。
アプリケーションスコープのトピックを購読する場合、以下のようになります。この例では、SendingAlert
という既存のトピックを取得して、ログイン中のユーザーから購読しています
-
// Instantiate an existing topic in the application scope. var topicName = "SendingAlert"; var topic = Kii.topicWithName(topicName); // Subscribe to the topic. var user = KiiUser.getCurrentUser(); user.pushSubscription().subscribe(topic).then( function(params) { var thePushSubscription = params[0]; var theTopic = params[1]; // Do something. } ).catch( function(error) { // Handle the error. var thePushSubscription = error.target; var errorString = error.message; } );
-
// Instantiate an existing topic in the application scope. var topicName = "SendingAlert"; var topic = Kii.topicWithName(topicName); // Subscribe to the topic. var user = KiiUser.getCurrentUser(); user.pushSubscription().subscribe(topic, { success: function(thePushSubscription, theTopic) { // Do something. }, failure: function(thePushSubscription, errorString) { // Handle the error. } });
グループスコープのトピックを購読する場合、以下のようになります。この例では、URL 指定されたグループより GroupTopic
という既存のトピックを取得して、ログイン中のユーザーから購読しています。
-
// Instantiate an existing group. var group = KiiGroup.groupWithURI(groupUri); // Instantiate an existing topic in the group scope. var topicName = "GroupTopic"; var topic = group.topicWithName(topicName); // Subscribe to the topic. // (The current user must be a group member) var user = KiiUser.getCurrentUser(); user.pushSubscription().subscribe(topic).then( function(params) { var thePushSubscription = params[0]; var theTopic = params[1]; // Do something. } ).catch( function(error) { // Handle the error. var thePushSubscription = error.target; var errorString = error.message; } );
-
// Instantiate an existing group. var group = KiiGroup.groupWithURI(groupUri); // Instantiate an existing topic in the group scope. var topicName = "GroupTopic"; var topic = group.topicWithName(topicName); // Subscribe to the topic. // (The current user must be a group member) var user = KiiUser.getCurrentUser(); user.pushSubscription().subscribe(topic, { success: function(thePushSubscription, theTopic) { // Do something. }, failure: function(thePushSubscription, errorString) { // Handle the error. } });
ユーザースコープのトピックを購読する場合、以下のようになります。この例では、ログイン中のユーザーの MyTODO
という既存のトピックを取得して、同じユーザーから購読しています。
-
// Instantiate an existing topic in the user scope. var topicName = "MyTODO"; var topic = KiiUser.getCurrentUser().topicWithName(topicName); // Subscribe to the topic. var user = KiiUser.getCurrentUser(); user.pushSubscription().subscribe(topic).then( function(params) { var thePushSubscription = params[0]; var theTopic = params[1]; // Do something. } ).catch( function(error) { // Handle the error. var thePushSubscription = error.target; var errorString = error.message; } );
-
// Instantiate an existing topic in the user scope. var topicName = "MyTODO"; var topic = KiiUser.getCurrentUser().topicWithName(topicName); // Subscribe to the topic. var user = KiiUser.getCurrentUser(); user.pushSubscription().subscribe(topic, { success: function(thePushSubscription, theTopic) { // Do something. }, failure: function(thePushSubscription, errorString) { // Handle the error. } });
いずれの例においても、以下の処理が行われています。
- 購読対象となるトピックを特定。
- トピックの
pushSubscription
メソッドを実行し、KiiPushSubscription インスタンスを作成。 - 対象トピックを指定して
subscribe
メソッドを実行。