-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrending_topic_push.rb
97 lines (81 loc) · 2.34 KB
/
trending_topic_push.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'apnotic'
require 'logger'
require './news'
require './user'
def news_to_broadcast(date)
trending = News.trending(date, 3)
keyword = trending['keywords'].sample
return if keyword.nil?
trending['news'][keyword].first
end
def notification_from_news_item(item, token)
notification = Apnotic::Notification.new(token)
notification.alert = {
'title' => item['source_name'],
'body' => item['title']
}
notification.mutable_content = true
notification.category = 'news_apn'
notification.sound = 'default'
notification.topic = 'ar.com.betzerra.headlines'
notification.custom_payload = {
'media-url' => item['img_url'],
'post-id' => item['news_id'],
'post-url' => item['url']
}
notification
end
def apns_connection
# for production pushes use:
# https://api.push.apple.com:443
# https://api.development.push.apple.com:443
#Apnotic::Connection.new(
# cert_path: 'apns_development.pem',
# url: 'https://api.development.push.apple.com:443'
#)
if ENV["APN_CERT"].nil?
# adding backwards compatibility to non-dokku instances
Apnotic::Connection.new(
cert_path: 'apns_development_production.pem'
)
else
Apnotic::Connection.new(
cert_path: StringIO.new(ENV["APN_CERT"])
)
end
#Apnotic::Connection.new(
# auth_method: :token,
# cert_path: "AuthKey_VU6355SQLQ.p8",
# key_id: "VU6355SQLQ",
# team_id: "2334RGUT2P"
#)
end
def single_sync_push_to_token(connection, notification)
connection.push(notification)
# do something with "response"... I guess?
end
def single_async_push_to_token(connection, notification)
push = connection.prepare_push(notification)
push.on(:response) do |response|
# do something... I guess?
#puts response.ok?
#puts response.status
#puts response.body
end
connection.push_async(push)
end
def push_trending_news(date)
logger = Logger.new(STDOUT)
connection = apns_connection
item = news_to_broadcast(date)
User.where('source = "iOS" AND device_token IS NOT NULL').each do |u|
logger.debug("Treding topic push to: #{u.device_token}")
notification = notification_from_news_item(item, u.device_token)
single_async_push_to_token(connection, notification)
end
# wait for all requests to be completed
connection.join
connection.close
end
#date = Time.now.strftime('%Y-%m-%d')
#push_trending_news(date)