docbrown/app/models/device.rb
Fernando Valverde e602d50d32
Push Notification multi app support (#13304)
* PushNotificationTarget model + /admin/push_notifications (index)

* Admin panel CRUD + request tests

* Migrate Redis backed Rpush model responsibilities into PushNotificationTarget

* Fix failing specs

* Fix conflicts + clean up test by using constant reference

* Removed unused policy

* policy + services + misc feedback

* PushNotificationTarget refactored to AppIntegration

* Review feedback

* Some small cleanup

* Refactor AppIntegration -> ConsumerApp

* Fixing specs

* Trigger Travis

* More naming refactor changes

* Refactor services into queries

* Revert to where(...).first, fix typo and tests

* Apply suggestions from code review

Co-authored-by: rhymes <rhymes@hey.com>

* PR review feedback - create_or_find_by, validations, renaming + more tests

* Fix aria-label text

* Remove unnecessary individual index - composite index will do

Co-authored-by: rhymes <rhymes@hey.com>
2021-04-21 17:13:01 -06:00

56 lines
1.4 KiB
Ruby

class Device < ApplicationRecord
# @fdoxyz to remove app_bundle from Device soon
self.ignored_columns = ["app_bundle"]
belongs_to :user
belongs_to :consumer_app
IOS = "iOS".freeze
ANDROID = "Android".freeze
validates :token, uniqueness: { scope: %i[user_id platform consumer_app_id] }
validates :platform, inclusion: { in: [IOS, ANDROID] }
def create_notification(title, body, payload)
case platform
when IOS
ios_notification(title, body, payload)
when ANDROID
android_notification(title, body, payload)
end
end
private
def ios_notification(title, body, payload)
n = Rpush::Apns2::Notification.new
n.device_token = token
n.app = ConsumerApps::RpushAppQuery.call(
app_bundle: consumer_app.app_bundle,
platform: platform,
)
n.data = {
aps: {
alert: {
title: SiteConfig.community_name,
subtitle: title,
body: body
},
'thread-id': SiteConfig.community_name
},
data: payload
}
n.save!
end
def android_notification(title, body, payload)
n = Rpush::Gcm::Notification.new
n.app = ConsumerApp.rpush_app(app_bundle: app_bundle, platform: platform)
n.registration_ids = [token]
n.priority = "high"
n.content_available = true
n.notification = { title: title, body: body }
n.data = { data: payload }
n.save!
end
end