From 257ea3e47e275ac94e731dbe402edac36d7e35c9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 6 Apr 2024 15:26:44 -0400 Subject: [PATCH] Add version to registration --- lib/tusker_push/forwarder.ex | 5 +++-- lib/tusker_push/registration.ex | 10 +++++++++- .../controllers/registrations_controller.ex | 6 ++++-- .../migrations/20240405181919_create_registrations.exs | 2 ++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/tusker_push/forwarder.ex b/lib/tusker_push/forwarder.ex index f3b10c5..abc6f36 100644 --- a/lib/tusker_push/forwarder.ex +++ b/lib/tusker_push/forwarder.ex @@ -3,7 +3,7 @@ defmodule TuskerPush.Forwarder do alias TuskerPush.Registration @spec forward(Registration.t(), binary(), String.t(), String.t()) :: :ok | {:error, term()} - def forward(registration, body, salt, key) do + def forward(%Registration{push_version: 1} = registration, body, salt, key) do payload = %{ "aps" => %{ "alert" => %{ @@ -14,7 +14,8 @@ defmodule TuskerPush.Forwarder do "reg_id" => registration.id, "data" => Base.encode64(body), "salt" => salt, - "pk" => key + "pk" => key, + "v" => 1 } Apns.send(registration, payload) diff --git a/lib/tusker_push/registration.ex b/lib/tusker_push/registration.ex index 50fa698..e720c80 100644 --- a/lib/tusker_push/registration.ex +++ b/lib/tusker_push/registration.ex @@ -8,6 +8,7 @@ defmodule TuskerPush.Registration do storekit_original_transaction_id: String.t(), apns_environment: String.t(), apns_device_token: String.t(), + push_version: integer(), inserted_at: NaiveDateTime.t(), updated_at: NaiveDateTime.t() } @@ -21,10 +22,17 @@ defmodule TuskerPush.Registration do # hex-encoded field :apns_device_token, :string + field :push_version, :integer + timestamps() end - @required_fields [:storekit_original_transaction_id, :apns_environment, :apns_device_token] + @required_fields [ + :storekit_original_transaction_id, + :apns_environment, + :apns_device_token, + :push_version + ] def create_changeset(registration \\ %__MODULE__{}, params) do registration diff --git a/lib/tusker_push_web/controllers/registrations_controller.ex b/lib/tusker_push_web/controllers/registrations_controller.ex index f60dcb8..6c813d1 100644 --- a/lib/tusker_push_web/controllers/registrations_controller.ex +++ b/lib/tusker_push_web/controllers/registrations_controller.ex @@ -8,13 +8,15 @@ defmodule TuskerPushWeb.RegistrationsController do def create(conn, %{ "transaction_id" => transaction_id, "environment" => env, - "device_token" => token + "device_token" => token, + "push_version" => version }) do with {:ok, %Registration{id: id}} <- TuskerPush.register(%{ storekit_original_transaction_id: transaction_id, apns_environment: env, - apns_device_token: token + apns_device_token: token, + push_version: version }) do conn |> json(%{ diff --git a/priv/repo/migrations/20240405181919_create_registrations.exs b/priv/repo/migrations/20240405181919_create_registrations.exs index c039cc9..1c62e23 100644 --- a/priv/repo/migrations/20240405181919_create_registrations.exs +++ b/priv/repo/migrations/20240405181919_create_registrations.exs @@ -10,6 +10,8 @@ defmodule TuskerPush.Repo.Migrations.CreateRegistrations do add :apns_environment, :string, null: false add :apns_device_token, :string, null: false + add :push_version, :integer, null: false + timestamps() end end