3

I use the following code to send notification through the apple push notification api. It worked well until a few days ago when it started throwing an error:

Socket was remotely closed

This is the code that I'm using:

p8key = "*****"
private_key = OpenSSL::PKey::EC.new p8key

token = JWT.encode(
  {
    'iss': "*****",
    'iat': DateTime.now().to_time.to_i
  },
  private_key,
  '***',
  header_fields=
  {
    'alg':"***",
    "kid": "***"
  }
)

client = NetHttp2::Client.new("https://api.push.apple.com:443")
client.on(:error) { |exception| puts "Exception has been raised: #{exception}" }

msg = "Hello"
body = { aps: { alert: msg, sound: "sound.mp3", badge: 1 } }.to_json

request = client.call(
    :post,
    "/3/device/#{recipient.push_token}",
    body: body,
    headers:
    {
      'authorization' => "bearer #{token}",
      "apns-topic" => "com.dogtime.dogtimeinit"
    }
  )

It keeps throwing the error every time I try sending a notification, any idea where it could come from?

Hugo
  • 2,073
  • 6
  • 23
  • 46
  • 3
    I actually found the fix to this. Apple recently updated the way their headers need to be passed. I was using a gem net-http2 that was sending the header not in the correct way. Fortunately they updated their gem recently to support that change, I just updated my gem with bundle update net-http2 and it works now. – Hugo Jul 20 '18 at 14:48
  • 1
    Confirmed that updating the net-http2 gem fixes this. Thanks! – Miles Egan Jul 23 '18 at 06:36

0 Answers0