Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom actions to notifications #179

Closed
gotev opened this issue Aug 25, 2016 · 12 comments
Closed

Add custom actions to notifications #179

gotev opened this issue Aug 25, 2016 · 12 comments
Assignees
Labels
feature request Issue with request of new features
Milestone

Comments

@gotev
Copy link
Owner

gotev commented Aug 25, 2016

It will be useful to be able to add custom actions to UploadNotificationConfig, like this:

new UploadNotificationConfig()
    .addAction(actionIconResourceID, buttonText, pendingIntent)

addAction should have the following signature:

public final UploadNotificationConfig addAction(
    int iconResourceID, String text, PendingIntent action)

This explains how to marshal and unmarshal PendingIntents to/from Parcelable

@gotev gotev added the feature request Issue with request of new features label Aug 25, 2016
@gotev
Copy link
Owner Author

gotev commented Aug 25, 2016

@hendrawd if you need help with the implementation, let me know here

@gotev gotev added this to the 3.2 milestone Aug 25, 2016
@hendrawd
Copy link
Contributor

hendrawd commented Aug 25, 2016

It's ok for marshal and un-marshal PendingIntent, but i think it is better if we can add more than 1 actions there, so i work on List instead of just 1 PendingIntent. I have tried with more than 1 action, and 3 is the best(visible). What do you think?

@gotev
Copy link
Owner Author

gotev commented Aug 25, 2016

@hendrawd Yes. When addAction is invoked, a new element has to be added to the underlying list. We can log a warning message if the upload notification config has more than three buttons

@gotev gotev modified the milestones: 3.3, 3.2 Mar 3, 2017
@mnaswin3
Copy link

mnaswin3 commented Mar 7, 2017

Hi guys,
any update on this feature?

@gotev
Copy link
Owner Author

gotev commented Mar 7, 2017

@mnaswin3 not yet, but you can send a PR!

gotev added a commit that referenced this issue Jul 29, 2017
…otificationConfig class was needed, to have a better structure, less method count and easier code readability. Updated demo app.
@gotev gotev self-assigned this Jul 29, 2017
@gotev
Copy link
Owner Author

gotev commented Jul 29, 2017

Implemented actions for notifications. Major refactor to UploadNotificationConfig class was needed, to have a better structure, less method count (#248) and easier code readability. It drops retro-compatibility, but for the better. Updated demo app. This feature will be available starting from release 3.3.

Read the code example below, which is taken from the demo app's BaseActivity, in which I have implemented an example of how you can cancel an upload from an action.

Before 3.3:

protected UploadNotificationConfig getNotificationConfig(@StringRes int title) {
    return new UploadNotificationConfig()
            .setIcon(R.drawable.ic_upload)
            .setCompletedIcon(R.drawable.ic_upload_success)
            .setErrorIcon(R.drawable.ic_upload_error)
            .setCancelledIcon(R.drawable.ic_cancelled)
            .setIconColor(Color.BLUE)
            .setCompletedIconColor(Color.GREEN)
            .setErrorIconColor(Color.RED)
            .setCancelledIconColor(Color.YELLOW)
            .setTitle(getString(title))
            .setInProgressMessage(getString(R.string.uploading))
            .setCompletedMessage(getString(R.string.upload_success))
            .setErrorMessage(getString(R.string.upload_error))
            .setCancelledMessage(getString(R.string.upload_cancelled))
            .setClickIntent(new Intent(this, MainActivity.class))
            .setClearOnAction(true)
            .setRingToneEnabled(true);
}

In 3.3:

protected UploadNotificationConfig getNotificationConfig(String uploadId, 
                                                         @StringRes int title){
    UploadNotificationConfig config = new UploadNotificationConfig();

    PendingIntent clickIntent = PendingIntent.getActivity(
            this, 1, new Intent(this, MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    config.setTitleForAllStatuses(getString(title))
            .setRingToneEnabled(true)
            .setClickIntentForAllStatuses(clickIntent)
            .setClearOnActionForAllStatuses(true);

    config.getProgress().message = getString(R.string.uploading);
    config.getProgress().iconResourceID = R.drawable.ic_upload;
    config.getProgress().iconColorResourceID = Color.BLUE;
    
    // this is how you add an action to a specific notification status
    config.getProgress().actions.add(new UploadNotificationAction(
            R.drawable.ic_cancelled,
            getString(R.string.cancel_upload),
            NotificationActions.getCancelUploadAction(this, 1, uploadId)));

    config.getCompleted().message = getString(R.string.upload_success);
    config.getCompleted().iconResourceID = R.drawable.ic_upload_success;
    config.getCompleted().iconColorResourceID = Color.GREEN;

    config.getError().message = getString(R.string.upload_error);
    config.getError().iconResourceID = R.drawable.ic_upload_error;
    config.getError().iconColorResourceID = Color.RED;

    config.getCancelled().message = getString(R.string.upload_cancelled);
    config.getCancelled().iconResourceID = R.drawable.ic_cancelled;
    config.getCancelled().iconColorResourceID = Color.YELLOW;

    return config;
}

It's more verbose now, but you can finely tune every aspect of every notification, in a way which was not possible before (#293), and it's easier to read. Check out the new utility functions in UploadNotificationConfig which lets you set properties globally for all the notification statuses.

@gotev
Copy link
Owner Author

gotev commented Jul 30, 2017

3.3 released and this feature is now available!

@vsg24
Copy link

vsg24 commented Nov 8, 2017

config.getCancelled().message = getString(R.string.upload_cancelled);

This coding style is horrible and not appropriate for Java or any other lang as far as I can tell.

@gotev
Copy link
Owner Author

gotev commented Nov 8, 2017

@vsg24 Fine. If you implemented that part, how would you do it instead? I have reasons for that kind of implementation, but I want to hear your feedback and suggestions

@hendrawd
Copy link
Contributor

hendrawd commented Nov 8, 2017

@vsg24 Do you want to use setter and getter for message?

@vsg24
Copy link

vsg24 commented Nov 8, 2017

@gotev @hendrawd Just taking a look at other libraries and even Android SDK itself, I think it's pretty clear how it should be done.

@gotev
Copy link
Owner Author

gotev commented Nov 8, 2017

@vsg24 Ok but no value added with your comments yet and it's a pity. I like to discuss about software design a lot! A part the obvious, what do you suggest? It's open source, everything can be changed for the better 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issue with request of new features
Projects
None yet
Development

No branches or pull requests

4 participants