C

QAndroidNotificationListener Class

A class that allows listening for posted notifications on Android devices. It utilizes Android's NotificationListenerService to receive information about notifications that are posted or removed. More...

Header: #include <QAndroidNotificationListener>
qmake: QT += androidnotificationslistener
Since: Qt 6.4
Inherits: QObject

Public Functions

virtual ~QAndroidNotificationListener() override
QList<QAndroidNotificationItem *> getActiveNotifications()
QAndroidNotificationItem *getNotification(const QString &key)
bool hasNotificationAccess()
bool isConnected()
void requestNotificationAccess()
void start()
void stop()

Public Slots

void dismissNotification(const QString &key) const
void launchContentIntent(const QString &key) const
void snoozeNotification(const QString &key, long duration) const

Signals

void connectedChanged(bool connected)
void notificationAccessChanged(bool isAccessGranted)
void notificationPosted(QAndroidNotificationItem *notification)
void notificationRemoved(const QString &key)

Static Public Members

QAndroidNotificationListener *instance()

Detailed Description

Member Function Documentation

[override virtual] QAndroidNotificationListener::~QAndroidNotificationListener()

Destroys the instance of QAndroidNotificationListener. The destructor is virtual.

[signal] void QAndroidNotificationListener::connectedChanged(bool connected)

This signal is emitted when the listener is ready to use and has connected to Android's notification service, or the listener has disconnected from Android's notification service. Once this signal is emitted with a false value of connected, only QAndroidNotificationListener::start() should be called until QAndroidNotificationListener::isConnected() is true. Trying to invoke other methods will not yield expected results.

See also isConnected().

[slot] void QAndroidNotificationListener::dismissNotification(const QString &key) const

Dismisses the notification with the given key, if one is currently active and is clearable.

QList<QAndroidNotificationItem *> QAndroidNotificationListener::getActiveNotifications()

Returns a list of active notifications. Ownership of the returned notifications is not transferred, and any held reference to a returned notification should be cleared when notificationRemoved(const QString &key) is emitted with the matching key.

See also notificationRemoved().

QAndroidNotificationItem *QAndroidNotificationListener::getNotification(const QString &key)

Returns the notification with the matching key, or nullptr if one does not exist. Ownership is not transferred, and any held reference to the returned notification should be cleared when notificationRemoved(const QString &key) is emitted with the matching key.

See also notificationRemoved().

bool QAndroidNotificationListener::hasNotificationAccess()

Checks whether access rights to listen for notifications has been granted. Returns true if the access has been granted, false if not.

Note: Due to used underlying Android API, this method can only check for notification access when the Android API level used is 27 (Android 8.1) or higher. If using a lower API level, the method always return true, meaning QAndroidNotificationListener will not handle requesting access by itself. Instead, you need to handle the access request by yourself, for example by calling requestNotificationListenerAccess().

[static] QAndroidNotificationListener *QAndroidNotificationListener::instance()

Returns a pointer to the singleton instance.

bool QAndroidNotificationListener::isConnected()

Returns whether the listener is connected to Android's notification service and is ready to use.

Note: Due to the underlying implementation of Android's notification listener service, the only functionality that is available before the listener has successfully connected is start(). Using other functions while this method returns false will not result in the expected behavior, for example getActiveNotifications() will always return an empty list. For this reason it is recommended to check that this method returns true before trying to access other functionalities, and if false is returned to wait for the connected() signal to be emitted.

See also connectedChanged().

[slot] void QAndroidNotificationListener::launchContentIntent(const QString &key) const

Sends the contentIntent of a notification with the matching key. Calling this function has no effect if the notification does not have a valid contentIntent.

See also QAndroidNotificationItem::performContentAction().

[signal] void QAndroidNotificationListener::notificationAccessChanged(bool isAccessGranted)

This signal is emitted when the access rights for listening on notifications, isAccessGranted , have changed.

See also hasNotificationAccess() and requestNotificationAccess().

[signal] void QAndroidNotificationListener::notificationPosted(QAndroidNotificationItem *notification)

This signal is emitted when a new notification has been posted.

[signal] void QAndroidNotificationListener::notificationRemoved(const QString &key)

This signal is emitted when the notification with the given key has been removed. The corresponding QAndroidNotificationItem will be deleted, and any held references to it should be cleared when this signal is emitted.

void QAndroidNotificationListener::requestNotificationAccess()

Opens up the settings view for granting access to notifications.

[slot] void QAndroidNotificationListener::snoozeNotification(const QString &key, long duration) const

Snoozes the notification with the matching key for the duration in milliseconds. This will lead to the snoozed notification being removed, and posted again after duration has passed. notificationRemoved(const QString& key) and notificationPosted(QAndroidNotificationItem* notification) will be emitted at these times, just like when a notification is removed or posted normally.

void QAndroidNotificationListener::start()

Requests that the notification listener service be started and bound. If notification access hasn't been granted, binding the service will not succeed. If this method is called and the access hasn't been granted, the app notifications access settings view is opened.

Typically, this only needs to be called if stop() has been called, or if access to notifications has not yet been granted. Android will start the service without the need to call this method, but only if the access has been granted.

See also stop() and isConnected().

void QAndroidNotificationListener::stop()

Stops listening for notifications, unbinding the service.

See also start().

Available under certain Qt licenses.
Find out more.