Approvals

An approval allows you to send a request to one or multiple approvers through a channel. Approvers can review information associated with the approval and accept or reject the approval. You will receive response results from each approver. The approval-related APIs are described below.

Create an Approval

The following example shows how to create an approval named NewApproval.

String title = "New Approval"; String description = "Description..."; List<UserProfile> users = Arrays.asList(userObject1, userObject2); ApprovalMode mode = ApprovalMode.ANY_ORDER; //create channel that will be added inside of approval MMXChannel innerChannel; Set<String> subscriberIds = new HashSet<>(users.size()); for (UserProfile up : users) { subscriberIds.add(up.getUserIdentifier()); } MMXChannel.create(UUID.randomUUID().toString(), "MMXApproval", false, MMXChannel.PublishPermission.SUBSCRIBER, subscriberIds, new MMXChannel.OnFinishedListener<MMXChannel>() { @Override public void onSuccess(MMXChannel result) { innerChannel = result; } @Override public void onFailure(MMXChannel.FailureCode code, Throwable throwable) { //show error } }); //Add template item if it’s necessary List<ApprovalProperty> items = new ArrayList<>(); ApprovalProperty item = new ApprovalProperty.Builder() .name("item name") .value("item value") .optional(false) .type(STRING) .build(); items.add(item); MMXApproval.Builder approvalBuilder = new MMXApproval.Builder(); approvalBuilder.title(title) .description(description) .approvers(users) .properties(items) .channelId(innerChannel.getIdentifier()) .mode(newApproval.mode); //Add attachment if it’s necessary Bitmap imageData = //set image bitmap here Attachment attach = new Attachment(imageData, "image/png"); attach.upload(new Attachment.UploadListener() { @Override public void onStart(Attachment attachment) { } @Override public void onComplete(Attachment attachment) { approvalBuilder.attachment(attachment.getAttachmentId(), attachment); } @Override public void onError(Attachment attachment, Throwable error) { //show error } }); //Use channel to publish the approval MMXChannel channelToPublish = … ; MMXApproval approval = approvalBuilder.build(); approval.publish(channelToPublish, new MMX.OnFinishedListener<MMXMessage>() { @Override public void onSuccess(MMXMessage mmxMessage) { //publish approval successfully } @Override public void onFailure(MMX.FailureCode failureCode, Throwable throwable) { //show error });

Get Approvals Using approvalID

Get the approval object from the server using the approvalId.

String approvalID = … ; MMXApproval.get(approvalID , new MMX.OnFinishedListener<MMXApproval>() { @Override public void onSuccess(MMXApproval mmxApproval) { //do smth with approval } @Override public void onFailure(MMX.FailureCode failureCode, Throwable throwable){ //show error } });

Fetch Approvals from Server

The following code fetches the assigned approval objects from the server.

String query = null; List<ApprovalStatus> createdFilter = null; List<ApprovalStatus> assignedFilter = null; Integer offset = 0; Integer limit = 20; MMXApproval.find(query, createdFilter, assignedFilter, offset, limit, new MMX.OnFinishedListener<List<MMXApproval>>() { @Override public void onSuccess(List<MMXApproval> result) { //do somethings with approvals } @Override public void onFailure(MMX.FailureCode code, Throwable ex) { //show error } });

Approval Object Information Display

The approval object contains all the information needed to display in your user interface.

String title = myApproval.getTitle(); // A user-friendly name of the approval String description = myApproval.getDescription(); // The description of the approval ApprovalStatus status = myApproval.getStatus(); // Status of the approval, which includes APPROVED, DELETED, PENDING, REJECTED, CANCELED List<UserProfile> approvers = myApproval.getApprovers(); // approvers objects for the approval Map<String, Attachment> attachments = myApproval.getAttachments(); // Atatchments of the approval Map<String, String> extras = myApproval.getMetaData(); // A user-defined objects used to store arbitrary data will can accessed from the approval instance Date updatedDate = myApproval.getUpdatedDate(); // Latest updated date of the approval

Accept or Reject Approval Request

Approvers can either accept or reject an approval request, as shown below:

MMXApproval myApproval = … ; // To accept the approval myApproval.approveWithUpdates("I approve it!", new MMX.OnFinishedListener<MMXApproval>() { @Override public void onSuccess(MMXApproval result) { //accept approval successfully } @Override public void onFailure(MMX.FailureCode code, Throwable ex) { //accept approval failed, show error } }); // To reject approval myApproval.declineWithUpdates("Sorry, I reject it", new MMX.OnFinishedListener<MMXApproval>() { @Override public void onSuccess(MMXApproval result) { //reject approval successfully } @Override public void onFailure(MMX.FailureCode code, Throwable ex) { //reject approval failed, show error } });

Cancel Approval

The approval owner can also cancel the approval.

MMXApproval myApproval = … ; myApproval.cancel("The approval is cancelled", new MMX.OnFinishedListener<MMXApproval>() { @Override public void onSuccess(MMXApproval result) { //cancel approval successfully } @Override public void onFailure(MMX.FailureCode code, Throwable ex) { //cancel approval failed, show error; } });