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
Join the discussion

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

Join 37,629+ 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

    © 2020 GoRails, LLC. All rights reserved.