AndrewBarba/fetch-http2

Expose client.close()?

Closed this issue · 6 comments

Hello and thanks for a nice library.

I tried to convert this code to fetch-http2. I ended up with:

const options = {
    method: 'POST',
    headers: {
      'apns-topic': 'com.my.app',
      authorization: `bearer ${authorizationToken}`,
    },
    body: JSON.stringify(dataObj),
  }

const res = await fetch(
        `https://api.push.apple.com/3/device/${token}`,
        options
      )
      console.log(res.ok)

It sort of works, I get the push notification, but then it hangs and the function never exists. I get the same behaviour if I comment out the client.close() call from the reference code above.

Can this be solved without calling client.close()

Have you tried using my apns library (which is built on fetch-http2)? That should give you a better idea on how to correctly implement this if you want to do it from scratch for whatever reason

I think this will give you what you want though: #5

I had not seen apns2, but I seem have the same issue there, it blocks and does not return using this code:

 const client = new ApnsClient({
    team: `team-id`,
    keyId: `key-id`,
    signingKey: key,
    defaultTopic: `com.my.app`,
  })
   const bn = new Notification(token, {
    alert: body,
    badge,
    data,
  })
try {
  await client.send(bn)
} catch (err) {
  console.error(err.reason)
}

I'm going to merge that PR and then update apns2 with a keepAlive option so you can set it to false

I just released v1.4.0 of this package, give it a shot. If its working correctly I'll update apns2

I just released v1.4.0 of this package, give it a shot. If its working correctly I'll update apns2

Thanks! Setting keepAlive: false works, it now returns as expected.