Tips and tricks for Android Kii developers

Here’s a few tricks that you can do with Kii Cloud when using the Android SDK. Even if they are unrelated, they prove to be quite useful during daily development.


1) Sorting query results by object creation time

There a few reserved fields that are set automatically in Kii Object but that you can still use in query conditions:

  • _id
  • _created
  • _modified

Here’s a code snippet that shows you how to sort the results of a query by object creation time:

KiiQuery query = new KiiQuery().sortByAsc("_created");
List results = bucket.query(query).getResult();

Even though _id is unique you’ll soon find out that KiiObject#getId() is protected. So we recommend you use KiiObject#toUri()1 if you want to grab a unique universal id for your objects.

2) Sharing files between users

These are basically two code snippets (one for each user) that cover the following scenario:

  1. User A and User B are both logged in.
  2. User A uploads a file
  3. User A shares the file to User B
  4. User B receives push Notification that the file is available, with the URI contained in the Push Message
  5. User B acknowledges that he has received the file with a Push Message from User A.

Code snippet for user 1

// Assume already logged in as user A
final KiiObject obj = Kii.user().bucket("Shares").object();  
File source = new File(Environment.getExternalStorageDirectory(), "filetoshare.jpg");

// Upload file from user A
KiiUploader up = obj.uploader(this, source);  
try {  
} catch (AlreadyStartedException e) {
    // Handle error.
} catch (SuspendedException e) {
    // Handle error.
} catch (TerminatedException e) {
    // Handle error.
} catch (StateStoreAccessException e) {
    // Handle error.

KiiUser userB = null;  
// Grant access from user B
try {  
    userB = KiiUser.findUserByEmail("");
    KiiACL acl = obj.acl();
    KiiACLEntry grant = new KiiACLEntry(userB,
            KiiACL.ObjectAction.READ_EXISTING_OBJECT, true);
} catch (AppException e) {
    // Handle error.
} catch (IOException e) {
    // Handle error.
} catch (ACLOperationException e) {
    // Handle error.

// Uri sharing using topic
// Topic can be created in App/ Group/ User scope. So you can choose appropriate one.
// In this example using user scope.

// Assume This topic has created by user B and user A has granted to send message to this topic. 
KiiTopic topic = userB.topicOfThisUser("FileShare");  
try {

    KiiPushMessage.Data data = new KiiPushMessage.Data();
    data.put("furi", obj.toUri().toString());
    KiiPushMessage msg = KiiPushMessage.buildWith(data)
} catch (AppException e) {
    // Handle error.
} catch (IOException e) {
    // Handle error.

Code snippet for user 2

// Assume already logged in as user B
// Create topic for sharing.
KiiTopic topic = KiiUser.topic("FileShare");  
try {;
    // Subscribe to the topic to get the notification.
    KiiPushSubscription subscription = KiiUser.getCurrentUser()

    // Grant sending message to the topic from any authenticated user (includes user A).
    KiiACL acl = topic.acl();
    acl.putACLEntry(new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
            KiiACL.TopicAction.SEND_MESSAGE_TO_TOPIC, true));;

} catch (AppException e) {
    // Handle error.
} catch (IOException e) {
    // Handle error.
} catch (ACLOperationException e) {
    // Handle error.

3) Six things to check when you Android app does not receive push notifications via Kii Cloud

Well, this one is *very* Android specific because it involves push notifications (and push mechanisms are quite different between operating systems).

If your app doesn’t receive GCM message, please check the followings.

  1. Did you put API key to your app on  If no, please get API key on Google API console and put it in your app.
  2. Did you add GooglePlayService project to your app project?  If no, please install GooglePlayServe project on Android SDK manager and link it to your app project
  3. Did you add the following entries to your AndroidManifest.xml? Confirm that {package} must be your app’s package name.
    <manifest package="{package}" ...>
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="" />
    <permission android:name="{package}.permission.C2D_MESSAGE" 
        android:protectionLevel="signature" />
    <uses-permission android:name="{package}.permission.C2D_MESSAGE" />
    <application ...>
            android:permission="" >
                <action android:name="" />
                <category android:name="{package}" />
  4. Did you add your Receiver class to your app project? Double check to make sure the receiver’s FQDN must match the one in AndroidManifest.xml
  5. Did you call KiiUser.pushInstallation().install() after user login?
    If no, please call the following APIs after user login in AsyncTack.

    GoogleCloudMessaging gcm;
    gcm = GoogleCloudMessaging.getInstance(getActivity().getApplicationContext());
    String regId = gcm.register(SENDER_ID);
  6. Did you subscribe to a bucket or a topic?  If no, please call user.pushSubscription().subscription()

Hope you liked these snippets.  We’ll have more tips and tricks soon, please stay tuned!

- Share -


More posts from

  • Kii IoT platform enables customers to create smart, connected solutions and services
  • Long track record of supporting customers like Docomo, Toshiba, Kyocera and many others
  • Strong ecosystem partners across the globe, consisting of carriers, device/sensor manufacturers, system integrators, network/infrastructure providers, chip vendors and solution developers
  • HQ in Tokyo, US HQ in Silicon Valley & offices in China, Taiwan, Hong Kong and Europe (UK, Spain, Germany)