Ask A Question

Notifications

You’re 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.

Reply

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...

Reply

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.

Reply

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!

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.

Reply

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

In the repo

Reply
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?

Reply

Check the readme, we have instructions for that. 👍

Reply

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?

Reply

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

Lol, now I'm here wondering if either of you two have figured this out. I don't understand how the delivery method for action cable works with noticed.

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

Reply

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.

Reply

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?

Reply

Sidekiq needs to be configured to process the mailers queue.

Reply

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

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.

Reply

It throws me this error uninitialized constant CommentNotification

Reply

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

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

Reply
Join the discussion
Create an account Log in

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

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

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