Skip to content

Push Server Integration

Petr Dvorak edited this page Nov 10, 2016 · 13 revisions

In order to register devices and send push notifications, the Push Server needs to be integrated with server applications. The first application that must communicate with the Push Server is a mobile API application (the one that publishes service specific API for mobile application).

Prerequisites for the tutorial

  • Running PowerAuth 2.0 Server with available SOAP interface.
  • Running PowerAuth 2.0 Push Server with available RESTful interface.
  • Knowledge of applications based on Spring Framework.
  • Software: IDE - Spring Tool Suite, Java EE Application Server (Pivotal Server, Tomcat, ...)

Common integration steps

Adding Maven Dependency

In order to be able to implement integration easily, add Push Server client Maven dependency to your pom.xml file:

<dependency>
    <groupId>io.getlime.security</groupId>
    <artifactId>powerauth-push-server-client</artifactId>
    <version>0.13.0</version>
</dependency>

Prepare a Service Client Configuration

In order to connect to the PowerAuth 2.0 Push Server, you need to add following configuration:

@Configuration
@ComponentScan(basePackages = {"io.getlime.push"})
public class PowerAuthPushConfiguration {

  @Bean
  public PushServerClient pushServerClient() {
    PushServerClient client = new PushServerClient();
    client.setServiceBaseUrl("http://localhost:8080/powerauth-push-server");
    return client;
  }

}

Integration With Mobile API

Prepare the Device Registration Endpoint

In order to implement generic device registration, implement a custom registration RESTful Endpoint that calls the Push Server under the hood, like so:

@Controller
@RequestMapping(value = "push")
public class DeviceRegistrationController {

    @Autowired
    private PushServerClient pushServerClient;

    @RequestMapping(value="device/create", method = RequestMethod.POST)
    public @ResponseBody String registerDevice(@RequestBody Map<String,String> request) {

        // Get the values from the request
        String platform = request.get("platform");
        String token = request.get("push_token")

        // Check if the context is authenticated - if it is, add activation ID.
        // This assures that the activation is assigned with a correct device.
        String activationId = null;
        PowerAuthApiAuthentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            activationId = authentication.getActivationId();
        }

        // Register the device and return response
        boolean result = pushServerClient.registerDevice(token, platform, activationId);
        if (result) {
            return "OK"
        } else {
            return "NOT_OK"
        }
    }
}

In case you would like to assure strong bonding of the token right after login, you can add device registration call right in your login method as well, for example like this (note that the client app must send the push token with the login request):

@Controller
@RequestMapping(value = "session")
public class AuthenticationController {

  @RequestMapping(value = "login", method = RequestMethod.POST)
  @PowerAuth(resourceId = "/session/login")
  public @ResponseBody PowerAuthAPIResponse<String> login(@RequestBody Map<String, String> request, PowerAuthApiAuthentication apiAuthentication) {

      // Get the values from the request
      String platform = request.get("platform");
      String token = request.get("push_token")

      // Perform the classic login procedure by adding authentication object to your context
      SecurityContextHolder.getContext().setAuthentication(apiAuthentication);

      // Get activation ID right from the authentication object
      String activationId = apiAuthentication.getActivationId();

      // Register the device and return response
      boolean result = pushServerClient.registerDevice(token, platform, activationId);

      return new PowerAuthAPIResponse<String>("OK", null);
  }
}

Note: We are using 'dummy' request (Map<String,String>) and response (String) objects. Replace them with your proprietary classes.

Integrate with Push Producer Applications

Push Producer Application is any application that sends push notification request. The purpose of the producer applications is to call service for push notification sending (single or batch):

// Prepare push message body
PushMessage push = new PushMessage();
push.setUserId("123");
push.setAppId(2);
push.setSilent(false);
push.setPersonal(true);
push.setEncrypted(true);
push.getMessage().setTitle("Balance update");
push.getMessage().setBody("Your balance is now $745.00");
push.getMessage().setValidUntil(ISODate("2016-10-12T11:20:04Z").date());

// Send single push message
pushServerClient.sendNotification(push);

// Send push message batch
List<PushMessage> messageList = new ArrayList<>();
messageList.add(push);
pushServerClient.sendNotificationBatch(messageList);