Skip to content
New issue

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

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

Already on GitHub? # to your account

Expose client.close()? #4

Closed
ottob opened this issue Feb 20, 2024 · 6 comments
Closed

Expose client.close()? #4

ottob opened this issue Feb 20, 2024 · 6 comments

Comments

@ottob
Copy link

ottob commented Feb 20, 2024

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()

@AndrewBarba
Copy link
Owner

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

@AndrewBarba
Copy link
Owner

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

@ottob
Copy link
Author

ottob commented Feb 20, 2024

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)
}

@AndrewBarba
Copy link
Owner

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

@AndrewBarba
Copy link
Owner

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

@ottob
Copy link
Author

ottob commented Feb 20, 2024

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants