How push notification works for FCM/Apple push (APNs) in network level

Question:

How push notification works for FCM/Apple push (APNs) in network level?

There are so many articles describes the push notification work flow in application level. But how APN servers sends the notifications to clients in network level are not explained in many articles.

The answer is long polling connections from client devices(Mobiles, Laptops) to APN servers.

why long polling connection? if its short connections then it leads to notification delays based on polling interval. APN servers are configured with long keepalive timeout(usually this timeout is very short around 60 secs to 300 secs in normal application servers) so push notifications will deliver to client devices immediately through same long lived TCP connection once APN receives the notification from app server sidewhy polling comes here? TCP connection always disconnects if there is no internet or network change or device restarts, etc,… In this case, client device always keep trying/polling to the APN server to re-establish the connection. Once the connection established then polling doesn’t require as its long-live connections. Also client devices sends the keepalive requests to APN servers on periodic interval to extend the long connection.

why polling comes here? TCP connection always disconnects if there is no internet or network change or device restarts, etc,… In this case, client device always keep trying/polling to the APN server to re-establish the connection. Once the connection established then polling doesn’t require as its long-live connections. Also client devices sends the keepalive requests to APN servers on periodic interval to extend the long connection.

Its not some magic network L3 protocols and client devices(OSX, IOS, android) using TCP port 5223 of APN servers to connect.

Try this command /System/Library/PrivateFrameworks/ApplePushService.framework/apsctl status in mac.

This is the daemon starts during machine start and always running and maintaining the persistent unique connection(not TCP connection) to the APN server, doing all the retry/polling until the tcp connection established to APN servers successfully.

one important thing, these connections between client device and APN servers are not per apps running in device and its per device. so one connection per device will be established and APN will push all the apps notifications in this single connection.


daemon status:                                Running
startup time:                                 Oct 14, 2019 at 9:14:34 AM (4888137.61 seconds ago)
status counters last reset:                   Oct 14, 2019 at 9:14:34 AM (4888137.61 seconds ago)
certificate status:                           Provisioned, using existing certificate
app refresh activity:                         Yes


connection environment:                       production
   courier status:                            Connected
      enabled:                                Yes
      stream connected:                       Yes
      connected to service:                   Yes
         connected to server hostname:        23-courier.push.apple.com
         connected to server port:            5223
         connection time:               Dec 9, 2019 at 10:32:49 PM (1842.39 seconds ago)
         successful connections:              393
      last keepalive request time:      Dec 9, 2019 at 11:02:50 PM (42.03 seconds ago)
      last keepalive response time:     Dec 9, 2019 at 11:02:50 PM (41.66 seconds ago)
      connected users:                        0 WakeEnabled 260 501 501 WakeEnabled
         last user connection time:     Dec 9, 2019 at 10:32:52 PM (1839.29 seconds ago)
      users not connected:                    0 26 WakeEnabled
      keep alive proxying:                    Enabled
      request keep alive proxying:            Yes
         proxy request failures:              143
      interactive push:                       Yes
   application port name:                     com.apple.seld.aps
      process:                                seld[415]
      persistent connection status:           Waiting for push notifications
      persistent connection created: Oct 14, 2019 at 9:21:05 AM (4887746.79 seconds ago)
      user:                                   26
      

connection time: Dec 9, 2019 at 10:32:49 PM (1842.39 seconds ago) this is the real TCP connection time(after the last network interrupt). you can see the keepalive requests are going periodically.

persistent connection created: Oct 14, 2019 at 9:21:05 AM (4887746.79 seconds ago) this is not TCP connection and its unique persistent client identify technique, also you can see it matches the time with daemon startup time . If you want to verify just check the established TCP connection using netstat -an | grep 5223 and switch off your Wi-Fi, then run the netstat command again, you can see that tcp connection is closed.

WIFI ON
MBP2:~ saravanan$ netstat -an | grep 5223
tcp4       0      0  192.168.0.163.54179    17.57.145.69.5223      ESTABLISHED
MBP2:~ saravanan$
WIFI OFF
MBP2:~ saravanan$ netstat -an | grep 5223
MBP2:~ saravanan$

After turned off your internet connection in your mac, please check once again apple push service daemon


connection environment:                       production
   courier status:                            Will reattempt to reconnect
      enabled:                                Yes
      stream connected:                       No
      connected to service:                   No
         consecutive connection failures:     8
         last connection attempt:             Dec 9, 2019 at 11:11:16 PM (0.23 seconds ago)
         successful connections:              393
   application port name:                     com.apple.seld.aps
      process:                                seld[415]
      persistent connection status:           Not connected to service
      persistent connection created:          Oct 14, 2019 at 9:21:05 AM (4888211.51 seconds ago)
      user:                                   26

consecutive connection failures: 8 you can see it keep polling to APN servers for re-establishing the connection.

Comments

  1. That's amazing informative post, I want to add one more thing, If you want to make your web visitor to your subscriber then you should definitely check gravitec lifetime deal Best push notification for website ever.

    ReplyDelete

Post a Comment

Popular posts from this blog

Using a Linux server to route packets between two private networks

PHP Fatal error: Class 'JFactory' not found

KVM & Qemu