- vừa được xem lúc

Push Notification đơn giản với Rails ActionCable

0 0 16

Người đăng: yongsokheng

Theo Viblo Asia

Trong bài này, mình sẽ xây dựng chức năng push notification đơn giản sử dụng actioncable của Rails. Như đã biết actionCable được dùng để làm realtime cho ứng dụng của mình.

Bước thực hiện

Tạo một channel cho actioncable

✗ rails generate channel notifications
Running via Spring preloader in process 4124 create app/channels/notifications_channel.rb identical app/assets/javascripts/cable.js create app/assets/javascripts/channels/notifications.coffee

Nó đã tạo 2 file như trên. Bây giờ mình sẽ subscribe vào channel tên là: notifications_channel

# app/channels/notifications_channel.rb
class NotificationsChannel < ApplicationCable::Channel def subscribed stream_from "notifications_channel" end def unsubscribed end
end

Tạo Form

Đơn giản chỉ có một text_field và nút submit để có thể gửi nội dùng push message. View

<%= form_tag push_notification_path do %> <%= text_field_tag :message %> <%= submit_tag :submit %>
<% end %>

Controller

class NotificationsController < ApplicationController def push_notification ActionCable.server.broadcast("notifications_channel", {message: params[:message]}) redirect_to root_path end end

routes

Rails.application.routes.draw do post :push_notification, to: "notifications#push_notification"
end

Đến đây, khi submit form, nó sẽ broadcast tới notifications_channel với data object {message: params[:message]})

Làm thế nào để push notification

Ở đây mình sẽ sử dụng Push API để push notification.

https://developer.mozilla.org/en-US/docs/Web/API/Push_API

// app/assets/javascript/application.js
// request permission
Notification.requestPermission().then(function (result) {})

Để push notification, mình có thể dùng như sau:

new Notification(title, {body: body})

Từ đó, mình sẽ áp dụng cái này vào trong notifications_channel trong hàm received như sau:

App.notifications = App.cable.subscriptions.create("NotificationsChannel", { connected: function() {console.log("connected")}, disconnected: function() {}, received: function(data) { if (Notification.permission === "granted") { var title = "Push Notification" var options = {body: data["message"]} new Notification(title, options) } }
});

Như trên, sau khi submit form, controller sẽ broadcast đến notifications_channel. Sau đó hàm received sẽ được thực hiện và call push notification lên.

Đến đây là đã xong. Vào localhost:3000 và chạy thử nhé!

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tôi cá là bạn không biết những điều này - Ruby on rails ( Phần 2)

Các bạn có thể theo dõi phần 1 ở đây :. https://viblo.asia/p/toi-ca-la-ban-khong-biet-nhung-dieu-nay-ruby-on-rails-phan-1-WAyK8DDeKxX. 5.

0 0 211

- vừa được xem lúc

Rails Memoization

Memoization is a process that can be used to speed up rails methods. It caches the results of methods that do time-consuming work, work that only needs to be done once. Here is an example. Example.

0 0 34

- vừa được xem lúc

Tại sao Rails lại dùng cả Webpack lẫn Sprocket?

Khi Rails 6 được ra mắt, có thể bạn đã từng tự hỏi. WTF, sao Webpack đã được add vào rồi, mà Sprocket vẫn tồn tại thế kia . Chẳng phải Webpack và Sprocket được dùng để giải quyết chung một công việc hay sao. Hoặc cả đây:.

0 0 47

- vừa được xem lúc

Bạn nên sử dụng Elasticsearch trong ứng dụng Ruby On Rails như thế nào?

Elasticsearch là một công cụ phân tích và mã nguồn mở mạnh mẽ và linh hoạt, phân tán, theo thời gian thực. Đó là tiêu chuẩn vàng trong công nghệ tìm kiếm.

0 0 70

- vừa được xem lúc

Form object pattern trong rails

1.Mở đầu.

0 0 100

- vừa được xem lúc

Sử dụng Twilio để gửi SMS trong ứng dụng Ruby on Rails

Ngoài cách xác nhận tài khoản hay gửi thông báo bằng email thì hôm nay mình sẽ hướng dẫn các bạn 1 cách nữa là thông qua SMS/Voice. Công cụ sử dụng sẽ là gem Twilio. Installation. Để cài đặt bằng Bundler, hãy lấy phiên bản mới nhất:.

0 0 56