Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I keep getting :bad_device_token for APN #164

Closed
edisonywh opened this issue Jan 19, 2020 · 6 comments
Closed

I keep getting :bad_device_token for APN #164

edisonywh opened this issue Jan 19, 2020 · 6 comments

Comments

@edisonywh
Copy link

Environment

  • Elixir & Erlang/OTP versions (elixir --version): Elixir 1.9.1 (compiled with Erlang/OTP 20), Erlang/OTP 22
  • Operating system: Mac OS

Current behavior

I've been trying to just send a notification to my iOS device and it hasn't been working. Android worked fine, but for iOS it just never worked. I use runtime config, like so:

# config.exs
config :pigeon,
  workers: [
    {Slick.Pigeon, :apns_jwt}
  ]
defmodule Slick.Pigeon do
  def apns_jwt do
    APNS.JWTConfig.new(
      name: :apns_default,
      key: System.get_env("APN_AUTH_KEY"),
      key_identifier: System.get_env("APN_KEY_IDENTIFIER"),
      team_id: System.get_env("APN_TEAM_ID"),
      mode: :prod
    )
  end
end

My frontend is Flutter, I'm using Google's firebase_messasing package to get the device token.

Here's how I am sending the notification:

Pigeon.APNS.Notification.new("coconut", token)
|> Pigeon.APNS.Notification.put_alert(%{
  "title" => "coconut",
  "body" =>"coconut", 
})
|> Pigeon.APNS.push()

and the response:

%Pigeon.APNS.Notification{
  collapse_id: nil,
  device_token: "eL_kWlLwRfE:APA91bHedOstPi9tMF6YZet1pebmRPwt2h3tVcKX4Uxgp96USfGrE2tj3ok5CCvlCwCMZFmpipboh47L_IvkVTG20M47QsaDiNaWPQrCV9MmtdbEdLsiT2ets8LnYX4U2J1PFa9sJAXE",
  expiration: nil,
  id: nil,
  payload: %{
    "aps" => %{"alert" => %{"body" => "coconut", "title" => "coconut"}}
  },
  priority: nil,
  push_type: "alert",
  response: :bad_device_token,
  topic: nil
}

Expected behavior

I expect it to send notification to my iOS device

@edisonywh
Copy link
Author

Slightly unrelated, but depending on how I pass in the token my key for APNs will also result in:

argument error: [[], {:jose_jws, {:jose_jws_alg_ecdsa, :ES256}, :undefined, %{"kid" => "...", "typ" => "JWT"}}, %{"aud" => "Joken", "exp" => ..., "iat" => ..., "iss" => "...", "jti" => "...", "nbf" => ...}]

@hpopp
Copy link
Member

hpopp commented Jan 19, 2020

If you're using firebase, use FCM notifications for all push notifications, even iOS devices. APNS workers talk to Apple servers, which would reject the device token you've sent.

@edisonywh
Copy link
Author

edisonywh commented Jan 19, 2020

@hpopp does that mean I only have to set-up FCM then?

I don't think I see it in the docs, do you have an example perhaps? Thanks!

Also, why would the Apple servers reject the token I sent? What's wrong with that token?

@hpopp
Copy link
Member

hpopp commented Jan 20, 2020

Think about it this way... if Firebase on Google servers was the one issuing device tokens, Apple servers wouldn't know anything about it.

Using FCM for iOS is not something I personally use in production, but I have verified that it does indeed work. You'll notice that some of the helper functions in FCM.Notification are iOS-specific. I don't have an actual guide for this, but perhaps I should write one.

@edisonywh
Copy link
Author

Ah, I thought the the device tokens were Apple's tokens. I thought it didn't matter how the token was retrieved, because ultimately it's a "device token" that belongs to the device. Looks like I'm wrong?

Yeah a guide would be immensely helpful, I'm going to try it out and see how to get it to work, thanks for the pointers! Hopefully this works

@edisonywh
Copy link
Author

I just gave it a go and I got it under 30 minutes. It was incredibly easy, I spent so much time figuring out why the token was bad turns out this whole time I had a FCM token instead of an iOS token.. Thanks for the help @hpopp!

Anyway, here's a code snippet for people who stumble upon this and want to send notification to APN via FCM:

body = %{"title" => title, "body" => message}

Pigeon.FCM.Notification.new(token)
|> Pigeon.FCM.Notification.put_notification(body)
|> Pigeon.FCM.Notification.put_content_available(true)
|> Pigeon.FCM.push()

That's literally all you need to do to send notifications to iOS. No need to fuss around with APN settings!

@hpopp hpopp closed this as completed Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants