timehop / apns

A Go package to interface with the Apple Push Notification Service
https://godoc.org/github.com/timehop/apns
MIT License
185 stars 47 forks source link

Using the Feedback Service #61

Open codepushr opened 8 years ago

codepushr commented 8 years ago

Hey guys, thanks for this awesome piece of work, this is definitely one of the best APNS packages out there for go.

I'm not really sure if I'm using the feedback service correctly because it sort of never prints any errors, even if I test it with broken/custom device tokens. I would love to detect expired tokens and delete them to keep my data clean and only send pushes to valid devices.

This is how I use it:

func (n *NotificationAgent) sendPushMessages(devices []model.Device,
    t NotificationType, args NotificationArguments, payload Payload) {

    for _, device := range devices {
        if *device.Type == model.DeviceTypeIos {
            p := apns.NewPayload()
            p.APS.Alert.Body = n.getLocalizedPushText(t, args)
            p.APS.Badge.Set(1)
            p.SetCustomValue("type", t)
            p.SetCustomValue("data", payload)

            m := apns.NewNotification()
            m.Payload = p
            m.DeviceToken = *device.Token
            m.Priority = apns.PriorityImmediate

            if e := n.apns.Send(m); e != nil {
                log.Println("Error: " + e.Error())
            }
        }
    }
    for ft := range n.apnsf.Receive() {
        log.Println("Feedback for token:" + ft.DeviceToken)
    }
}

As mentioned above I tried to use random device tokens in the belief that it would print something, but nothing happens. I'm not even sure if I'm calling the feedback service correctly because the sending is asynchronous and maybe my feedback loop is called too early... if so, where and how should I call it?

Oh and, yes n.apnsf is correctly initialized and throws no errors. I just wanted to keep the code small.

Thanks, codingrogue

nathany commented 8 years ago

I haven't used the feedback service yet, though I know it can take some time to get feedback.

Maybe @tylrtrmbl or @bdotdub can chime in.

taylortrimble commented 8 years ago

There's a few things going on here.

...even if I test it with broken/custom device tokens

The Feedback service does not report missing / invalid / "broken" tokens. It only reports valid device tokens that belong to devices that have repeated reported failed delivery attempts. If you keep sending notifications to these offline devices, Apple may punish your provider; but there's nothing "expired" or "invalid" about tokens you receive from the feedback service.

I would love to detect expired tokens and delete them to keep my data clean and only send pushes to valid devices.

Yes, implement that! For testing, the usual way you trigger a removed token is to uninstall your development app from your test device. The token for that device will appear on the Sandbox APNS feedback channel. However, beware. The uninstall is only reported to APNS if you have another app using the Sandbox gateway on that same device. Otherwise, the OS simply abruptly closes its connection to Sandbox APNS and does not report the uninstall. Don't ask me why, Apple was simply very very simplistic with their implementation of all of APNS. :smile: To get around this, I made an app I called "SandAPNS" that registers for push notifications on the sandbox and does nothing else. I leave it installed on my phone.

Best of luck!

codepushr commented 8 years ago

@tylrtrmbl Wow thanks for the insight! I will test it by removing my app and check if I get responses from the feedback channel. I didn't know I also need another app listening via APNS sandbox lol, good tip!

@nathany Thanks for tuning in!

codepushr commented 8 years ago

@tylrtrmbl I just read the APNS documentation... So basically what I have to do is:

  1. Check the feedback service daily (use a job or something)
  2. Compare the timestamps and check if a token has reregistered in the meantime 2a) If the feedback timestamp is still up to date mark the token as expired (no further pushes) 2b) If the feedback timestamp is outdated do nothing (gets cleared after fetching)

I just tried uninstalling my app and sending another push to it, afterwards I fetched the feedback service but nothing came. Does it take some time for APNS to add it into the feedback queue?

nathany commented 8 years ago

I'm looking forward for this whole feedback service mess to go away with #57. Whenever we can get some documentation from Apple.

taylortrimble commented 8 years ago

Your steps are correct! It does take a while for APNS to put your token on the queue- and potentially maybe several "failed deliveries" if the documentation is to be believed.

My exhaustive list of things to double check:

And hopefully you see your token! I'm curious which steps of these are necessary- how long is it necessary to wait? Are sending failed messages required?

I too look forward to these questions getting cleared up with the HTTP/2 API. :smile:

codepushr commented 8 years ago

@tylrtrmbl Thanks :smile: One last question: Do I need to recreate the Feedback "client" every time I want to Receive() because what I'm doing with the regular APNS client is: I'm initialising it once and using it all the time. Right now I'm doing the same with the Feedback object but maybe it's not working the same way...

codepushr commented 8 years ago

Ok I checked the code and figured it out :) Still no feedback though, if I find something I'll let you know, but for now I think all my questions are answered :)

nathany commented 8 years ago

I'd like to leave this open as documentation (or an issue that should be documented).

Kinghack commented 7 years ago

hi, I found this issue by searching apn's feedback problem. I had tested feedback service about 3 months ago with other apns library which works fine. But I did not do a production deploy. Today I am trying to do production deploy of feedback check. I found apple's feedback service would close the connection immediately. So I am wondering that was this be normal when no fail token at all? Since I have tried to send some msgs with uninstalled token, I don't think there would exist no fail token.. Thanks.

codepushr commented 7 years ago

@Kinghack For me the feedback never worked in production with this library! I had tons of expired tokens but never got any feedback. I eventually moved to GCM which I use for both Android and iOS.

nathany commented 7 years ago

Apple has since changed their API to be based on HTTP/2 without the need for a feedback service.

You may want to check out one of these libraries:

Kinghack commented 7 years ago

@codepushr @nathany Ok. I will check the new APNS service. Thanks.