Should I keep the ApnsService for all the time?

620 views
Skip to first unread message

ramon

unread,
Jul 27, 2011, 1:54:38 AM7/27/11
to Java client for Apple Push Notification service (APNs)
Hi

We're going to use this great lib in a real project, i have couples
question actually, first, should i keep the ApnsService for all the
time for our application after it has been built? For example, use a
static variable. I remember that Apple suggest to keep the connection.

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 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?

Thanks
Ramon

Andrew Kinnie

unread,
Jul 27, 2011, 8:18:33 AM7/27/11
to java-apn...@googlegroups.com
FWIW, I think Apple wants you to close the connection at the end of a batch of pushes.

What we do is create an array of the devices, open the connection, push to them all, then close.

HTH

Olivier Costet

unread,
Jul 27, 2011, 8:29:45 AM7/27/11
to java-apn...@googlegroups.com
I beg to differ with Andrew. Apple wants you to open as few connections
as possible (because opening the connection, esp. since it is SSL, is
the most expensive part).

If you have a strong indication that you will not use the connection for
a sustained period of time, then you might in theory want to close it,
otherwise you had better leave it open.

But that's a moot point, since the APNS lib is supposed to handle those
issues for you so that you don't have to care about them. I'm pretty
certain you should start an ApnsService at the begin of your
application's (or module's) life cycle, and keep it until the end. You
should probably NOT create a new service every time you have to execute
a batch, save for exceptional circumstances. Note that the service's
life cycle is radically different from the underlying connections' life
cycle.

OC.
--
The way some people find fault, you'd think there was some kind of reward.

Mahmood Ali

unread,
Jul 27, 2011, 8:51:01 AM7/27/11
to java-apn...@googlegroups.com
Greetings Ramon,

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

Andrew Kinnie

unread,
Jul 27, 2011, 9:16:51 AM7/27/11
to java-apn...@googlegroups.com
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?

Mahmood Ali

unread,
Jul 27, 2011, 9:34:18 AM7/27/11
to java-apn...@googlegroups.com
Greetings,

> 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

Andrew Kinnie

unread,
Jul 27, 2011, 9:44:46 AM7/27/11
to java-apn...@googlegroups.com
OK, well good to know this isn't further evidence that I am going insane. :-)
Reply all
Reply to author
Forward
0 new messages