Skip to content

Commit 777cf34

Browse files
committed
Merge branch '16-better-error-log-and-limit-max-100' into 'develop'
Resolve "Better error log (for many experience ids) and limit max 100 for each request" Closes #16 See merge request gtt/redmine_expo_push!8
2 parents 464a86d + 247270f commit 777cf34

File tree

6 files changed

+78
-25
lines changed

6 files changed

+78
-25
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
gem "exponent-server-sdk"
1+
gem 'exponent-server-sdk', git: 'https://github.com/gtt-project/expo-server-sdk-ruby.git', tag: 'v0.1.0-dev1'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<p>
2+
<%= content_tag(:label, l(:label_expo_push_experience_id)) %>
3+
<%= text_field_tag('settings[experience_id]',
4+
@settings['experience_id'],
5+
:size => 30) %>
6+
</p>

config/locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ en:
1212
zero: Currently no devices are registered.
1313
one: Currently one device is registered.
1414
other: "Currently %{count} devices are registered."
15+
field_push_notifications: Push notifications
16+
label_expo_push_experience_id: Expo Experience ID

config/locales/ja.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ja:
2+
hooks:
3+
redmine_expo_push:
4+
my_account_preferences:
5+
delete: 削除
6+
delete_all: すべて削除
7+
push_notifications:
8+
enabled: 有効
9+
disabled: 無効
10+
enabled_no_email: 有効・メールなし
11+
x_devices_registered:
12+
zero: 現在、デバイスは登録されていません。
13+
one: 現在、1台のデバイスが登録されています。
14+
other: "現在、%{count}台のデバイスが登録されています。"
15+
field_push_notifications: Push通知
16+
label_expo_push_experience_id: Expo Experience ID

init.rb

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,10 @@
1414
description 'Notify mobile app users through push notifications'
1515
version '1.0.0'
1616

17-
requires_redmine version_or_higher: '3.4.0'
17+
requires_redmine version_or_higher: '4.0.0'
1818

19-
#settings default: {
20-
#}, partial: 'redmine_text_blocks/settings'
21-
22-
# project_module :text_blocks do
23-
#
24-
# permission :view_text_blocks, {}, require: :member, read: true
25-
# permission :manage_text_blocks, {
26-
# text_blocks: %i( new edit update create destroy ),
27-
# projects: %i( manage_text_blocks )
28-
# }, require: :member
29-
# end
30-
31-
# menu :admin_menu, :text_blocks,
32-
# { controller: 'text_blocks', action: 'index' },
33-
# caption: :label_text_block_plural, :html => {:class => 'icon'}
19+
settings default: {
20+
'experience_id' => "@owner/slug(projectname)",
21+
}, partial: 'settings/expo_push_settings'
3422

3523
end
36-

lib/redmine_expo_push/notification.rb

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def add_recipient(user)
1717
@recipients << user
1818
end
1919

20-
# https://docs.expo.io/versions/latest/guides/push-notifications/
20+
# https://docs.expo.io/push-notifications/sending-notifications/
2121
# https://github.com/expo/expo-server-sdk-ruby
22-
# https://docs.expo.io/versions/latest/guides/push-notifications/#message-format
22+
# https://docs.expo.io/push-notifications/sending-notifications/#message-request-format
2323
def deliver
2424
messages = @recipients.map do |user|
2525
ExpoPushToken.where(user: user).map do |token|
@@ -34,11 +34,53 @@ def deliver
3434
messages.flatten!
3535

3636
if messages.any?
37-
begin
38-
Exponent::Push::Client.new(gzip: true).send_messages messages
39-
rescue Exception
40-
Rails.logger.error "error sending push notifications:\n#{$!}\n" + $!.backtrace.join("\n")
41-
end
37+
messages.each_slice(100) {|message_list|
38+
begin
39+
if message_list.length == 0
40+
next
41+
end
42+
tokens = message_list.map {|message|
43+
message[:to]
44+
}
45+
Rails.logger.info "tokens: #{tokens}"
46+
handler = Exponent::Push::Client.new(gzip: true).send_messages message_list
47+
if handler.errors.present?
48+
Rails.logger.error "handler.errors: #{handler.errors}"
49+
end
50+
if handler.receipt_ids.present?
51+
Rails.logger.info "handler.receipt_ids: #{handler.receipt_ids}"
52+
end
53+
rescue Exception => e
54+
Rails.logger.error "error sending push notifications: #{e}"
55+
if e.message.present? and e.message.start_with?("Unknown error format: ") \
56+
and Setting.plugin_redmine_expo_push['experience_id'].present?
57+
# https://github.com/expo/expo/issues/6771#issuecomment-780095144
58+
begin
59+
response = JSON.parse(e.message.sub(/^Unknown error format: /, ''))
60+
error = response.fetch("errors").first
61+
code = error.fetch("code")
62+
if code == "PUSH_TOO_MANY_EXPERIENCE_IDS"
63+
Rails.logger.info "retrying to send messages"
64+
message = error.fetch("message")
65+
details = error.fetch("details")
66+
details.keys.each {|key|
67+
if key != Setting.plugin_redmine_expo_push['experience_id']
68+
details[key].each {|token|
69+
message_list.reject! {|message|
70+
message[:to] == token
71+
}
72+
}
73+
end
74+
}
75+
# Retry with cleaned up message_list
76+
retry
77+
end
78+
rescue Exception => e2
79+
Rails.logger.info "retrying to send messages failed: #{e2}"
80+
end
81+
end
82+
end
83+
}
4284
end
4385
end
4486
end

0 commit comments

Comments
 (0)