Push Notification with FCM

To use the FCM-based push notification, you need to make some FCM-related configuration and prepare your build environment besides configuring Kii Cloud.

See Using FCM for the overview of FCM.

Note: The FCM-based push notifications are currently not supported on the China server (Site.CN3)

Setting up

You need to make the various configuration to use the FCM-based push notification. We have a tutorial that will guide you through the basics. This tutorial covers the contents like how to configure your project and how to start implementing skeleton codes for leveraging the push notification feature.

First, complete these steps by walking through Android (FCM) Push Notification Tutorial. Skip the steps that you have already done such as integration of the Kii Cloud SDK into your development project. Move to Modification for Thing-IF after you complete the tutorial.

  1. Registering a Firebase project: Register your project in the Firebase console and register the issued server key in the developer portal as the GCM key.

  2. Configuring the build environment: Configure the library and permissions for push notification in the build environment.

  3. Implementing and testing the program: Implement the initialization logic and handler to the mobile app and try to send a push notification.

The tutorial covers how to set up the push notification for Kii Cloud SDK. Follow the same instruction to prepare your environment.

Modification for Thing-IF

Once you are done with the tutorial, we will remove lines that are unnecessary for mobile apps with the Thing-IF SDK. Replace the lines for device installation that use the Kii Cloud SDK API with those that use the Thing-IF SDK API. This is because the method for device installation is different between the two SDKs.

From the implementation of MainActivity, delete the part that is calling the KiiUser.pushInstallation and the line above (the one declaring the development).

The updated sample code should look like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ......
  if (!checkPlayServices()) {
    Toast.makeText(MainActivity.this, "This application needs Google Play services", Toast.LENGTH_LONG).show();
    return;
  }

  String username = "user1";
  String password = "123ABC";
  KiiUser.logIn(new KiiUserCallBack() {
    @Override
    public void onLoginCompleted(int token, KiiUser user, Exception exception) {
      if (exception != null) {
        Toast.makeText(MainActivity.this, "Error login user:" + exception.getMessage(), Toast.LENGTH_LONG).show();
        return;
      }
      Toast.makeText(MainActivity.this, "Succeeded to login", Toast.LENGTH_LONG).show();

      String fcmToken = FirebaseInstanceId.getInstance().getToken();
      if (fcmToken == null) {
        Toast.makeText(MainActivity.this, "Error FCM is not ready", Toast.LENGTH_LONG).show();
        return;
      }
    }
  }, username, password);
}

Leave the sample code as the above for now. We will insert the API call for Thing-IF later. See Installing a device in the Initializing and Onboarding topic for more information.

Also, according to the specification of your mobile app, rewrite the login processing that uses user1 for testing. The sample code in the Initializing and Onboarding topic uses a pseudo user as a thing owner. If you implement a login screen where an explicit owner user logs in, move the above login processing to the processing class of the login screen.