Defining an Owner
To operate a thing from a mobile application, you need to specify a Kii Cloud user as the thing owner. See here for the overview.
You can create a user with the User Management Feature of the Kii Cloud SDK. After you create a user, you will pass the user ID and access token of this user to the initialization API.
See Logging in and Using an Access Token to learn more about the access token.
There are several ways for creating a user. This guide uses the pseudo user which is also used in the development guides for Android and iOS.
By using the pseudo user, you will create a user without any name and keep using their access token thereafter. Since it does not require any username or password, you can leverage the user without any explicit login.
This method is the easiest if your scenario is simple (e.g., just sending commands and browsing state). However, especially when you use a web browser, be careful that you will not be able to access the pseudo user if you lose the saved access token.
Should you want to secure a login method after the access token is cleared or develop more complex apps by leveraging the Kii Cloud SDK features, you can create a user or group and set them as the thing owner. See Implementation tips at the bottom of this page for more discussion.
The following sample code shows an example of creating a pseudo user. In this example, a new pseudo user is created and their user ID and access token are stored in the device storage (please note that the implementation of the data storing method is omitted in the sample code).
The access token and user ID are used when initializing the Thing-IF SDK. If you lose the access token, you will no longer be able to access to this user because there will be no way to distinguish the user.
The basic steps are as follows.
- Execute the
registerAsPseudoUsermethod to create and register a pseudo user.
- After the user creation and login is executed, execute the
getIDmethods to get the access token and the user ID of this pseudo user, respectively. Then, save them in the device.
Next, execute the following code to specify the user who will be the thing owner. Finally, pass
accessToken obtained in the above code to the constructor of
ThingIFAPI when you initialize the Thing-IF SDK.
This code creates a
TypedID with the user ID and
Types.User that specifies the type of the ID. If you are adding a group as an owner, pass the group ID with
By using this "pseudo user as a thing owner" implementation on multiple devices, you will be able to share a thing among users who own the devices. This is done by binding the thing to the multiple users (i.e., pseudo users). This approach is OK as the Thing-IF SDK supports sharing a thing by multiple owners.
If you are planning to expand your application with the Kii Cloud SDK features (e.g., use the data management to store extra data), this simple relationship between the thing and users might not be sufficient (e.g., the user scope data are created per device, and they cannot be shared among users). Please refer to the next section "Implementation tips".
If you simply insert the above codes in the initialization process, a new user will be created every time the application is launched. Please implement the appropriate logic to reuse the access token and user ID stored with
storeUser when the application is launched for the second time.
We've presented a method to add a pseudo user as the thing owner. Alternatively, you can use the following approaches. All of them leverage the Kii Cloud SDK features.
- Create a user with the username (or email address/phone number) and password, and let the user logs in with them (ref. Managing Users)
- Create a user with the external service account like Facebook and Twitter (ref. Authenticating with an External Service Account)
- Create a group and set the group as the thing owner to share the thing among multiple group members (ref. Managing Groups)
To get a command result, you need to receive its command ID as a push notification. However, such a push notification is sent only to the devices associated with the command sender. Design your mobile app with this point in mind.
The command ID is notified only to the device on which the command is executed because a pseudo user is unique to each device. You do not receive notifications for commands issued on other devices which share the thing.
The command ID is notified to all the devices associated with the owner by installing devices.