Class: Modulofcm::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/modulofcm/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, api_key: nil, google_application_credentials_path: nil, google_application_credentials: nil, firebase_project_id: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
# File 'lib/modulofcm/client.rb', line 13

def initialize(name:, api_key: nil,
               google_application_credentials_path: nil, google_application_credentials: nil,
               firebase_project_id: nil)
  @name = name.to_sym
  @api_key = api_key
  @google_application_credentials_path = google_application_credentials_path
  @google_application_credentials = google_application_credentials
  @firebase_project_id = firebase_project_id
  @mode = :api_v1
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/modulofcm/client.rb', line 11

def api_key
  @api_key
end

#firebase_project_idObject (readonly)

Returns the value of attribute firebase_project_id.



11
12
13
# File 'lib/modulofcm/client.rb', line 11

def firebase_project_id
  @firebase_project_id
end

#google_application_credentialsObject (readonly)

Returns the value of attribute google_application_credentials.



11
12
13
# File 'lib/modulofcm/client.rb', line 11

def google_application_credentials
  @google_application_credentials
end

#modeObject (readonly)

Returns the value of attribute mode.



11
12
13
# File 'lib/modulofcm/client.rb', line 11

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/modulofcm/client.rb', line 11

def name
  @name
end

Instance Method Details

#google_application_credentials_pathObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/modulofcm/client.rb', line 68

def google_application_credentials_path
  if @google_application_credentials_path.present? || @google_application_credentials.blank?
    return @google_application_credentials_path
  end

  @google_application_credentials_path = if @google_application_credentials.is_a?(String)
                                           FileIO.new(@google_application_credentials, 'creds.json')
                                         elsif @google_application_credentials.respond_to?(:to_json)
                                           FileIO.new(@google_application_credentials.to_json, 'creds.json')
                                         else
                                           @google_application_credentials
                                         end
end

#push(token, data: nil, title: nil, body: nil, sound: nil, content_available: true) ⇒ Object

All keyword arguments are optional and references a field in Firebase documentation: Legacy API: firebase.google.com/docs/cloud-messaging/http-server-ref APIv1 : firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

title: The notification's title.
  Legacy: `notification.title`
  APIv1: `message.notification.title`
body: The notification's body text.
  Legacy: `notification.body`
  APIv1: `message.notification.body`,
sound: The sound to play when the device receives the notification.
  Legacy: `notification.sound`
  APIv1:
    android: `message.android.sound`
    ios: `message.apns.aps.sound`
content_available: Allow to awaken the iOS app.
  Legacy: `content_available` - ignored on non-iOS devices.
  APIv1:
    ios: `message.apns.aps.content-available`
data: Arbitrary key/value payload, which must be UTF-8 encoded. The key should not be a reserved word
  ("from", "message_type", or any word starting with "google" or "gcm").
  Legacy: `data`
  APIv1: `message.data`

Raises:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/modulofcm/client.rb', line 46

def push(token, data: nil, title: nil, body: nil, sound: nil, content_available: true)
  raise NoTokenError if token.blank?
  raise EmptyNotificationError if data.blank? && title.blank? && body.blank?

  case @mode
  when :legacy
    push_legacy(token, data: data, title: title, body: body, sound: sound, content_available: content_available)
  else
    push_v1(token, data: data, title: title, body: body, sound: sound, content_available: content_available)
  end
end