novoda / download-manager

A library that handles long-running downloads, handling the network interactions and retrying downloads automatically after failures
Apache License 2.0
483 stars 63 forks source link

Customizing Notifications #439

Closed Hackmodford closed 6 years ago

Hackmodford commented 6 years ago

I'm having trouble customizing the notifications. Looking at the source code, I thought I would have to create my own NotificationCreator that implements the NotificationCreator interface. However, the interface is package private so I am unable to do so.

My goal is to have the library only provide notifications for download progress. I have my own backend that handles download completion and failure.

Is there a way I can do this with the library?

--- Update --- Okay, it looks like this might be what I need to do.

mDownloadManager = DownloadManagerBuilder
                .newInstance(context, handler, R.mipmap.ic_launcher)
                .withAllowedConnectionType(getPreferredConnectionType(context))
                .withNotification(new NotificationCustomizer<DownloadBatchStatus>()
                {
                    @Override
                    public NotificationDisplayState notificationDisplayState(DownloadBatchStatus payload)
                    {
                        switch (payload.status())
                        {
                            case DOWNLOADING:
                                return NotificationDisplayState.SINGLE_PERSISTENT_NOTIFICATION;
                            case PAUSED:
                                return NotificationDisplayState.SINGLE_PERSISTENT_NOTIFICATION;
                            case WAITING_FOR_NETWORK:
                                return NotificationDisplayState.SINGLE_PERSISTENT_NOTIFICATION;
                            default:
                                return NotificationDisplayState.HIDDEN_NOTIFICATION;
                        }
                    }

                    @Override
                    public Notification customNotificationFrom(NotificationCompat.Builder builder, DownloadBatchStatus payload)
                    {
                        return null;
                    }
                })
                .build();

However, I'm having a new problem. Since I have switched to AndroidX I cannot use the NotificationCompat library that the interface requires. I have to use this:

import androidx.core.app.NotificationCompat;

What can I do here?

Mecharyry commented 6 years ago

Hi @Hackmodford sorry to hear you are having issues with customising notifications. You are indeed correct that you should use the .withNotification() method.

As for AndroidX unfortunately our hands our tied at the moment because the migration to AndroidX relies on com.android.tools.build:gradle:3.2.0 which requires a wrapper update but our release plugin is unfortunately not up to date with this. I believe someone on the release plugin team is working on this. I think we'll be hesitant to release too hastily for AndroidX because we'll need to update our interfaces which would represent quite a big breaking change.

@zegnus what are your thoughts on this?

Hackmodford commented 6 years ago

Okay, I moved away from AndroidX. But I'm still unclear how to handle the notifications. Can I return null for CustomNotification?

Mecharyry commented 6 years ago

Hi @Hackmodford you cannot return null. As a general rule the only places you can safely return null have been highlighted with the @Nullable annotation. For the customNotificationFrom method you need to create a Notification using the NotificationCompat.Builder which is supplied as a param on the method.

Here is the version that is used by default in the library, perhaps this will help you https://github.com/novoda/download-manager/blob/release/library/src/main/java/com/novoda/downloadmanager/DownloadManagerBuilder.java#L363-L412

Hackmodford commented 6 years ago

That's what I needed. I did this and it's working perfectly now :)

.withNotification(new NotificationCustomizer<DownloadBatchStatus>()
                {
                    @Override
                    public NotificationDisplayState notificationDisplayState(DownloadBatchStatus payload)
                    {
                        DownloadBatchStatus.Status status = payload.status();
                        if (status == DOWNLOADED || status == DELETED || status == DELETING || status == ERROR || status == PAUSED) {
                            return NotificationDisplayState.HIDDEN_NOTIFICATION;
                        } else {
                            return NotificationDisplayState.SINGLE_PERSISTENT_NOTIFICATION;
                        }
                    }

                    @Override
                    public Notification customNotificationFrom(android.support.v4.app.NotificationCompat.Builder builder, DownloadBatchStatus payload)
                    {
                        DownloadBatchTitle downloadBatchTitle = payload.getDownloadBatchTitle();
                        String             title              = downloadBatchTitle.asString();
                        builder.setSmallIcon(R.drawable.ic_stat_onesignal_default)
                                .setContentTitle(title);

                        switch (payload.status()) {
                            case DOWNLOADING:
                                return createProgressNotification(builder, payload);
                            default:
                                return createHiddenNotification(builder);
                        }
                    }
                })