Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please tell me how I can implement web push #127

Open
m7madmagdy opened this issue Sep 19, 2024 · 2 comments
Open

Please tell me how I can implement web push #127

m7madmagdy opened this issue Sep 19, 2024 · 2 comments

Comments

@m7madmagdy
Copy link

No description provided.

@erimicel
Copy link
Contributor

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

@m7madmagdy
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants