Receiving a Direct Push Notification

To receive push notifications, you need to implement either FirebaseMessagingService or BroadcastReceiver that is defined in the AndroidManifest.xml. Kii Cloud SDK provides a helper API to support your application to parse push messages.

See the following sample code:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    // Get the push message.
    Map<String, String> payload = remoteMessage.getData();
    ReceivedMessage message = PushMessageBundleHelper.parse(payload);

    // Get the sender of the push message.
    KiiUser sender = message.getSender();
    // Refresh the sender and then access it.

    // Determine the push notification type and start parsing.
    PushMessageBundleHelper.MessageType type = message.pushMessageType();
    switch (type) {
      case PUSH_TO_APP:
        // Handle the "Push to App" message.
        break;
      case PUSH_TO_USER:
        // Handle the "Push to User" message.
        break;
      case DIRECT_PUSH:

        // Extract field values set in the developer portal.
        long when = message.getMessage().getLong("when");
        String app_specific_string = message.getMessage().getString("app_specific_key1");
        long app_specific_long = message.getMessage().getLong("app_specific_key2");

        break;
    }
  }
}

You will implement the logic as a subclass of the FirebaseMessagingService. Pass the push message payload that is obtained as the argument of the onMessageReceived method to the PushMessageBundleHelper.parse method in order to create a ReceivedMessage instance.

After creating a ReceivedMessage instance, you can get the push message content after getting the Bundle as follows:

  1. Gets the sender of the push message by calling the getSender method.
  2. Determines the type of the push message by calling the pushMessageType method.
    If the message is "Direct Push", the type will be "DIRECT_PUSH".
  3. Gets the message Bundle with the getMessage method and extracts more default and custom field values.

If you have issues in receiving push notifications, information in Troubleshooting might help you. You can also use the simple implementation illustrated in Push Notification Tutorials to investigate if the push notification is working properly.

Notification on the status bar

An Android device only executes the onMessageReceived or onReceive method when it receives a push notification. If you want to show a message on the status bar, you need to implement the logic by yourself. See Push Notification Implementation Tips for some hints.

Push message example

The following is an example of the data you can get by executing the remoteMessage.getData() method when an FCM push message is received. You can get the custom data specified on the developer portal (in this example Key1 = Value1). See the Javadoc to learn more.

{
    Key1 = Value1,
    collapse_key = do_not_collapse,
    from = 123456789012
}