Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Apple push notification from a Go appengine site

I'm trying to send an Apple push notification from a Go appengine site. I'm using the apns2 library as follows:

cert, err := certificate.FromPemFile(pemFile, "")
if err != nil {
    log.Fatalf("cert error: %v", err)
}
client := apns2.NewClient(cert).Development()
n := &apns2.Notification{...}
if res, err := client.Push(n); err != nil { ... }

On a local development server, it works fine; but in production I'm seeing:

Post https://api.development.push.apple.com/3/device/995aa87050518ca346f7254f3484d7d5c731ee93c35e3c359db9ddf95d035003:
dial tcp: lookup api.development.push.apple.com on [::1]:53: dial udp [::1]:53: socket: operation not permitted

It looks like appengine expects you to use its own urlfetch library when sending outbound requests, so I tried setting the underlying HTTPClient to use that:

client.HTTPClient = urlfetch.Client(ctx)

However, the response from the Apple server is now

@@?HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 504f5354202f332f6465766963652f393935616138373035

I believe the problem is that Apple push notifications require HTTP/2, but urlfetch only implements HTTP/1.1.

How do I solve this problem? Is there a way for an appengine app to send an HTTP/2 request?

like image 347
Jesse Beder Avatar asked Oct 11 '16 03:10

Jesse Beder


People also ask

Does Apple support web push notifications?

In Safari 16 in macOS 13 or later, Safari supports web push — push notifications that use the cross-browser Push API, Notifications API, and Service Worker standards.

Can PWA send push notifications to iOS?

Progressive Web Apps is a trend in 2019 and web push notifications for iOS are not supported right now.

Does Safari support web push notification?

The good news is that Pusher Beams now has Safari support and we've been able to hide most of the complexity for developers by: We provide a unified API for triggering notifications to Push API browsers and Safari – you can send to both with a single API call (as well as to iOS/Android)


1 Answers

This would require going through the App Engine Sockets API. The documentation states:

Libraries that can accept a net.Conn should work without modification.

You can get a net.Conn from the appengine/socket package and pass it to a lib that will accept one, but in the case of apns2 it doesn't allow you to do this. However another user has submitted a pull request to the apns2 project that adds a distinct GAEClient which can use App Engine sockets.

As of right now it looks like the commits still have not been pulled into the master branch, however you could still merge these updates manually into your own source tree as a workaround for now.

like image 97
Adam Avatar answered Oct 02 '22 09:10

Adam