Skip to content

7. SDK: Notification Providers

Kieron Quinn edited this page Oct 23, 2023 · 1 revision

Notification Providers

A Notification Provider works with a Smartspacer Target or Complication to allow the loading of data from another app's notification. With it, you can effectively "map" content from a notification to a Smartspace Target or Complication, providing a simple way to load data from another app such as a status update.

Smartspacer handles listening for notifications for you, and provides your plugin with the raw StatusBarNotification from the notification when it appears or changes, or an empty list if the notification has been dismissed. It's then up to your plugin to parse data out of the notification, store it, and update a Target or Complication.

Note: Users will be asked if they wish to allow your plugin to access notifications when your Target/Complication is added. If they deny this access, the Target/Complication will not be added.

Setup

Follow the pages for creating a Target or Complication first.

The Notification Provider Class

Notification Providers at their core are Content Providers, but the Smartspacer SDK handles most of the logic required for a Provider to function.

The basic Notification Provider class is as follows:

class ExampleNotificationProvider: SmartspacerNotificationProvider() {

    override fun onNotificationsChanged(
        smartspacerId: String,
        isListenerEnabled: Boolean,
        notifications: List<StatusBarNotification>
    ) {
        //Handle changes to the list of notifications
    }

    override fun getConfig(smartspacerId: String): Config {
        //Return your configuration
    }

}

Declaring in the Manifest

Since Notification Providers are based on ContentProviders, it must be specified as one, with a specific permission. An action is not required for this provider:

<provider
    android:name=".ExampleNotificationProvider"
    android:authorities="${applicationId}.notification.example"
    android:permission="com.kieronquinn.app.smartspacer.permission.ACCESS_SMARTSPACER_NOTIFICATIONS"
    android:exported="true"/>

Note: The authority specified here is the authority you must specify in your Target or Complication's configuration, as the notificationProvider.

Configuration

The getConfig method must return some basic information for Smartspacer about your Notification Provider. The available options are:

Config(
	packages = // A set of package names to receive notifications from
)

Handling Notification Changes

When a notification is shown, dismissed or changed for the packages you are listening for, onNotificationsChanged will be called.

override fun onNotificationsChanged(
    smartspacerId: String,
    isListenerEnabled: Boolean,
    notifications: List<StatusBarNotification>
) {
    if(!isListenerEnabled) {
        //Smartspacer's notification listener is disabled, either ignore or prompt the user to enable it
        return
    }
    notifications.forEach {
        val title = it.notification.getContentTitle()
        val content = it.notification.getContentText()
        val icon = it.notification.smallIcon
        //Do something with the data
    }
}

Note: isListenerEnabled is provided to differentiate between no notifications being shown and Smartspacer not being able to provide them. If it is critical for your plugin to show Targets/Complications based on notifications, you can use this to prompt the user to enable the notification listener. Otherwise, you can treat it as if no notifications are showing.

Smartspacer's SDK contains two helper methods to get the title (getContentTitle) and text (getContentText) from a Notification. Other data can be accessed using the various fields of StatusBarNotification and Notification. You can even load RemoteViews if you wish.

Dismissing a Notification

SmartspacerNotificationProvider has a static method dismissNotification, which takes a Context and StatusBarNotification and dismisses the notification. You can call this when your Target is dismissed, if you wish.

Converting a PendingIntent to an Intent

Notifications use PendingIntents, which sometimes means useful information is obscured from plugins which could be useful, for example the contents of a deep link tapping a notification would launch. SmartspacerNotificationProvider has a static method resolveContentIntent, which takes a Context and StatusBarNotification, as well as a callback to be invoked when the Intent is loaded from the Notification's content PendingIntent. Please note that this will only work if the user is using Smartspacer's Enhanced Mode, otherwise it will always return null.