thiendangit / react-native-thermal-receipt-printer-image-qr

React native thermal receipt printer
https://www.npmjs.com/package/react-native-thermal-receipt-printer-image-qr
112 stars 73 forks source link

Targeting S+ (version 10000 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. #70

Open affan338 opened 2 years ago

affan338 commented 2 years ago

For Android above 11 init for USB printers is not working.

USBPrinter.init().then(() => { USBPrinter.getDeviceList().then(setPrinters, error => ToastAndroid.show(error, ToastAndroid.LONG)); });

palani-kamaraj commented 1 year ago

We can fix it manually by adding the flag for PendingIntent in USBPrinterAdapter.java. Since android 31 and above required flag for Pending Intent.

this.mPermissionIndent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);

with

this.mPermissionIndent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

vagnerlandio commented 1 year ago

Hi! 👋

Firstly, thanks @palani-kamaraj ! 🙂

Today I used patch-package to patch react-native-thermal-receipt-printer-image-qr@0.1.10 for the project I'm working on.

Here is the diff that solved my problem based on #91 :

diff --git a/node_modules/react-native-thermal-receipt-printer-image-qr/android/src/main/java/com/pinmi/react/printer/adapter/USBPrinterAdapter.java b/node_modules/react-native-thermal-receipt-printer-image-qr/android/src/main/java/com/pinmi/react/printer/adapter/USBPrinterAdapter.java
index c4fc63a..f5089b2 100644
--- a/node_modules/react-native-thermal-receipt-printer-image-qr/android/src/main/java/com/pinmi/react/printer/adapter/USBPrinterAdapter.java
+++ b/node_modules/react-native-thermal-receipt-printer-image-qr/android/src/main/java/com/pinmi/react/printer/adapter/USBPrinterAdapter.java
@@ -107,7 +107,7 @@ public class USBPrinterAdapter implements PrinterAdapter {
     public void init(ReactApplicationContext reactContext, Callback successCallback, Callback errorCallback) {
         this.mContext = reactContext;
         this.mUSBManager = (UsbManager) this.mContext.getSystemService(Context.USB_SERVICE);
-        this.mPermissionIndent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
+        this.mPermissionIndent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
         IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
         filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
         filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);

This issue body was partially generated by patch-package.

palani-kamaraj commented 1 year ago

Thanks @vagnerlandio for suggesting patch-package. Yes it will help us to maintain our node_modules changes.