トピックの作成
ここでは、スコープごとにトピックの作成方法を説明していきます。
アプリケーションスコープのトピックの作成
アプリケーションスコープのトピックは、アプリ開発者のみ作成できます。作成したトピックは、全てのアプリユーザーから購読できますが、このトピックにメッセージを送信できるのは、デフォルト状態ではトピック作成者(つまりアプリ開発者)だけです。
アプリケーションスコープのトピックを作成する例を以下に挙げます。
-
// Authenticate as the app administrator. // Check the ClientID and ClientSecret in the Kii Cloud developer portal. Kii.authenticateAsAppAdmin("ClientID", "ClientSecret").then( function(adminContext) { // Create a topic in the application scope. var topicName = "SendingAlert"; var topic = adminContext.topicWithName(topicName); // Save the topic to Kii Cloud. return topic.save(); } ).then( function(theTopic) { // Do something. } ).catch( function(error) { // Handle the error. // Get the topic for the failed topicWithName() method. var theTopic = error.target; // Get the error message. var errorString = error.message; } );
-
// Authenticate as the app administrator. // Check the ClientID and ClientSecret in the Kii Cloud developer portal. Kii.authenticateAsAppAdmin("ClientID", "ClientSecret", { success: function(adminContext) { // Create a topic in the application scope. var topicName = "SendingAlert"; var topic = adminContext.topicWithName(topicName); // Save the topic to Kii Cloud. topic.save({ success: function(theTopic) { // Do something. }, failure: function(errorString) { // Handle the error. } }); }, failure: function(errorString, errorCode) { // Handle the error. } });
トピック名は英数字、"-"、"_" から構成される 64 文字までの文字列として指定します。
グループスコープのトピックの作成
グループスコープのトピックは、グループメンバーであれば誰でも作成可能です。また、デフォルト状態ではグループメンバーであれば誰でも購読可能であり、全てのグループメンバーがトピックに対してメッセージを送信できます。
グループスコープのトピックは通常アプリケーションによって動的に作成します。以下に、グループスコープのトピックを作成する例を挙げます。
-
// Create a group. var groupName = "myNewGroup"; var group = Kii.groupWithName(groupName); group.save().then( function(theGroup) { // Create a topic in the group scope. var topicName = "GroupTopic"; var topic = theGroup.topicWithName(topicName); // Save the topic to Kii Cloud. return topic.save(); } ).then( function(theTopic) { // Do something. } ).catch( function(error) { // Handle the error. // Get the group for the failed save() method. var theGroup = error.target; // Get the topic for the failed topicWithName() method. var theTopic = error.target; // Get the error message. var errorString = error.message; } );
-
// Create a group. var groupName = "myNewGroup"; var group = Kii.groupWithName(groupName); group.save({ success: function(theGroup) { // Create a topic in the group scope. var topicName = "GroupTopic"; var topic = theGroup.topicWithName(topicName); // Save the topic to Kii Cloud. topic.save({ success: function(theTopic) { // Do something. }, failure: function(errorString) { // Handle the error. } }); }, failure: function(theGroup, errorString) { // Handle the error. } });
ここでは、以下の処理を実行しています。
topicWithName
メソッドを実行してグループスコープのトピックを作成します。save
メソッドを実行してトピックを Kii Cloud に保存します。
トピック名は英数字、"-"、"_" から構成される 64 文字までの文字列として指定します。
ユーザースコープのトピックの作成
ユーザースコープのトピックは、ログイン済みのユーザーであれば誰でも作成可能です。このトピックはデフォルト状態ではユーザーに閉じており、トピックの購読およびトピックへのメッセージ送信を行えるのはこのユーザーのみになります。
ユーザースコープのトピックは通常アプリケーションによって動的に作成します。以下に、ユーザースコープのトピックを作成する例を挙げます。
-
// Create a topic in the user scope. var topicName = "MyTODO"; var user = KiiUser.getCurrentUser(); var topic = user.topicWithName(topicName); // Save the topic to Kii Cloud. topic.save().then( function(theTopic) { // Do something. } ).catch( function(error) { // Handle the error. // Get the topic for the failed topicWithName() method. var theTopic = error.target; // Get the error message. var errorString = error.message; } );
-
// Create a topic in the user scope. var topicName = "MyTODO"; var user = KiiUser.getCurrentUser(); var topic = user.topicWithName(topicName); // Save the topic to Kii Cloud. topic.save({ success: function(theTopic) { // Do something. }, failure: function(errorString) { // Handle the error. } });
ここでは、以下の処理を実行しています。
topicWithName
メソッドを実行してユーザースコープのトピックを作成します。save
メソッドを実行してトピックを Kii Cloud に保存します。
トピック名は英数字、"-"、"_" から構成される 64 文字までの文字列として指定します。