question

labventures avatar image
labventures asked chanel Deactivated commented

Print web orders

We developed a web application integrated with the Clover API, to insert orders from the web.We have successfully created orders via the API, but we were unable to automatically print them.

In an answer to our questions you told us we need to create an Android app with the Clover SDK, Listen to incoming orders and send to the printer via the PrintJob classes.

We tried the following solutions:

  1. Use a broadcast receiver on ACTION_ORDER_CREATED
.
    In this case I have 2 problems:
    1. I get an invalid ID on the event which results in an empty object when using orderConnector.getOrder
    2. This event triggers only when I add orders from the Register app, but not from the REST API.
  2. Use AppNotificationReceiver - I’m successfully sending the notification and gets a 200 response, but the receiver on the Android app never triggers.
(https://apisandbox.dev.clover.com/v3/apps/M1PQGMQYFXCG6/merchants/P7EWCVM316RDY/notifications)
* I’m testing on Clover Station 2018 emulator

The question is, what is the proper way to retrieve new order objects for the PrintJob?
SandboxPrint
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

chanel avatar image
chanel Deactivated answered chanel Deactivated commented
You are correct in that ACTION_ORDER_CREATED will only look for orders that are created on the device.

For your solution, you will need to:
1) From your web app, send a notification containing the order ID (which confirms that it's in Clover's system)
2) Make sure your app is listening for the notifications that you are sending (confirm that the correct order ID is being received by the device)
3) When your app gets the notification, it triggers a separate Service in your app to:
a) Find the order by the ID that you've passed (to ensure that the order does indeed exist)
b) Initiate the printOrder job
Below is a quick and dirty example of how to do this (Note: This implementation is not complete and also requires additional error checking and memory management.)

public class NotificationReceiver extends AppNotificationReceiver {
    public final static String TEST_NOTIFICATION_ACTION = "printOrder";

    public NotificationReceiver() {
    }

    @Override
    public void onReceive(final Context context, AppNotification notification) {
        //App Notifications (from Web API call)
        Toast.makeText(context, "Notification Received", Toast.LENGTH_SHORT).show();

        if (notification.appEvent.equals(TEST_NOTIFICATION_ACTION)) {
            Log.i("MyApp", "Intent received NOTIFICATION " + notification.payload);

            Intent intent = new Intent(context, printOrderService.class);
            intent.putExtra("orderId", notification.payload);
            context.startService(intent);
        }
    }
}

@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
    if (account == null) {
        account = CloverAccount.getAccount(this);

        if (account == null) {
            Toast.makeText(this, "there is no account", Toast.LENGTH_SHORT).show();
        }
    }

    return super.onStartCommand(intent, flags, startId);
}

private void connect(){
    disconnect();
    if (account != null) {
        Log.i("connect()", "Account found: connecting services");
        orderConnector = new OrderConnector(this, account, null);
        orderConnector.connect();
    }
    Log.i("connect()", "Done connecting!");
}

private void disconnect(){
    Log.i("disconnect()", "Disconnecting services!");
    if (orderConnector != null) {
        orderConnector.disconnect();
        orderConnector = null;
    }
    Log.i("disconnect()", "Done disconnecting!");
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.i("printOrderService", "onHandleIntent: " + intent.getStringExtra("orderId"));
    Toast.makeText(getApplicationContext(), "New Order Received!", Toast.LENGTH_LONG).show();
    new SpecificOrderAsyncTask().execute(intent.getStringExtra("orderId"));


}

public class SpecificOrderAsyncTask extends AsyncTask<String, Void, Order> {
    @Override
    protected final Order doInBackground(String... currentOrderID) {
        try {
            connect();
            newOrder = orderConnector.getOrder(currentOrderID[0]);
            Log.i("SpecificOrderAsyncTask", "Order found: returning");
            return newOrder;
        } catch (RemoteException | ClientException | ServiceException | BindingException e) {
            e.printStackTrace();
        } finally{
            Log.i("printOrderService", "onHandleIntent: Attempted to retrieve order");
            if (newOrder !=null){
                Log.i("printOrderService", "onHandleIntent: Order found");
                new StaticOrderPrintJob.Builder().order(newOrder).build().print(getApplicationContext(), account);
            } else{
                Log.i("printOrderService", "onHandleIntent: Order not found, print not triggered.");
            }
            disconnect();
        }
        return null;
    }
}
4 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

labventures avatar image labventures commented ·

Thank you for the answer.

This is exactly how I implemented the notification receiver on Android.
I followed your push notification example:
https://github.com/clover/android-examples/tree/master/pushnotificationexample

Unfortunately the 'onReceive' method never called.

This is the push call:

https://apisandbox.dev.clover.com/v3/apps/M1PQGMQYFXCG6/merchants/P7EWCVM316RDY/notifications
Headers:
Authorization: Bearer {APP_SECRET}
Content-Type: application/json
Body: { "event": "test_notification", "data": "Q1P5293EATX8G" }

* Tested both on Clover Station 2018 emulator and DevKit device

* Sandbox environment

0 Likes 0 ·
chanel avatar image chanel labventures commented ·

I've looked up your merchant and you have an emulator and a Station 1 set up. It looks like your Station 1 is not connecting to the internet. Please ensure that your device has a network connection and try again.

0 Likes 0 ·
labventures avatar image labventures chanel commented ·

Both emulator and station device are connected to the internet. The app was downloaded from the Clover App Market so it has to be connected

0 Likes 0 ·
chanel avatar image chanel labventures commented ·

The particular order you're trying to print has been deleted. Could that be your issue?

If that isn't the case, I can't help you further until you provide a code snippet of what your app is doing.

0 Likes 0 ·

Welcome to the
Clover Developer Community