question

Rohit Bhardwaj avatar image
Rohit Bhardwaj asked chanel Deactivated commented

How to send custom text to printer while the app receives notification ?

Hello everyone,

I'm trying to print custom text which is created with the help of order and line item details. I want this custom text to be sent to the printer while the app is not running and the printer should print this custom text. We send a notification to the application which has orderId in the form of string. The notification service runs every time and without a fail notification is received on the app.

What I have tried till now :

METHOD 1:-

TextPrintJob.Builder().text(message).build().print(context, mAccount);

I'm directly sending the message to the printer which should print the message but unfortunately it is not working.

METHOD 2:-

I'm using printerConnector and get the details of the connected printer and wrote asyncTask to print the job. I referred this code on the gitHub example.

new AsyncTask<Void, Void, Printer>() {
                
@Overrideprotected Printer doInBackground(Void... params) {
return getPrinter();
}

@Overrideprotected void onPostExecute(Printer printer) {
if (printer == null) {
return;
}
new ReceiptPrintJob.Builder().orderId(orderId).build().print(context, mAccount, printer);
}
}.execute();
Even this method doesn't work.
NOTE: ReceiptPrintJob.Builder() is deprecated.

Please advice.

OrdersPrintMerchant
1 comment
10 |2000

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

Rohit Bhardwaj avatar image Rohit Bhardwaj commented ·

Guys, can anyone help me on this ?

0 Likes 0 ·

1 Answer

chanel avatar image
chanel Deactivated answered chanel Deactivated commented
Once you have your NotificationReciever set up properly, you can use an AsyncTask, just as you were trying to do. I was able to get this code snippet working:

new AsyncTask<AppNotification, Void, Void>() {
    @Override
    protected Void doInBackground(AppNotification... params) {
        try {
            AppNotification notification = params[0];
            new TextPrintJob.Builder().text(notification.payload).build().print(context, account);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, notification);
2 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.

Rohit Bhardwaj avatar image Rohit Bhardwaj commented ·

Hi Chanel,
Thanks for your response but it's still not working for me. Can you provide me with your full source code which I can use in a sample application and test it on my clover devices.

0 Likes 0 ·
chanel avatar image chanel Rohit Bhardwaj commented ·

Sorry, but providing the full source code isn't really feasible at this time.

Can you describe "not working"? Are you able to send a notification successfully? Is your app able to receive the notification with the appropriate data (the Order ID you're passing)? Are you seeing an error? Using log statements, can you determine if the async task is being called?

0 Likes 0 ·

Welcome to the
Clover Developer Community