Monday, April 29, 2024
HomeRuby On RailsPush Notifications with ActionCable | Drifting Ruby

Push Notifications with ActionCable | Drifting Ruby


# Terminal
rails g channel notification
# app/javascript/packs/utility.js
Notification.requestPermission().then(perform (outcome) {})
# views/welcome/index.html.erb
<%= link_to 'Ship Notification', welcome_index_path, distant: true %>
# views/welcome/index.js.erb
if (Notification.permission === 'granted') {
  var title="Push Notification"
  var physique = 'Triggered by hyperlink on the <%= Rails.env %> setting.'
  var choices = { physique: physique }
  new Notification(title, choices)
}
# welcome_controller.rb
def index
  ActionCable.server.broadcast('notification_channel', 'You might have visited the welcome web page.')
finish
# app/channels/notification_channel.rb
def subscribed
  stream_from "notification_channel"
finish
# app/javascript/channels/notification_channel.js
import shopper from "./shopper"

shopper.subscriptions.create("NotificationChannel", {
  linked() {},
  disconnected() {},
  obtained(information) {
    if (Notification.permission === 'granted') {
      var title="Push Notification"
      var physique = information
      var choices = { physique: physique }
      new Notification(title, choices)
    }
  }
});
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments