Receiving Push Notification via SDK(Last Updated : 01/11/2017)


To receive Push Notifications, you must have a web server that can receive POST requests. Once a web server is ready, you must implement two different interfaces.

  • ApiResultTypeSpecFinder

    This class should return an ApiResultTypeSpec object after taking in a request ID. The ApiResultTypeSpec returned will correspond with the API service responsible for processing your request. If you cannot determine the source of the ApiResultTypeSpec, you should return null instead. The different types of ApiResultTypeSpec objects are listed below.

    • - distributionListCreation
    • - distributionListUpdate
    • - distributionListDeletion
    • - subscriptionCreation
    • - subscriptionUpdate
    • - subscriptionDeletion
    • - messageComposition
    • - scheduledMessageCancellation

  • ApiResultListener

    - This handler will be called after the ApiResultTypeSpec is found.

A ResultHandler is created by implementing these two interfaces.

ApiResultListener<MessageCompositionResult, MessageCompositionFailure> messageResultListener = new ApiResultListener<MessageCompositionResult, MessageCompositionFailure>() { @Override public void onSuccess(@Nonnull MessageCompositionResult result) { // Handle success result } @Override public void onFailure(@Nonnull MessageCompositionFailure failure) { // Handle failure result } }; ApiResultTypeSpecFinder typeSpecFinder = new ApiResultTypeSpecFinder() { @Nullable @Override public ApiResultTypeSpec<?, ?> findResultTypeSpec(@Nonnull String requestId) { if (isMessageCompositionResult(requestId)) { return ApiResultTypeSpec.messageComposition; } else { return null; } } }; ResultHandler resultHandler = new ResultHandler.Builder() .listen(ApiResultTypePair.messageComposition, messageResultListener) .build(typeSpecFinder);

There can be cases where a ResultHandler receives a push notification that it cannot process. For these cases, you can register a UnhandledApiResultListener to handle any push notification for which a listener was not registered.

ResultHandler resultHandler = new ResultHandler.Builder() .listen(ApiResultTypeSpec.messageComposition, messageResultHandler) .listenUnhandled(new UnhandledApiResultListener() { @Override public void onUnhandled(@Nonnull String rawBody) { // Handle the result manually... } }) .build(typeSpecFinder);

An instance of ResultHandler needs to be active until the Push Notification from the POST request is processed.

If your web application is built with servlets, you can store the ResultHandler object in a ServletContext and then use that instance.

You can call the handler method of a ResultHandler instance when you receive the results from a POST request. Below is an example for implementations utilizing Servlets:

public class PushNotificationReceiverServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ResultHandler resultHandler = ... resultHandler.handle(req.getReader()); resp.setStatus(HttpServletResponse.SC_OK); } }

A Reader, InputStream, or byte array can be passed in as a parameter to handle method. It is your responsibility to close InputStream and Reader objects after they are opened and data is passed in.

Based on the returned ApiResultTypeSpec, the handle method of the ResultHandler will call the callback method of the ApiResultListener provided by the user.