public void achievementUnlocked(@Named("message") String achievement, User user)
      throws ServiceException {

    if (user == null && !Configuration.ALLOW_UNAUTHENTICATED_CALLS) {
      throw new UnauthorizedException("Only authenticated calls are allowed");
    }

    // The actual game backend would probably validate the achievement first
    // and then store it in Datastore. Then it might send push notification
    // to user's friends.
    // In this sample only the send part is implemented and instead of
    // retrieving the list of user's friends, the code just retrieves
    // a few most recently registered devices and sends an alert to them

    String playerName =
        (user == null || user.getEmail() == null) ? "Anonymous User" : user.getEmail();

    String alert = String.format("%1$s unlocked achievement '%2$s'", playerName, achievement);

    EntityManager mgr = null;
    try {
      mgr = getEntityManager();
      Query query =
          mgr.createQuery("select deviceToken from DeviceRegistration d order by timestamp");

      query.setFirstResult(0);
      query.setMaxResults(5);

      @SuppressWarnings("unchecked")
      List<String> deviceTokensOfFriends = query.getResultList();

      log.info("Sent achievement unlocked push notification to " + deviceTokensOfFriends.size()
          + " friend(s) of " + playerName);
      PushNotificationUtility.enqueuePushAlert(alert, deviceTokensOfFriends);
    } finally {
      mgr.close();
    }
  }