Class DownloadNotificationProvider

java.lang.Object
com.castlabs.sdk.downloader.DownloadNotificationProvider

public abstract class DownloadNotificationProvider extends Object
Class to provide the DownloaderPlugin with the required info related to notifications for the foreground Downloader service. Starting in Android O, background execution limits have been imposed. Standard Services may not be run in background anymore. This can be overcome using a Foreground Service instead. In order for a Service to become a Foreground Service, a persistent Notification must be shown for as long as such Service is a Foreground Service. For the sake of consistency, the DownloaderPlugin will always run its service in foreground mode, regardless of the Android API version. The only abstract method in this class is getNotification(DownloadServiceBinder, Context). This method must be overridden and return the Notification to show while a Download is ongoing. The most common use case for this notification is to show the download progress of the Downloads. There are also two additional methods that can be overridden for further customisation:
Since:
4.1.0
  • Constructor Details

    • DownloadNotificationProvider

      public DownloadNotificationProvider(int notificationId)
      Parameters:
      notificationId - Notification id to bind to the persistent notification. Must be greater than 0.
  • Method Details

    • getNotificationId

      public final int getNotificationId()
      Returns the notification id provided in the constructor.
      Returns:
      the notification id
    • getNotification

      @NonNull public abstract Notification getNotification(@NonNull DownloadServiceBinder downloadServiceBinder, @NonNull Context context)
      Build and provide a notification. This notification will be tied to the DownloadService whenever it is in foreground mode, aka. when there's at least one active Download.
      Parameters:
      downloadServiceBinder - binder instance to get Download-related info
      context - the application context.
      Returns:
      the Notification to show
    • onDownloadEvent

      public boolean onDownloadEvent(@NonNull DownloadServiceBinder downloadServiceBinder, @NonNull Intent intent)
      Called whenever a new Download event is fired. The event Intent is forwarded to this method. The Intent actions will be one from the MessageHandler.

      You must return true *only* if you want the notification to be updated/posted. To avoid posting a notification for some download events, just return false in those cases. For example, to avoid showing a notification when a Download is deleted:

       
       	public boolean onDownloadEvent(@NonNull DownloadServiceBinder downloadServiceBinder, @NonNull Intent intent) {
       		String action = intent.getAction();
       		switch (action) {
       			case MessageHandler.ACTION_DOWNLOAD_DELETED:
       				return false;
       			default:
       				return super.onDownloadEvent(downloadServiceBinder, intent);
       		}
       	}
       
       
      Parameters:
      downloadServiceBinder - binder instance to get Download-related info
      intent - Intent having the Download event info
      Returns:
      true if the notification should be updated. In that case, getNotification(DownloadServiceBinder, Context) will be called right after. Default implementation returns true.
    • shouldKeepNotification

      public boolean shouldKeepNotification(@NonNull DownloadServiceBinder downloadServiceBinder)
      Whether the persistent notification should be cleared or not.

      This is called whenever the DownloadService transitions out of foreground mode, and thus the notification is no longer enforced. If true is returned it's the clients responsibility to dismiss the notification, or at least build the notification calling Notification.Builder.setOngoing(boolean) with false so it can be discarded by the user.

      Parameters:
      downloadServiceBinder - binder instance to get Download-related info
      Returns:
      Whether the persistent notification should be cleared or not on service transition to background mode. Default implementation returns true.