Class: ActionNativePush::Notification

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Defined in:
lib/action_native_push/notification.rb

Overview

Action Native Push Notification

A notification that can be delivered to devices.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, body: nil, badge: nil, thread_id: nil, sound: nil, high_priority: true, apple_data: {}, google_data: {}, data: {}, **context) ⇒ Notification

Attributes

title - The title
body - The message body
badge - The badge number to display on the app icon
thread_id - The thread ID for grouping notifications
sound - The sound to play when the notification is received
high_priority - Whether to send the notification with high priority (default: true).
                For silent notifications is recommended to set this to false
apple_data - Apple Push Notification Service (APNS) specific data
google_data - Firebase Cloud Messaging (FCM) specific data
data - Custom data to be sent with the notification

Any extra attributes are set inside the `context` hash.


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/action_native_push/notification.rb', line 47

def initialize(title: nil, body: nil, badge: nil, thread_id: nil, sound: nil, high_priority: true, apple_data: {}, google_data: {}, data: {}, **context)
  @title = title
  @body = body
  @badge = badge
  @thread_id = thread_id
  @sound = sound
  @high_priority = high_priority
  @apple_data = apple_data
  @google_data = google_data
  @data = data
  @context = context
end

Instance Attribute Details

#apple_dataObject

Returns the value of attribute apple_data.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def apple_data
  @apple_data
end

#badgeObject

Returns the value of attribute badge.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def badge
  @badge
end

#bodyObject

Returns the value of attribute body.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def body
  @body
end

#contextObject

Returns the value of attribute context.



11
12
13
# File 'lib/action_native_push/notification.rb', line 11

def context
  @context
end

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def data
  @data
end

#google_dataObject

Returns the value of attribute google_data.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def google_data
  @google_data
end

#high_priorityObject

Returns the value of attribute high_priority.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def high_priority
  @high_priority
end

#soundObject

Returns the value of attribute sound.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def sound
  @sound
end

#thread_idObject

Returns the value of attribute thread_id.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def thread_id
  @thread_id
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/action_native_push/notification.rb', line 10

def title
  @title
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/action_native_push/notification.rb', line 12

def token
  @token
end

Class Method Details

.queue_as(name) ⇒ Object



21
22
23
# File 'lib/action_native_push/notification.rb', line 21

def queue_as(name)
  self.queue_name = name
end

Instance Method Details

#as_jsonObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/action_native_push/notification.rb', line 72

def as_json
  {
    title: title,
    body: body,
    badge: badge,
    thread_id: thread_id,
    sound: sound,
    high_priority: high_priority,
    apple_data: apple_data,
    google_data: google_data,
    data: data,
    **context
  }.compact
end

#deliver_later_to(devices) ⇒ Object



66
67
68
69
70
# File 'lib/action_native_push/notification.rb', line 66

def deliver_later_to(devices)
  Array(devices).each do |device|
    ApplicationPushNotificationJob.set(queue: queue_name).perform_later(self.class.name, self.as_json, device)
  end
end

#deliver_to(device) ⇒ Object



60
61
62
63
64
# File 'lib/action_native_push/notification.rb', line 60

def deliver_to(device)
  if enabled
    run_callbacks(:delivery) { device.push(self) }
  end
end