All threads / How to add Notifications to Rails with Noticed Discussion

Ask A Question

Notifications

You鈥檙e not receiving notifications from this thread.

How to add Notifications to Rails with Noticed Discussion

Awesome gem, I had been thinking on how to integrate all types of notification from rails app, from web notification, emails and Push Notifications. I guess Push Notification is just an API call (firebase, pusher or onesignal) away with custom delivery methods.

As always a great one, thanks Chris.

Yep! Push notifications would be an API call (similar to Slack, Twilio, and Vonage are). 馃憤

Reply

Not sure anyone else finds this really difficult to understand. The original videos on adding notifications were very 'step by step' with clearer explanations. This seems like it's documentation for an expert coder. I fear anyone who is intermediate or lower is not going to be able to follow this. I certainly won't be able to implement this myself...

Well, notifications are a complicated subject. Basic ones like sending an email are easy, but this is a full-featured notification system like you'd see on Facebook or Twitter. I'll talk more about it and show a very basic use case of Noticed soon.

Ahh, I see. I鈥檓 sure the advanced users will understand better.

What would be great is:

  • An example of a basic use case as you suggested
  • A tutorial how to switch from the earlier notification system to this new system

It looks quite amazing鈥攊t鈥檚 just overwhelming for someone at my level.

Thanks for the reply Chris!

Reply

Hi Chris,

Thanks for this gem and tutorial!

With that said, I'm struggling to understand how to pass it to my mailer method.

I'm getting this error that says: ArgumentError: wrong number of arguments (given 0, expected 2..3) for my mailer.

In my mailer, I have a method, new_post_notification, named after my notification class NewPostNotification.

The method is defined as:

def new_post_notification
@notification = notification

mail(to: @notification.recipient.email, subject: "My subject here")

end

And in my posts_controller I have this:

def create
@post = current_user.posts.build(post_params)

respond_to do |format|
  if @post.save
    NewPostNotification.with(post: @post).deliver_later(Audience.all) # Here's where I inserted the notification code
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render :show, status: :created, location: @post }
  else
    format.html { render :new }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
end

end

Per the error, my thinking is that I need to add parameters to my new_post_notification mailer method:

def new_post_notification(notification, recipient, options={})
@notification = notification

mail(to: @notification.recipient.email, subject: "My subject here")

end

But, that didn't work either and had the same error message. I realize I am definitely not doing this right, and I am pretty new to Ruby and Rails, so I definitely think I'm going 'round in circles missing the obvious.

Thank you.

Reply

Chris helped clarify what I was doing wrong, thank you Chris!

I ended up with this in my mailer:

def new_post_notification
    @post = params[:post]
    @recipient = params[:recipient]

    mail(to: @recipient.email, subject: "My subject")
  end
Reply

This is awesome work 馃憦

Reply

I wonder if this could be tied into creating a tweet or Facebook post on a Facebook page.

Of course, just add your own delivery method for it.

Reply

oh, cool. Is there an open source place where delivery methods are shared, or a list?

Reply

This looks like it could turn into the same situation as OmniAuth had. How many delivery methods and how much demand would warrant going to a strategy framework?

Reply

Hello. Faced a problem while testing notifications. If there is a row in the notifications table that refers to a non-existent record, then calling notifications.to_notifications results in an error. It is clear that you need to delete associated notifications on before_destroy. But if the record is deleted directly through the psql shell for example?

Check the readme, we have instructions for that. 馃憤

Thanks for the answer and special for a very cool gem tutorial. I read the readme and added to the model deleting notifications when deleting a parent record. All right. Do you mean section https://github.com/excid3/noticed/blob/master/README.md#associating-notifications?

Reply

Hey Chris, I'm having a play around with this, as it's pretty amazing what it's doing. I have a question about implementing the ActionCable delivery. I can't get the notification channel to deliver to the recipient. I've tried with the default setting, and then I've tried setting the channel. When I check the logs I can see "[ActionCable] Broadcasting to :: {params}". I can't work out where the is coming from. Shouldn't it be the ID of the recipent? When I broadcast through the channel manually from the rails console using "ActionCable.server.broadcast ":, {params}", I get a notification as expected. Not sure if I've missed something, or having something configured wrongly?

Wondering if you ever figured this out as I'm having a similar issue. Set it up per the documentation in the gem, and correctly getting a message saying I've subscribed to the channel, but whenever I "deliver_by" action cable I'm not getting any data on the websocket (checked the logs and it's not even showing up there).

Reply

Hey Chris, thanks so much for this one.
Just wondering if you have some explanation on how to switch:

  • from the current system (the user needs to click on the bell to go to /notifications and see the notifications)
  • to a fully real time system (the current page dynamically notices the new notif coming in and adding a red icon on the bell, and when clicking on the bell the notifications show as kind of a menu, not opening a complete new page)

Thanks a lot
Vinc

That was the first thing I did when I added Noticed to JumpstartRails.com :)

Conceptually all you need to do is:

  1. Add a dropdown for notifications
  2. Setup notifications to use Database + Websocket delivery methods
  3. Have Javascript listen to the websocket and insert the notification when a new one is received
  4. Add the red dot to the HTML
  5. Remove the red dot when opening the dropdown
Reply

Is there a way to check to see if the notification already exists before adding another as I'm getting a lot of duplicates.

You can query the database if you are using the database delivery method.

Reply

I'm having trouble following the tutorial. Can you share the source for the example you were showing?

Reply

Great gem! Should it work with ActiveJob (using sidekiq) out of the box like ActionMailer's deliver_later method? Or do I need to configure someting in order for Sidekiq to queue the deliveries?

Sidekiq needs to be configured to process the mailers queue.

Thx! It is though ( mailers queue is configured and working for action_mailer). Any other suggestions?

Reply

Hey Chris - do you recommend a way to test the Notification class in RSpec? eg. test Twilio so you don't have to keep resending the SMS over and over.

Reply

Hey Chris, awesome gem. I've been looking for a good notification gem for so long. Might sound dumb but how does this work with Hotwire, like can we use ActionCable Delivery method and stream to channel or create a custom delivery_method for TurboStream?

Reply

It throws me this error uninitialized constant CommentNotification

Can you post a screenshot? Also come post on the GR Slack

Reply

Hi everyone! Any directions on how to group notifications to avoid the flood in the views when a lot of notifications?

Thanks!

Reply

Hi thanks for this useful gem ! But after generating a new notification and trying to use it in a controller, i am getting an error :
路 "NameError (uninitialized constant ProjectInvitationNotification)"

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 53,406+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.