Creating a Topic

This page explains how to create a topic for each scope.

Creating an application-scope topic

Only an application admin can create an application-scope topic. All authenticated users can subscribe the topic, but only the topic creator (i.e. the application admin) can send push messages to the topic by default.

Creating an app-scope topic is supported via Kii REST API or JavaScript SDK. Follow these steps:

With REST API

Follow these steps:

  1. Follow the steps described in "REST guides - Admin Features" and get an app admin token.
  2. Create an app-scope topic as follows (in this example, we are creating a topic named "SendingAlert"):
curl -v -X PUT \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  "https://api-jp.kii.com/api/apps/{APP_ID}/topics/SendingAlert"

Make sure to replace {APP_ID} with your application's AppID. Also, make sure to replace {ACCESS_TOKEN} with the token obtained in the previous step.

The topic name should be composed of up to 64 characters (alphanumeric, "-" and "_").

With the Kii Cloud SDK for JavaScript

See Creating an application-scope topic in the JavaScript Programming Guide.

Creating a group-scope topic

Any group members can create a group-scope topic. All group members can subscribe to the topic, and all of them can send push messages to the topic by default.

Unlike an app-scope topic, a group-scope topic is usually created dynamically by an application. See the following sample code to see how to do this:

Swift:

  • // Create a group.
    let group = KiiGroup(name: "group name")
    
    do{
      try group.saveSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
    
    // Create a topic in the group scope.
    let topicName = "GroupTopic"
    let topic = group.topic(withName: topicName)
    
    do{
      // Save the topic to Kii Cloud.
      try topic.saveSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Create a group.
    let group = KiiGroup(name: "group name")
    group.save { (group : KiiGroup?, error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    
      // Create a topic in the group scope.
      let topicName = "GroupTopic"
      let topic = group!.topic(withName: topicName)
    
      // Save the topic to Kii Cloud.
      topic.save { (topic , error : Error?) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
      }
    }

Objective-C:

  • NSError *error;
    
    // Create a group.
    KiiGroup *group = [KiiGroup groupWithName:@"group name"];
    [group saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Create a topic in the group scope.
    NSString *topicname = @"GroupTopic";
    KiiTopic *topic = [group topicWithName:topicname];
    
    // Save the topic to Kii Cloud.
    [topic saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Create a group.
    KiiGroup *group = [KiiGroup groupWithName:@"group name"];
    [group saveWithBlock:^(KiiGroup *group, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Create a topic in the group scope.
      NSString *topicname = @"GroupTopic";
      KiiTopic *topic = [group topicWithName:topicname];
    
      // Save the topic to Kii Cloud.
      [topic saveWithBlock:^(KiiTopic *topic, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
      }];
    }];

Here is a brief description of the sample code:

  • Creates a group-scope topic by calling the topicWithName: method.
  • Save the topic by calling the saveSynchronous: method.

The topic name should be composed of up to 64 characters (alphanumeric, "-" and "_").

Creating a user-scope topic

Any authenticated users can create a user-scope topic. Only this user can subscribe to the topic and send push messages to it by default.

Like a group-scope topic, a user-scope topic is usually created dynamically by an application. See the following sample code to learn how to create a user-scope topic.

Swift:

  • // Create a topic in the user scope.
    let user = KiiUser.current()!
    let topicName = "MyTODO"
    let topic = user.topic(withName: topicName)
    
    do{
      // Save the topic to Kii Cloud.
      try topic.saveSynchronous()
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Create a topic in the user scope.
    let user = KiiUser.current()!
    let topicName = "MyTODO"
    let topic = user.topic(withName: topicName)
    
    // Save the topic to Kii Cloud.
    topic.save { (topic , error : Error?) -> Void in
      if error != nil {
        // Handle the error.
        return
      }
    }

Objective-C:

  • NSError *error;
    
    // Create a topic in the user scope.
    KiiUser* user = [KiiUser currentUser];
    NSString *topicname = @"MyTODO";
    KiiTopic *topic = [user topicWithName:topicname];
    
    // Save the topic to Kii Cloud.
    [topic saveSynchronous:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Create a topic in the user scope.
    KiiUser* user = [KiiUser currentUser];
    NSString *topicname = @"MyTODO";
    KiiTopic *topic = [user topicWithName:topicname];
    
    // Save the topic to Kii Cloud.
    [topic saveWithBlock:^(KiiTopic *topic, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

Here is a brief description of the sample code:

  • Creates a user-scope topic by calling the topicWithName: method.
  • Saves the topic by calling the saveSynchronous: method.

The topic name should be composed of up to 64 characters (alphanumeric, "-" and "_").