C
AndroidNotificationListener QML Type
Allows listening for posted notifications on Android devices. More...
Import Statement: | import QtAndroidAutomotive.Base |
Since: | Qt 6.4 |
Properties
- connected : bool
- notificationAccess : bool
- notificationsModel : QAbstractListModel
(since 6.4)
Signals
- notificationPosted(string key)
- notificationRemoved(string key)
Methods
- void dismissNotification(string key)
- void launchContentIntent(string key)
- void requestNotificationAccess()
- void snoozeNotification(string key, int duration)
- void start()
- void stop()
Detailed Description
AndroidNotificationListener allows listening for posted notifications on Android devices. It utilizes Android's NotificationListenerService to receive information about notifications that are posted or removed.
Note: It is an singleton, so you do not have to declare it in your qml code.
Notifications Listener known issues and limitations
Checking whether access to notifications has been granted requires Android API 27 or higher to work properly.
Property Documentation
connected : bool |
This property holds 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 property holds false
will not result in the expected behavior. For this reason it is recommended to check that this property holds true
before trying to access other functionalities.
See also notificationAccess.
notificationAccess : bool |
This property holds whether access rights to listen for notifications has been granted. Holds true
if access has been granted, and false
if not.
Note: Due to used underlying Android API, this property can only show notification access when the Android API level used is 27 (Android 8.1) or higher. If using a lower API level, the property always holds true
, meaning AndroidNotificationListener will not handle requesting access by itself. Instead, you need to handle the access request yourself, for example by calling requestNotificationAccess().
notificationsModel : QAbstractListModel |
This property holds the model for active notifications an Android device. The model is based on a QAbstractListModel and manages a list of these elements containing
Role | Type |
---|---|
valid | bool |
notificationId | int |
key | string |
packageName | string |
clearable | bool |
title | string |
content | string |
iconSmall | string |
iconLarge | string |
postTime | Date |
visibility | enumeration |
actions | Array |
Each action
has the following properties
Property | Type |
---|---|
title | string |
icon | string |
Use the model like so:
ListView { id: listView anchors.fill: parent model: AndroidNotificationListener.notificationsModel delegate: NotificationDelegate { width: listView.width notificationId: model.notificationId key: model.key packageName: model.packageName title: model.title content: model.content clearable: model.clearable uriEncodedIcon: model.iconSmall actions: model.actions } }
To use a notification's icon in an Image, use the following:
Image { id: notificationIcon source: model.iconSmall }
To use action properties:
RowLayout { id: actionsLayout Layout.leftMargin: iconItem.width + 12 Repeater { id: actionsModel model: root.actions delegate: Button { text: root.actions[index].title icon.source: root.actions[index].icon onClicked: root.actions[index].performAction() } } }
This property was introduced in Qt 6.4.
Signal Documentation
notificationPosted(string key) |
This signal is emitted when a new notification with the given key has been posted.
Note: The corresponding handler is onNotificationPosted
.
notificationRemoved(string key) |
This signal is emitted when the notification with the given key has been removed.
Note: The corresponding handler is onNotificationRemoved
.
Method Documentation
void dismissNotification(string key) |
Dismisses the notification with the given key, if one is currently active and is clearable.
void launchContentIntent(string key) |
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.
void requestNotificationAccess() |
Opens up the settings view for granting access to notifications.
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(string key) and notificationPosted(string key) will be emitted at these times, just like when a notification is removed or posted normally.
void 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 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 access has been granted.
See also stop().
void stop() |
Stops listening for notifications, unbinding the service.
See also start().
Available under certain Qt licenses.
Find out more.