@@ -17,9 +17,9 @@ def add_recipient(user)
17
17
@recipients << user
18
18
end
19
19
20
- # https://docs.expo.io/versions/latest/guides/ push-notifications/
20
+ # https://docs.expo.io/push-notifications/sending -notifications/
21
21
# 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
23
23
def deliver
24
24
messages = @recipients . map do |user |
25
25
ExpoPushToken . where ( user : user ) . map do |token |
@@ -34,11 +34,53 @@ def deliver
34
34
messages . flatten!
35
35
36
36
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
+ }
42
84
end
43
85
end
44
86
end
0 commit comments