Search code examples
ruby-on-railsnotificationsmailboxer

How do notifications work with Mailboxer gem?


I'm trying to figure out how to setup a notifications system using Mailboxer.

I've already used it to setup an in-site messaging system so that users can message each other. Now I'd like to use it to send notifications from the site, to notify users of changes in their reputation points or remind them of actions they must complete. Something like Facebook or Stackoverflow's dropdown notification menus.

As an example, it might contain these types of notices:

  • User gets some points for performing an action:
    • "You received 50 points for helping #{user.name} with #{request.title}.
  • A reminder that the user must perform an action:
    • "You must review #{user.name}'s help with #{request.title}!
    • Will link to a page to complete that action.
  • User receives a reply to a message they sent:
    • "You've received a message from #{sender.name}"
    • Will link to the message.

Here are some details:

  • I don't want all notifications to send an email. Most will only need to be seen in the notifications menu. Is there an option in Mailboxer to control what is emailed, or would I have to bypass Mailboxer's mailers?

  • I'll want to format each type of notification differently in the dropdown. Add a specific glyphicon to each for example. Could I use the notification type field for this (using it to set a conditional)? How does type work? Can I just set it to a string, such as "reputation", depending on the notification?

  • Objects can be passed to the notify method. I'm confused about the purpose of this. How can that object be used? What objects would I want to send?


Feel free to leave some general info on Mailboxer notifications rather than specifically answering everything.

I've had pretty bad luck finding documentation for the notifications features, so would appreciate it if someone with some Mailboxer knowledge could chime in on this. Thanks in advance.


Solution

  • This is an late answer but just in case someone came upon this question like me, this is what I ended up doing.

    There is an user identities method defined as https://github.com/mailboxer/mailboxer#user-identities

    If it returns an email, an email will be sent. If it returns nil, no email will be sent.

    There is a notify method in mailboxer, and you can use it to send notifications as,

    alice.notify("Hi","Bob has just viewed your profile!") 
    

    And in mailboxer_email, check if the object is a notification or not.If it is, do not send an email.

    def mailboxer_email(object)
    
        if object.class==Mailboxer::Notification
          return nil
        else
          email
        end
     end
    

    You can defined more complex logic here such as disable email for certain type of users, etc.