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). 👍
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’m 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—it’s just overwhelming for someone at my level.
Thanks for the reply Chris!
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.
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
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?
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?
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?
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).
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:
- Add a dropdown for notifications
- Setup notifications to use Database + Websocket delivery methods
- Have Javascript listen to the websocket and insert the notification when a new one is received
- Add the red dot to the HTML
- Remove the red dot when opening the dropdown
Is there a way to check to see if the notification already exists before adding another as I'm getting a lot of duplicates.
I'm having trouble following the tutorial. Can you share the source for the example you were showing?
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?
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.
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?
Hi, I am interested in doing this also. Di you have any success? I tried deliver_by :action_cable, stream: Turbo::StreamsChannel.signed_stream_name([Current.account.id, Current.user.id])
in the notification and <%= turbo_stream_from [Current.account.id, Current.user.id], channel: Noticed::NotificationChannel %>
in the view without success.
Hi everyone! Any directions on how to group notifications to avoid the flood in the views when a lot of notifications?
Thanks!
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)"
Can we use a PORO class to deliver ?
This is what I did
app/models/new_email.rb
class NewEmail
attr_reader :params
def initialize(params)
@params = params.permit(:to, :subject, :body)
end
end
app/controllers/emails_controller.rb
NewEmailNotification.with(email: NewEmail.new(params)).deliver_later(current_user)
I received an error: Unsupported argument type: NewEmail