I have a decent understanding of networking, but this question popped up in my head randomly and I can't figure it out. For example, if I have an iPhone that is used on a 3G network but is also connected to my home wifi throughout the day, how does an application know whether to send data (whether it be a push notification or something else) to my phone via my cellular IP, or my Wifi IP address? Wouldn't both the cellular network and my home network associate my phone with themselves?
In this scenario, your phone has at least two network interfaces: WLAN and 3G. Each interface has his own IP address and mask. They usually install as two routes on the phones routing table, the locally connected network and a default gateway.
Let's see a simplified example:
Interface: Wifi IP/mask: 192.168.10.10 255.255.255.0 Default gateway: 192.168.10.1 (your home router)
Interface: 3G IP/mask: 10.10.10.10 255.255.255.0 Default gateway: 10.10.10.1 (your next hop to ISP)
Resulting routing table:
Destination Interface Metric
192.168.10.0/24 Wifi 0
0.0.0.0/0 Wifi 20
10.10.10.0 3G 0
0.0.0.0/0 3G 30
So, what happens when your phone wants to send a packet? It checks the destination IP address vs its own routing table and decides which interface to send it to.
- If you send a packet to 192.168.10.49 it matchs first line and sends it through the Wifi interface.
- If you send a packet to 10.10.10.50 it matches the third line and sends it through the 3G interface.
- If you send a a packet to 18.104.22.168 (Google DNS) there are two matches. Which of the two lines has precedence? The one with the lowest metric. In this case it's the second line and your packet to internet will go through the Wifi interface.
In personal devices, this metric is automatic. Your iphone is smart enough to set a lower metric to Wifi.
The other answer deals well with how the outbound traffic from your device decides which network connection to use. However it seems your question is asking more about how data inbound to your phone (such as push notifications) decides which network to use.
There are a number of ways this is done, but if it is truly a service that works on both WiFi and cellular, then odds are it periodically "phones home" or establishes and maintains a connection to a server.
When the device/app is checking in or "phoning home," it can collect any messages that are stored for it. In this case, it works much like email and all return traffic from the server will go back to whichever IP address was used to send the request.
Another option is that the device/app creates a connection to the server and keeps it open so that the server can use it to immediately push the notification to the client whenever it receives one. If your device connects/disconnects, the device/app will re-establish the connection to the server. Again, since the device/app will originate this traffic, all return traffic will go to the source IP whether that is on your WiFi network or your cellular network.
There's only one default route active, so if you have both enable, it will choose one. For example, if both are reachable, then use wifi, if wifi is unreachable use 3G. The routes are basically balanced with a preference.