summaryrefslogtreecommitdiffstats
path: root/examples/corelib/platform/androidnotifier
diff options
context:
space:
mode:
Diffstat (limited to 'examples/corelib/platform/androidnotifier')
-rw-r--r--examples/corelib/platform/androidnotifier/CMakeLists.txt27
-rw-r--r--examples/corelib/platform/androidnotifier/android/AndroidManifest.xml18
-rw-r--r--examples/corelib/platform/androidnotifier/androidnotifier.pro4
-rw-r--r--examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc4
-rw-r--r--examples/corelib/platform/androidnotifier/main.cpp4
-rw-r--r--examples/corelib/platform/androidnotifier/notificationclient.cpp12
6 files changed, 43 insertions, 26 deletions
diff --git a/examples/corelib/platform/androidnotifier/CMakeLists.txt b/examples/corelib/platform/androidnotifier/CMakeLists.txt
index 0caa208a95..b356b15a43 100644
--- a/examples/corelib/platform/androidnotifier/CMakeLists.txt
+++ b/examples/corelib/platform/androidnotifier/CMakeLists.txt
@@ -1,3 +1,6 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
cmake_minimum_required(VERSION 3.16)
project(androidnotifier LANGUAGES CXX)
@@ -5,24 +8,21 @@ if(NOT ANDROID)
message(FATAL_ERROR "Example only works on Android")
endif()
-set(CMAKE_AUTOMOC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
find_package(Qt6 REQUIRED COMPONENTS Widgets)
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/platform/androidnotifier")
+qt_standard_project_setup()
qt_add_executable(androidnotifier
MANUAL_FINALIZATION
main.cpp
notificationclient.cpp
notificationclient.h
+ android/src/org/qtproject/example/androidnotifier/NotificationClient.java
+ android/AndroidManifest.xml
)
target_link_libraries(androidnotifier PRIVATE
+ Qt6::CorePrivate
Qt6::Widgets
)
@@ -44,7 +44,14 @@ qt_add_resources(androidnotifier "main"
)
install(TARGETS androidnotifier
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
+ BUNDLE DESTINATION .
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+)
+
+qt_generate_deploy_app_script(
+ TARGET androidnotifier
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml b/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml
index 4850a8e8e6..778028a566 100644
--- a/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml
+++ b/examples/corelib/platform/androidnotifier/android/AndroidManifest.xml
@@ -4,12 +4,9 @@
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.0">
- <!-- The comment below will be replaced with dependencies permissions upon deployment.
- Remove the comment if you do not require these default permissions. -->
- <!-- %%INSERT_PERMISSIONS -->
- <!-- The comment below will be replaced with dependencies permissions upon deployment.
- Remove the comment if you do not require these default features. -->
+ <!-- %%INSERT_PERMISSIONS -->
+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<!-- %%INSERT_FEATURES -->
<supports-screens
@@ -23,13 +20,16 @@
android:hardwareAccelerated="true"
android:label="Qt Notifier"
android:requestLegacyExternalStorage="true"
- android:icon="@drawable/icon">
+ android:icon="@drawable/icon"
+ android:allowBackup="true"
+ android:fullBackupOnly="false">
<activity
android:name="org.qtproject.qt.android.bindings.QtActivity"
android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density"
android:label="Qt Notifier"
android:launchMode="singleTop"
- android:screenOrientation="unspecified">
+ android:screenOrientation="unspecified"
+ android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
@@ -42,10 +42,6 @@
<meta-data
android:name="android.app.background_running"
android:value="false"/>
-
- <meta-data
- android:name="android.app.extract_android_style"
- android:value="none" />
</activity>
</application>
</manifest>
diff --git a/examples/corelib/platform/androidnotifier/androidnotifier.pro b/examples/corelib/platform/androidnotifier/androidnotifier.pro
index f1650c1911..7e5b845e10 100644
--- a/examples/corelib/platform/androidnotifier/androidnotifier.pro
+++ b/examples/corelib/platform/androidnotifier/androidnotifier.pro
@@ -1,5 +1,5 @@
-QT += core gui
-greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+QT += core gui widgets
+QT += core-private # For Notification permission request
SOURCES += \
main.cpp \
diff --git a/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc b/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc
index 8d2a9a47e4..1315ad7762 100644
--- a/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc
+++ b/examples/corelib/platform/androidnotifier/doc/src/androidnotifier-example.qdoc
@@ -4,7 +4,9 @@
/*!
\title Qt Android Notifier
\example platform/androidnotifier
+ \meta tag {widgets,android,notification}
\brief Demonstrates calling Java code from Qt in an Android application.
+ \ingroup androidplatform
\image androidnotifier.png
@@ -47,7 +49,7 @@
The call to the Java meethod use \l QJniObject which relies on the Java Native
Interface (JNI) APIs to communicate with Java. Also, in the previous snippet,
- we are passing the app's context object which the the static Java code can use
+ we are passing the app's context object, which the static Java code can use
to tap into the app's specific properties and APIs.
To make sure our smiley buttons do what they are supposed to, we add the
diff --git a/examples/corelib/platform/androidnotifier/main.cpp b/examples/corelib/platform/androidnotifier/main.cpp
index 33e77c7018..07eff5d2b0 100644
--- a/examples/corelib/platform/androidnotifier/main.cpp
+++ b/examples/corelib/platform/androidnotifier/main.cpp
@@ -41,11 +41,11 @@ int main(int argc, char *argv[])
widget.setLayout(&mainLayout);
//! [Connect button signals]
- QObject::connect(&happyButton, &QPushButton::clicked, []() {
+ QObject::connect(&happyButton, &QPushButton::clicked, &happyButton, []() {
NotificationClient().setNotification("The user is happy!");
});
- QObject::connect(&sadButton, &QPushButton::clicked, []() {
+ QObject::connect(&sadButton, &QPushButton::clicked, &happyButton, []() {
NotificationClient().setNotification("The user is sad!");
});
//! [Connect button signals]
diff --git a/examples/corelib/platform/androidnotifier/notificationclient.cpp b/examples/corelib/platform/androidnotifier/notificationclient.cpp
index af1cb7322a..aa6093c29c 100644
--- a/examples/corelib/platform/androidnotifier/notificationclient.cpp
+++ b/examples/corelib/platform/androidnotifier/notificationclient.cpp
@@ -5,10 +5,22 @@
#include <QtCore/qjniobject.h>
#include <QtCore/qcoreapplication.h>
+#include <QtCore/private/qandroidextras_p.h>
+
+using namespace Qt::StringLiterals;
NotificationClient::NotificationClient(QObject *parent)
: QObject(parent)
{
+ if (QNativeInterface::QAndroidApplication::sdkVersion() >= __ANDROID_API_T__) {
+ const auto notificationPermission = "android.permission.POST_NOTIFICATIONS"_L1;
+ auto requestResult = QtAndroidPrivate::requestPermission(notificationPermission);
+ if (requestResult.result() != QtAndroidPrivate::Authorized) {
+ qWarning() << "Failed to acquire permission to post notifications "
+ "(required for Android 13+)";
+ }
+ }
+
connect(this, &NotificationClient::notificationChanged,
this, &NotificationClient::updateAndroidNotification);
}