class PUSHER_FCM
Constants
- NOTIFICATION_BASE_URI
constants
Attributes
body[RW]
color[RW]
icon[RW]
priority[RW]
sound[RW]
title[RW]
vibrate[RW]
Public Class Methods
new(api_key, options = {})
click to toggle source
# File lib/pusher_fcm.rb, line 13 def initialize(api_key, options = {}) @api_key = api_key @options = options end
Public Instance Methods
build_body(registration_ids, options = {})
click to toggle source
# File lib/pusher_fcm.rb, line 42 def build_body(registration_ids, options = {}) { registration_ids: registration_ids }.merge(options) end
message_body()
click to toggle source
# File lib/pusher_fcm.rb, line 46 def message_body() {notification: { body: body, title: title, icon: URI('/lib/icon'), sound: "default", vibrate: true, color: "#B42405", priority: "high"}} end
parse_response(response_body)
click to toggle source
# File lib/pusher_fcm.rb, line 54 def parse_response(response_body) response_data = {} res_body = response_body.body || {} case response_body.code when 200 body = JSON.parse(res_body) unless res_body.empty? response_data[:status] = 'success' response_data[:response] = body || {} when 400 response_data[:status] = 'failed' response_data[:response] = 'Json fields are invalid!!' when 401 response_data[:status] = 'failed' response_data[:response] = 'Authentication error!!' when 503 response_data[:status] = 'failed' response_data[:response] = 'Server is temporarily unavailable!!' when 500..599 response_data[:status] = 'failed' response_data[:response] = 'FCM internal server error!!' end response_data end
response_builder(response)
click to toggle source
# File lib/pusher_fcm.rb, line 50 def response_builder(response) parse_response(response) end
send_notification(registration_ids, options = {})
click to toggle source
# File lib/pusher_fcm.rb, line 18 def send_notification(registration_ids, options = {}) raise ArgumentError, 'Api Key not Found' unless !@api_key.nil? raise ArgumentError, 'Body is missing' unless !body.nil? raise ArgumentError, 'DeviceToken not Found' unless !registration_ids.empty? message = message_body() body = build_body(registration_ids, message) params = { body: body.to_json, headers: { 'Authorization' => "key=#{@api_key}", 'Content-Type' => 'application/json' } } response = self.class.post('/send', params.merge(@options)) puts "response" puts response.inspect response_builder(response) end
Also aliased as: send