What is the right way to use PushSharp?
Asked Answered
V

1

9

I use PushSharp to send notifications for a few Apps. PushSharp is great it really simplifies the work with push services, and I wonder what is the right way to work with it? I haven't found examples/ explanations about that.

Now, when I have a message to send , I ...

  1. create a PushSharp object
  2. do a PushService.QueueNotification() for all devices
  3. do a PushService.StopAllServices to send all queued messages
  4. exits the method (and kill the PushService object).

Should I work this way, or keep this PushService object alive and call its methods when needed?

How should I use a PushService object to get the unregistered device ids? with a dedicated instance?

Any suggestion would be appreciated.

Valeric answered 28/3, 2013 at 14:7 Comment(0)
W
8

This is a question which frequently comes up.

The answer isn't necessarily one way or the other, but it depends on your situation. In most cases it would be absolutely fine to just create a PushBroker instance whenever you need it, since most platforms use HTTP based protocols for sending notifications. In the case of Apple, they state in their documentation that you should keep your connection to APNS open in order to minimize overhead of opening and closing secure connections.

However, in practice I think this means that they don't want you connecting and disconnecting VERY frequently (eg: they don't want you creating a new connection for every message you send). In reality, if you're sending batches of notifications every so often (let's say every 15 minutes or every hour) they probably won't have a problem with you opening a new connection for each batch and then closing it when done.

I've never heard of anyone being blocked from Apple's APNS servers for doing this. In fact in the very early days of working with push notifications, I had a bug that caused a new apns connection to be created for each notification. I sent thousands of notifications a day like this and never heard anything about it from Apple (eventually I identified it as a bug and fixed it of course).

As for collecting feedback, by default the ApplePushService will poll the feedback servers after 10 seconds of starting, and then every 10 minutes thereafter. If you want to disable this from happening you can simply set the ApplePushChannelSettings.FeedbackIntervalMinutes to <= 0. You can then use the FeedbackService class to poll for feedback whenever you need to, manually.

Whoosh answered 23/5, 2013 at 12:24 Comment(6)
If I create new instance of PushBroker each time and wire its events, won't it leak memory? How do I know when to unwire the events?Certifiable
Redth, this was an invaluable comment. Particularly about whether connections need to stay open nearly perpetually, which is the feeling I got previously.Pammy
@Nicholas glad that helped. Like I said, Apple's documentation requests that you keep a connection open to them. In practice, this is probably not needed if you're sending notifications in batches, and I've never heard of Apple banning anyone for doing this.Whoosh
@Certifiable this is a bit late, but you can unwire events properly as to not leak memory... Don't see why this should be a problem..Whoosh
Thanks Redth! Maybe you would be so kind to check out this related question I just posted: #20980758.Pammy
Just to clarify about opening/closing connections rapidly - From the Apple docs: "APNs treats rapid connection and disconnection as a denial-of-service attack". So yeah, you'd have to open a lot of connections very frequently and over a decent period of time to get shut off i'd imagine. That said, better to play it safe and keep connection open for as long as is reasonably possible.Benzene

© 2022 - 2024 — McMap. All rights reserved.