Skip to content

Commit

Permalink
fix: reset stream_id when the http2 connection is closed (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaphod534 committed Nov 30, 2021
1 parent 54735f8 commit 72186b2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
19 changes: 15 additions & 4 deletions lib/pigeon/apns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule Pigeon.APNS do
```
n = Pigeon.APNS.Notification.new("your message", "your device token", "your push topic")
```
5. Send the packet. Pushes are synchronous and return the notification with an
updated `:response` key.
Expand Down Expand Up @@ -133,8 +133,8 @@ defmodule Pigeon.APNS do
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
```
6. Convert the key. Be sure to set a PEM pass phrase here. The pass phrase must be 4 or
more characters in length or this will not work. You will need that pass phrase added
6. Convert the key. Be sure to set a PEM pass phrase here. The pass phrase must be 4 or
more characters in length or this will not work. You will need that pass phrase added
here in order to remove it in the next step.
```
Expand Down Expand Up @@ -206,7 +206,13 @@ defmodule Pigeon.APNS do
case connect_socket(config) do
{:ok, socket} ->
Configurable.schedule_ping(config)
{:noreply, %{state | socket: socket}}

state =
state
|> reset_stream_id()
|> Map.put(:socket, socket)

{:noreply, state}

{:error, reason} ->
{:stop, reason}
Expand Down Expand Up @@ -251,4 +257,9 @@ defmodule Pigeon.APNS do
def inc_stream_id(%{stream_id: stream_id} = state) do
%{state | stream_id: stream_id + 2}
end

@doc false
def reset_stream_id(state) do
%{state | stream_id: 1}
end
end
21 changes: 16 additions & 5 deletions lib/pigeon/fcm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ defmodule Pigeon.FCM do
```
n = Pigeon.FCM.Notification.new({:token, "reg ID"}, %{"body" => "test message"})
```
5. Send the notification.
On successful response, `:name` will be set to the name returned from the FCM
API and `:response` will be `:success`. If there was an error, `:error` will
5. Send the notification.
On successful response, `:name` will be set to the name returned from the FCM
API and `:response` will be `:success`. If there was an error, `:error` will
contain a JSON map of the response and `:response` will be an atomized version
of the error type.
Expand Down Expand Up @@ -158,7 +158,13 @@ defmodule Pigeon.FCM do
case connect_socket(config) do
{:ok, socket} ->
Configurable.schedule_ping(config)
{:noreply, %{state | socket: socket}}

state =
state
|> reset_stream_id()
|> Map.put(:socket, socket)

{:noreply, state}

{:error, reason} ->
{:stop, reason}
Expand Down Expand Up @@ -237,4 +243,9 @@ defmodule Pigeon.FCM do
def inc_stream_id(%{stream_id: stream_id} = state) do
%{state | stream_id: stream_id + 2}
end

@doc false
def reset_stream_id(state) do
%{state | stream_id: 1}
end
end
19 changes: 15 additions & 4 deletions lib/pigeon/legacy_fcm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Pigeon.LegacyFCM do
defp legacy_fcm_opts do
[
adapter: Pigeon.LegacyFCM,
adapter: Pigeon.LegacyFCM,
key: "your_fcm_key_here"
]
end
Expand All @@ -74,8 +74,8 @@ defmodule Pigeon.LegacyFCM do
msg = %{"body" => "your message"}
n = Pigeon.LegacyFCM.Notification.new("your device registration ID", msg)
```
5. Send the notification.
5. Send the notification.
Pushes are synchronous and return the notification with
updated `:status` and `:response` keys. If `:status` is success, `:response`
Expand Down Expand Up @@ -239,7 +239,13 @@ defmodule Pigeon.LegacyFCM do
case connect_socket(config) do
{:ok, socket} ->
Configurable.schedule_ping(config)
{:noreply, %{state | socket: socket}}

state =
state
|> reset_stream_id()
|> Map.put(:socket, socket)

{:noreply, state}

{:error, reason} ->
{:stop, reason}
Expand Down Expand Up @@ -284,4 +290,9 @@ defmodule Pigeon.LegacyFCM do
def inc_stream_id(%{stream_id: stream_id} = state) do
%{state | stream_id: stream_id + 2}
end

@doc false
def reset_stream_id(state) do
%{state | stream_id: 1}
end
end

0 comments on commit 72186b2

Please sign in to comment.