Working with push messages in Android

PingID SDK utilizes push messaging in order to authenticate the end users. PingID SDK can work side by side within an app that uses push messaging. This page details the steps needed in order to work with push messages in Android.

Your application will receive push messages from the PingID SDK server, and also from other sources. As a result, your implementation of the push messaging service (GcmListenerService or FirebaseMessagingService) will have a higher priority than the PingID SDK component’s push messaging listener. Your push messaging service implementation will then have to differentiate between push messages sent from the PingID SDK server and other messages, and pass them to the PingID SDK component for processing.

  1. Configure the push messaging sender ID, according to the instructions in Getting started with Android in PingID SDK.

  2. In your app, add the appropriate sections in your androidmanifest.xml file (GCM listener / FCM messaging service), and add the appropriate class.

  3. You will need to differentiate between push messages for PingID SDK and for other types of push messages you might send.

    1. Add code that checks if the message received is intended for the PingID SDK component or for your application. In order to easily differentiate between the types of push messages, the data sent from the PingID SDK server will contain a key called "pingidsdk", which will have a value of "true".
    2. If the message received is intended for the PingID SDK component, invoke the static function PingIdPushHelper.handlePushMessage with the data from the push message, as follows:

      PingIdPushHelper.handlePushMessage(getApplicationContext(), from, data);

      The PingIdPushHelper.handlePushMessage function receives three arguments:
      • Application context
      • Push sender ID \(from argument\)
      • Bundle with the data from the server \(data argument\)
  4. Examples:

    • GCM example:

      Pass the arguments you received in your GcmListenerService class, as follows:

      @Override
      public void onMessageReceived(String from, Bundle data) {
      	if (data.containsKey("pingidsdk") && data.getString("pingidsdk").equals("true")) {
          	Log.i(TAG, "this Gcm msg is intended for the PingID SDK. msg:" + data.getString("message"));
        pingidsdkclient.fcm.PingIdPushHelper.handlePushMessage(getApplicationContext(), from, data);
        } else{
          	//This message is not a PingID SDK, continue processing...
        }
      }
    • FCM example:

      Extract the bundle data and sender ID from the RemoteMessage object you received in your FirebaseMessagingService class, as follows:

      @Override
      public void onMessageReceived(RemoteMessage message) {
      	Map<String, String> dataMap = message.getData();
       if (dataMap.containsKey("pingidsdk") && dataMap.get("pingidsdk").equals("true")) {
      
          	Log.i(TAG, "this Fcm msg is intended for the PingID SDK. msg:" + dataMap.get("message"));
      
        //we need to convert the data from the RemoteMessage to a Bundle
      
        Bundle data = new Bundle();
       for (Map.Entry<String, String> entry : dataMap.entrySet()) {
              	data.putString(entry.getKey(), entry.getValue());
      
        }
          	PingIdPushHelper.handlePushMessage(getApplicationContext(), message.getFrom(), data);
        } else {
          	//This message is not a PingID SDK message, continue processing...
        }
      }

    Please review the demo app to see a complete example of push messages for Android in PingID SDK.