C

AndroidNotificationListener QML Type

Allows listening for posted notifications on Android devices. More...

Import Statement: import QtAndroidAutomotive.Base
Since: Qt 6.4

Properties

Signals

Methods

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 [since 6.4]

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

RoleType
validbool
notificationIdint
keystring
packageNamestring
clearablebool
titlestring
contentstring
iconSmallstring
iconLargestring
postTimeDate
visibilityenumeration
actionsArray

Each action has the following properties

PropertyType
titlestring
iconstring

Use the model like so:

    ListView {
        id: listView

        anchors.fill: parent

        model: AndroidNotificationListener.notificationsModel

        delegate: NotificationDelegate {
            width: listView.width
        }

    }

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.


void snoozeNotification(string key, int duration)

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.