Create an Android Application
First, let us create a new application and integrate the Kii Cloud SDK. Let us also do a quick test to see if the SDK is integrated correctly.
Creating a new application
We will create a new Android application using Android Studio.
We assume that you will create your project as follows:
- Specify the default name
My Application
as the application name. - Create a new project in the package
com.example.pushtest
while leaving other settings as default. - Select "Phone & Tablet".
- Select "Empty Activity".
- Leave other settings as default.
When the application is created, build and run it. If the application does not run correctly, there is something wrong with the development environment. Double check the environment and solve the issue.
Integrating the Kii Cloud SDK
We will now prepare Kii Cloud SDK by following the steps described in the Android Quick Start guide.
Follow the quick start guide link in each step to see the detailed procedures. Once you've followed the procedures in the quick start guide, come back to this page and proceed to the next step.
We will integrate the SDK in the following two steps:
Create an application in Kii Cloud
Log in to the Kii Cloud developer portal and create a new application. The push notification will be executed under this application.
- Set an easy-to-remember application name, like "Push Notification".
- Set the server location to the nearest region from where you are going to distribute the application.
After the application is created in Kii Cloud, you will get the AppID for identifying the application.
Open Creating a Kii Application and complete all the steps excluding the section "Adding collaborators". When you are done, come back to this page and proceed to the next step.
Integrate the Kii Cloud SDK
Integrate the Kii Cloud SDK in the Android application you've created in the previous step.
You can either download the library from Maven repository or manually download it by yourself.
Open Adding the Kii Cloud SDK and complete all the steps. When you are done, come back to this page and proceed.
You do not need to use the procedures covered in the "Enabling the Kii Push Notification Feature" topic. We will present the detailed procedures in this tutorial.
Testing the Kii Cloud feature
At this moment, you should be able to call Kii Cloud APIs in your application. To test if the SDK is integrated correctly, we will now create a new user.
The user we create here will be used later. In Kii Cloud, the destination of push notifications is a user, so we need to create a user beforehand.
We will create a new user with the following fixed username and password for now (in developing a real application, you would most likely want to create a user in more practical way; check the implementation tips at the end of this tutorial):
Username:
user1
Password:
123ABC
Insert the following code in the place where it can be executed easily, like the onCreate
in the MainActivity
class.
String username = "user1";
String password = "123ABC";
KiiUser.Builder builder = KiiUser.builderWithName(username);
KiiUser user = builder.build();
user.register(new KiiUserCallBack() {
@Override
public void onRegisterCompleted(int token, KiiUser user, Exception exception) {
if (exception != null) {
// Handle the error.
Toast.makeText(MainActivity.this, "Error register user:" + exception.getMessage(), Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(MainActivity.this, "Succeeded", Toast.LENGTH_LONG).show();
}
}, password);
Adding the above code causes symbol resolution errors for the Kii Cloud SDK and the Android SDK. Add appropriate import statements by pressing Alt + Enter in Android Studio or anything. Fix resolution errors in the same way when you paste sample code from now on.
Here is what is happening in the sample code:
- Execute the
register
method to create a new Kii Cloud user with the fixed username and password. - Receive the user registration result with a callback method
onRegisterCompleted
and display a message with Toast.
Test run
Now, let's launch and run the application. The user registration is successful if the message "Succeeded" pops up after the application is launched.
If you launch the application more than once, the "USER_ALREADY_EXISTS" error will show up since the application will attempt to register the duplicating user. You can ignore the error in this case because the user has already been created.
If you see another error message, double-check all steps again. For example, make sure that the Kii.initialize
method is correctly executed to integrate the SDK. Also, make sure that the correct AppID and server location are set.
Deleting the user creation process
Once a user is created, the above sample code is no longer needed. Delete or comment out the code.
Let us move to the next step: Create a Google Project.
<< Android (GCM) Push Notification Tutorial | Create a Google Project >> |