Created
January 28, 2021 13:51
-
-
Save faust45/1aff17d2da97695f78ff77bc7888a52b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PushNotifications | |
class Enqueuer | |
attr_accessor :user | |
def initialize(u) | |
@user = u | |
end | |
def enqueue(payload) | |
token = User::DeviceToken.where(user_id: user.id, token_type: 'ios').last | |
if token | |
send_ios(token.token, payload) | |
end | |
# android_tokens = User::DeviceToken.where(user_id: user.id, token_type: 'android') | |
# android_tokens.each do |token| | |
# send_android(token, payload) | |
# end | |
end | |
def send_ios(token, payload) | |
n = Rpush::Apns::Notification.new | |
n.app = Rpush::Apns::App.find_by_name("ios_app") | |
n.content_available = true | |
n.device_token = token | |
n.alert = payload[:body] | |
n.data = { | |
data: payload[:data], | |
aps: { alert: payload[:body] }, | |
pn_type: (payload[:data][:pn_type] if payload[:data].present? && payload[:data][:pn_type].present?) | |
} | |
n.save! | |
end | |
def send_android(token, payload) | |
n = Rpush::Gcm::Notification.new | |
n.app = Rpush::Gcm::App.find_by_name("android_app") | |
n.registration_ids = [token] | |
n.priority = 'high' | |
n.data = payload[:data] | |
n.notification = { | |
body: payload[:body], | |
title: payload[:title], | |
} | |
n.save! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment