fcm icon indicating copy to clipboard operation
fcm copied to clipboard

Please tell me how I can implement web push

Open m7madmagdy opened this issue 1 year ago • 2 comments

m7madmagdy avatar Sep 19 '24 15:09 m7madmagdy

You can do something like this I believe:

message = {
  'token': "000iddqd", # send to a specific device
  'data': {
    payload: {
      data: {
        id: 1
      }
    }.to_json
  },
  'notification': {
    title: notification.title_th,
    body: notification.body_th,
  },
  'webpush': {
      ....data
  }
}

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#WebpushConfig

erimicel avatar Sep 20 '24 08:09 erimicel

Hello @erimicel, can you please suggest how to configure the js files to show messages on my dashboard

I already have a sender notification service with topic or one device, But after update the params and include the webpush what should i do to make it work on the browser

# frozen_string_literal: true

module Notifications
  class Sender
    attr_reader :client_id, :data

    def initialize(client_id, data = {})
      @client_id = client_id
      @data = data
    end

    # Send to all users with a custom topic => ("KSA, "KUWAIT", "male"....etc)
    def send_to_topic(topic)
      fcm.send_to_topic(topic, build_message_params)
    end

    # Sending messages to device groups => (many tokens for the same recipient)
    def send_to_specific_devices(recipient_id)
      tokens = registration_ids(recipient_id)
      return if tokens.blank?

      tokens.each do |token|
        message = { token: }.merge(build_message_params)

        fcm.send_v1(message)
      end
    end

    private

    def fcm
      fcm_service.call
    end

    def fcm_service
      Notifications::FcmService.new(client_id)
    end

    def build_message_params
      Notifications::MessageParams.build_message_params(data)
    end

    def registration_ids(recipient_id)
      Device.where(deviceable_id: recipient_id).pluck(:registration_id).compact.reject(&:empty?)
    end
  end
end

m7madmagdy avatar Sep 23 '24 18:09 m7madmagdy