HeligPfleigh / react-native-thermal-receipt-printer

A RN library for thermal printer
157 stars 102 forks source link

USBPrinter error with PendingIntent on Android 12 (SDK 31) #127

Open karaushu opened 1 year ago

karaushu commented 1 year ago

Current Behavior

When running USBPrinter.init() got next error: Targeting S+ (version 10000 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

minSdkVersion - 21 compileSdkVersion - 31 targetSdkVersion - 31 buildToolsVersion - 31

Have been trying to update deps in gradle (androidx.work:work-runtime-ktx: v2.6.0, 2.7.0, 2.7.1)

dependencies {
    def work_version = "2.6.0"
    // Force WorkManager 2.6.0 for transitive dependency
    implementation("androidx.work:work-runtime-ktx:$work_version") {
        force = true
    }
}

Your Environment

software version
iOS or Android Android 12
react-native 0.69.5
node 16.17.0
npm or yarn npm 8.15.0
lcang121 commented 1 year ago

have you found the solution here?

olfatgh commented 1 year ago

in the file react-native-thermal-receipt-printer/android/src/main/java/com/pinmi/react/printer/adapter/USBPrinterAdapter.java

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

lcang121 commented 1 year ago

in the file react-native-thermal-receipt-printer/android/src/main/java/com/pinmi/react/printer/adapter/USBPrinterAdapter.java

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

thanks for the response. The error is gone after I did this. however, the app crashes after the usb permission is accepted. any idea why?

lcang121 commented 1 year ago

nvm. I used FLAG_MUTABLE instead of FLAG_IMMUTABLE and it worked.