Push Notification Implementation Tips

This page introduces some technical information useful for implementing the push notification features on Android.

Most of the technical information covered is non-Kii Cloud specification. If you need more information on the APIs covered on this topic, consult the official Android documents and some technical websites.

Showing a message on the status bar

When an Android device receives a push notification, it only calls a dedicated method. It does not show any message on the screen by default. To show a message on the screen like the screenshot below, you need to implement your application accordingly (Note that the design of the status bar is different for each device model).

The following sample code illustrates how to show such a message on the status bar when the device receives Direct Push notification. This sample code is based on the sample code provided by Google (https://github.com/googlesamples/google-services.git); it is arranged for Kii Cloud.

private static final String CHANNEL_ID = "my_channel";

private void displayNotification(Context context, DirectPushMessage directMessage) {
  Intent intent = new Intent(context, MainActivity.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
          PendingIntent.FLAG_ONE_SHOT);

  String title = directMessage.getMessage().getString("title", "");
  String message = directMessage.getMessage().getString("message", "");
  Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  NotificationCompat.Builder builder;
  if (Build.VERSION.SDK_INT >= 26) {  // 26: Build.VERSION_CODES.O
      builder = new NotificationCompat.Builder(context, CHANNEL_ID);
  } else {
      builder = new NotificationCompat.Builder(context);
  }
  builder.setSmallIcon(R.drawable.ic_launcher)
         .setContentTitle(title)
         .setContentText(message)
         .setAutoCancel(true)
         .setSound(defaultSoundUri)
         .setContentIntent(pendingIntent);

  NotificationManager notificationManager = (NotificationManager)
          context.getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.notify(0, builder.build());
}

In this sample code, we are using two custom key-value pairs (title and message) in the Direct Push message to get the title and message to show on the status bar. We are also using the PendingIntent to call the MainActivity of the application when a user clicks the status bar.

You can use custom key-value pairs similarly for the Push to User notification. For the Push to App notification, you need to either use the predefined keys or select a predefined message based on the type of event that triggered the push notification.

Since the size of the payload in FCM is limited to 4 KB, you need to be careful if you want to handle a long message.

In the sample code, we are checking if the Android version is below 8.0 for determining how we should initialize the NotificationCompat.Builder. Using a constructor with the CHANNEL_ID will give us an error if the targetSdkVersion set in the build.gradle file is below 26 (earlier than Android 8.0), so we need to initialize the builder without setting the ID as coded in the else section. If you are setting the targetSdkVersion to 26 or greater, use the android.support.v4.app.NotificationCompat package for the Builder class; using the v7 will give you an error.

You need to be careful if you want to show a message in the status bar on Android 8.0 or later. See Notification channel for more information.

To learn more on how to use the status bar, see the Android documentation.

The screenshot below illustrates how to send a Direct Push message on the developer portal. On the developer portal, we are setting the custom keys title and message. As we've already seen, these custom key-value pairs are interpreted by our sample code to show the message on the status bar.

Notification channel

Staring from Android 8.0, you can specify a notification channel when you are to show a message in the status bar.

When you set the targetSdkVersion in the build.gradle file to 26 or above (Android 8.0 or later), a message without a notification channel will not appear on the status bar.

targetSdkVersion Running OS version If a message shows up on the status bar
Below 26 Android 7.1 or earlier A message will show up without a notification channel.
Below 26 Android 8.0 or later A message will show up without a notification channel.
26 or above Android 7.1 or earlier A message will show up without a notification channel.
26 or above Android 8.0 or later A message will NOT show up without a notification channel.

A notification channel is a feature for categorizing push notifications on the basis of your mobile app's needs. For example, you can categorize messages to be shown on the status bar in channels like "Information" and "Sales Info". Users can enable/disable notifications per channel. They can also configure a LED color and vibration pattern per channel.

The next screenshot shows an example of selecting notification channels of "KiiTest" app in the Android settings.

A notification channel is initialized as follows:

final String CHANNEL_ID = "my_channel";
final String CHANNEL_ID2 = "my_channel2";

if (Build.VERSION.SDK_INT >= 26) {  // 26: Build.VERSION_CODES.O
  NotificationChannel channel1 = new NotificationChannel(
          CHANNEL_ID,
          getString(R.id.notification_channel_information),
          NotificationManager.IMPORTANCE_DEFAULT
  );
  notificationManager.createNotificationChannel(channel1);

  NotificationChannel channel2 = new NotificationChannel(
          CHANNEL_ID2,
          getString(R.id.notification_channel_sales),
          NotificationManager.IMPORTANCE_DEFAULT
  );

  NotificationManager notificationManager = (NotificationManager)
          context.getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.createNotificationChannel(channel2);
}

In this example, two notification channels are initialized and registered in the NotificationManager.

Two IDs "my_channel" and "my_channel2" are set in NotificationChannel's constructor. These IDs are to be specified in NotificationCompat.Builder's constructor for identifying a notification channel of messages to be shown on the status bar.

The second argument sets a string to be shown on the configuration screen. The example assumes that you will get strings like "Information" and "Sales Info" with the getString() method.

You can further specify a LED color and vibration pattern with setter methods of the NotificationChannel instance.