What we do is create an array of the devices, open the connection, push to them all, then close.
HTH
Thanks for reaching out! Please do share what you use the library for (privately?). I love to hear of the cases it is used for.
> should i keep the ApnsService for all the
> time for our application after it has been built?
As Olivier stated, you should keep ApnsService alive for the entire duration of the application lifecycle, especially if you keep sending a stream of notifications. This is beneficial because ApnsService handles connection management and Apple requests that the connections stay alive as much as possible. Creating ApnsService for each message (or even batch) if it happens multiple times a second might trigger a DoS flag and Apple may ban the connection.
That being said, if your use case is to broadcast notifications once an hour on the hour for example, you can get away for creating an ApnsService for each batch.
> Another question is about socket close, do i need to call any method
> after sending push notifications? I see there is a stop method in
> ApnsService
The `stop()` method should be called at the end of the life-cycle of the ApnsService. Typically that is the end of application lifecycle.
> The sample code in the home page is very simple, so i'm
> not sure is that OK to be used in a real project with many iOS
> devices?
=) Nope. It's that simple! The only two possible addition I can think about are: using `asQueued()` and `asPooled()` to establish queued notification service (if you scale and have some throughput issues), and using delegates (especially if you want to detect notifications failures).
Hope this answers your questions.
Regards,
Mahmood
I didn't think this is what you told me before.
I'm sending a single notification perhaps once or twice a day, so I was opening a connection sending all the notifications to all the devices, then closing. It's better to leave the connection open to apple for hours or perhaps days on end with no activity?
> Wait, so you're saying I should open the connection as soon as the server runs and close it when it quits?
> I didn't think this is what you told me before.
>
> I'm sending a single notification perhaps once or twice a day, so I was opening a connection sending all the notifications to all the devices, then closing. It's better to leave the connection open to apple for hours or perhaps days on end with no activity?
I recall discussing this privately, and given your use case I think it is better to re-establish connections for each batch of notifications so I didn't push it as hard. I attempted to cover your case in my email:
> > That being said, if your use case is to broadcast notifications once an hour on the hour for example, you can get away for creating an ApnsService for each batch.
In such cases, keeping the connections long-lived comes with its own set of problems (e.g. half-dead connections, stale connections) that affects the reliability of the service. So it is even better to establish a new connection per batch in this case.
Basically, it comes down to the perception of a Denial of Service attack. If you don't create multiple requests a second to Apple, you should be fine. However, as a rule of thumb, default to long-lived connection (i.e. ApnsService) unless your case is special.
Regards,
Mahmood