I recently started learning about both Qt and Android development so forgive me if I say some nonsense.
Issue
Not being able to display a notification permission request dialog
Context
Today, I was using the project example Qt Android Notifier and I saw that no notifications were being displayed in certain devices. I discovered that since Android API level >= 33 (Tiramisu), you are required to prompt the user for notification permissions. I tried that implementing that and I found out that there are 2 main approaches when dealing with Android permissions in Qt.
Through QtAndroidPrivate
Through JNI
main.cpp
void requestNotificationPermission() {
// QNativeInterface::QAndroidApplication::sdkVersion()
if (QtAndroidPrivate::androidSdkVersion() < __ANDROID_API_T__) {
return;
}
QFuture<QtAndroidPrivate::PermissionResult> future = QtAndroidPrivate::requestPermission("android.permission.POST_NOTIFICATIONS");
future.then([](const QtAndroidPrivate::PermissionResult &result) {
if (result == QtAndroidPrivate::Authorized) {
qDebug() << "Permission granted!";
} else {
qDebug() << "Permission denied!";
}
});
// I was obviously toggling between each mode so that no permission requests collisions were made.
// QJniObject::callStaticMethod<void>(
// "org/qtproject/example/androidnotifier/NotificationClient",
// "requestNotificationPermission",
// "(Landroid/content/Context;)V",
// QNativeInterface::QAndroidApplication::context()
// );
}
NotificationClient.java
public static void requestNotificationPermission(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
return;
}
if (context.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
return;
}
if (!(context instanceof Activity)) {
return;
}
Activity activity = (Activity) context;
activity.requestPermissions(
new String[]{
Manifest.permission.POST_NOTIFICATIONS
},
NotificationClient.PERMISSION_REQUEST_CODE
);
}
Neither of the 2 methods shown above worked:
QtAndroidPrivate crashed the application with a null reference error when trying to deal and resolving the QFuture promise
F/libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x28 in tid 5071 (androidnotifier), pid 5071 (androidnotifier)
F/DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
F/DEBUG : Build fingerprint: 'Android/sdk_phone64_x86_64/emulator64_x86_64:13/TE1A.220922.034/10940250:userdebug/test-keys'
F/DEBUG : Revision: '0'
F/DEBUG : ABI: 'x86_64'
F/DEBUG : Timestamp: 2025-08-20 23:47:54.569231408+0200
F/DEBUG : Process uptime: 1s
F/DEBUG : Cmdline: org.qtproject.example.androidnotifier
F/DEBUG : pid: 5071, tid: 5071, name: androidnotifier >>> org.qtproject.example.androidnotifier <<<
F/DEBUG : uid: 10127
F/DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0000000000000028
F/DEBUG : Cause: null pointer dereference
F/DEBUG : rax 0000000000000028 rbx 0000706fc20dc758 rcx 0000000000000000 rdx 0000000000000000
F/DEBUG : r8 0000000000000002 r9 0000000000000000 r10 000000006fcb80e0 r11 0000706ea52d3450
F/DEBUG : r12 0000000000000002 r13 0000000000000000 r14 0000706f220e57b0 r15 00007ffffb3e7540
F/DEBUG : rdi 0000000000000028 rsi 0000000000000000
F/DEBUG : rbp 00007ffffb3e7140 rsp 00007ffffb3e7140 rip 0000706ef8dea18c
F/DEBUG : backtrace:
F/DEBUG : #00 pc 000000000002718c /data/app/~~thsWNG65e5GbVX_FLODyKQ==/org.qtproject.example.androidnotifier-DV2qLTB0YskNF1ggOrjb3Q==/lib/x86_64/libandroidnotifier_x86_64.so (QtPrivate::ResultItem::isVector() const+12) (BuildId: dd90d4c066b32994da3cba3a347167fb7779cd88)
F/DEBUG : #01 pc 000000000002bdf0 /data/app/~~thsWNG65e5GbVX_FLODyKQ==/org.qtproject.example.androidnotifier-DV2qLTB0YskNF1ggOrjb3Q==/lib/x86_64/libandroidnotifier_x86_64.so (QtAndroidPrivate::PermissionResult const* QtPrivate::ResultIteratorBase::pointer<QtAndroidPrivate::PermissionResult>() const+32) (BuildId: dd90d4c066b32994da3cba3a347167fb7779cd88)
JNI did not crash the application but it never displayed the notification popup and instead I always got this warning in the console.
W/libandroidnotifier_arm64-v8a.so: Found no valid pending permission request for request code 1001
For reference, the 1001 request code is what the variable NotificationClient.PERMISSION_REQUEST_CODE holds.
Cause
What was actually causing the problem was targetSdkVersion. I saw that my application was using a targetSdkVersion of 31 with the following log that I added into the NotificationClient.java class.
Log.d("MyTag", "targetSdkVersion: " + activity.getApplicationInfo().targetSdkVersion);
I was not really concerned about that at first because from what I read,