Apple Push Notifications on IC

Hey team,

I’m banging my head against the wall trying to get Apple Push Notifications working with my dapp. Here’s the deal:

  • APNS setup is solid (JWT, device token, the works)
  • cURL to APNS sandbox? Works great
  • Same request from an IC canister? No dice. :sob:

I’m getting this error:
“Connecting to api.sandbox.push.apple.com failed: Failed to directly connect: client error (SendRequest)”

So, I’m throwing this out there:

  1. Has anyone actually got APNS calls working straight from an IC canister?
  2. Got any clever workarounds up your sleeve?

I’ve confirmed my call structure is spot-on using cURL, so I’m pretty sure it’s an IC connectivity thing.

If you’ve wrestled this beast and won (or even just have some war stories to share), I’m all ears!

Cheers!

I suspect it has something to do with the HTTP Outcall lack of HTTP/2 support, but I can’t find anything in literature. Anyone know if this is the case?

It’s probably not your issue and not sure if it will help, but there are two common problems with HTTP outcalls that I’m aware of:

  • Lack of IPv6 support: The target API must support IPv6.
  • Identical HTTP outcalls answers: For one HTTP outcall, multiple requests are triggered and their responses should be identical. For example, this can be ensured with an idempotency key.

The latter is probably not your issue given your error message, maybe the former?

Great points. The APNS server allegedly only supports IPv6, so not sure if that is the problem. I don’t understand enough about HTTP/2 but I suspect that that IC Outcalls are HTTP/1.1.

1 Like

I forwarded your question to the team.

Hi, thanks for letting us know about the problem. I suspect the reason the requests fail is that the HTTPS outcalls adapter does not support HTTP/2, and it is listed a requirement to connect to APNS in the apple developer docs [0].

I have created PR to support HTTP/2 with outcalls, so hopefully this should resolve your issue. Here is the PR to track the feature feat(https-outcalls): Enable H/2 support for outcalls by DSharifi · Pull Request #2142 · dfinity/ic · GitHub.

Depending on what subnet your canister lives on, I estimate this feature should be enabled on the IC in ~1-2 weeks.

[0] Sending notification requests to APNs | Apple Developer Documentation

4 Likes

Wow, this is fantastic! Thanks for submitting that.

Next question:

I’ve been calculating the cost of Internet Computer outcalls for push notifications in a messaging app. Here’s what I found:

1 trillion cycles = 1 XDR = 1.33 USD
20 billion cycles (typical for an outcall) = 0.02 XDR = 0.0266 USD ≈ 2.66 cents

At 2.66 cents per push notification, this seems expensive, especially at scale. For an active user sending 50 messages a day, that’s $1.33 daily or about $40 per month just in notification costs.

Am I off base here, or is this indeed prohibitively expensive for a messaging app? Has anyone found ways to optimize this or implement alternative solutions?

Interested in hearing your experiences and thoughts on handling this at scale.

1 Like

Thank you for documenting your findings.

And especially for putting an estimated costs number to this.

If I understand correctly, 20B is allocated for a 2MB payload. You need to calculate the size of the outgoing payload, but if you send 20B and your outgoing call only uses, say, 1B, then the remaining 19B will be refunded to your canister. So, it’s essentially a ‘pay-as-you-use’ model.

Thanks for weighing in! So after turning down the allocated cycles to what I actually need to send a push notification, the cost ends up being 65,730,665 cycles (which by my math is approximately $0.000087 per notification). Its actually pretty reasonable! Not quite as cheap as a standard web 2 server, but still fairly reasonable.

The real challenge with these outcalls is the idempotency. I was able to implement outcalls straight to the APNS server, but it technically sends 10-20 push notifications at a time. Apple provides a way for client apps to handle that but its pretty inefficient.

Anyways, if anyone is interested in checking out the app, shoot me a DM and I’ll add you to the testflight.

I’m curious to learn more about your use case. When you send the request to APNS, does it trigger 13x duplicate notifications?

So I wrote an e2e encrypted messaging app to learn more about ios development with the IC and e2e encryption in general. I use the apple push notifications to then initiate a query of my canister and pull the actual data down.

Yes, it does send a group of notifications (sometimes 10ish sometimes 20ish), but using the apns-collapse-id field in the push notification package, ios does a decent job only displaying it once. Sometimes it buzzes 2-3 times.

When I am in the app, i wrote a little logic to handle the idempotency, so its not an issue, but its not an elegant solution.

It was much better when I used a google cloud server to filter through and only send one notification, but I removed that because it kind of defeats the decentralized purpose.