Ports number needed to open to receive push notification from APNS - ios

I would like to understand the cycle of APNS with my web server to receive push notification. I open the port 2195 from my webserver in the (out side), do I need another port to open it in my sever to receive the push notification??? because still not received it.
please help!

Yes, port 2195 open. With APNS, you only send out things. Apple doesn't send you anything back unless you're using the feedback gateway, which I guess is not your case...
Your server do not receive push notifications. It only sends requests to Apple, which then sends notifications to iOS devices or MacOS desktops.
If your device did not receive and your server code is being able to establish a connection with apple, much probably your push cert is not uploaded to your server or it has permission problems or it was not properly generated at your iOS dev website.
p.s. if you're sending OUT things, your port logically should be open from inside (outbound) in your server

Related

iOS APNS: Can two notification provider servers have same device token

The apple documentation mentions there is no restriction on multiple provider notifications servers to send push notification to iOS devices.
Is it possible, the same device token can be used in two different provider servers deployed in 2 different network,to send push notifications to same app at any moment.
Will Apple APNS, allow 2 different provider servers, use same device key to send notification from each to same app in one device?
Yes you can have multiple Gateways (SNS, Urban Airship, PushIO, etc.) connected to one platform provider (APNS, GCM, Amazon, etc.), the most important thing is the token/registration ID (specific to a device and an Application) and the certificate if you are using APNS.
Many people ask how the feedback sent by the platform provider is handled in this case.
Since feedback is idempotent, it should work without problem for different gateways; tokens saved on each Gateway can be handled in the backend application. If for example APNS sends a feedback to SNS saying that an endpoint is no longer valid (maybe because the user has uninstalled the Application), the token can directly be updated/removed from all the other Gateways. You can also leave it as it is and wait for the feedback sent by APNS to other gateways when they send notifications to the endpoint, and then update your endpoints list accordingly in those gateways.
The most important thing to note here is that feedback is idempotent, if SNS sends a push notification to an endpoint and receive a feedback, another gateway that sends a push notification to the same token will receive the same feedback.
This is my experience while working with APNS, this is like a checklist, before you start testing notification in any environment,
Certificates will decide server can connect to APN or not.
According to the certificate you have for ex. dev certificate or production
certificate, point to respective APNS sandbox.
Production Sandbox:-
Hostname: gateway.push.apple.com ,
Port: 2195
Development environment:-
Hostname: gateway.sandbox.push.apple.com ,
Port: 2195
There is no restriction that production certificate can be used in only one server or in only one network. The same production certificate can be used in multiple boxes or multiple networks to send notification to same app on a device.
Most important is checking Firewall is blocking connectivity from your network to APNS Dev or Production. So do a telnet to both production and dev environment APNS. The connection should not be blocked.
If firewall is blocking connectivity to APNS, the server code will throw "Connection Refused" or "Connection Closed" exception while sending push notification.
This is the most weird behavior I saw with APNS and certificate, because of which I lost 2-3 days. I experienced below depending upon the case,
Server is pointing to APNS Dev environment, certificate is
"Production", while sending notification to APNS, SOMETIMES
server will throw "Socket Closed Exception".
Server is pointing to APNS Dev environment, certificate is "Production", while sending notification to APNS, server logs says
notification sent to APNS, but device will not receive the
notification.
:) If you have done wrong configurations, you should be lucky to get "Socket Closed Exception".
If all the configurations are correct(for ex credentials, firewall), correct certificate and pointing to right APNS environment, the device will defiantly get a push notification. Push notification will work like a charm. It will be instant .
Hope this checklist will help in your implementation and sanity checks.

Can we send push notification to APNs from iOS device?

I want to send push notification from a iOS device to another iOS device without using backend server. Is it possible for an iOS device to act like a server and send push notification to APNs server?.
Thanks in advance.
Theoretically you can send Apple Push Notifications from a device directly to another device. All you need are the push certificate of the app, the device token of the device you are sending the notification to, and code that establishes a secure TLS connection to the APNS servers.
However, there are several practical problems that make the use of a server almost mandatory :
You need a single place where all the device tokens of all the devices that installed your app will be sent to and persisted in. The best such place would be a server. Without a server, how would device A send its device token to other devices that want to send it push notifications?
Apple require that you keep connections with the APNS server open for as long as possible and use the same connection for sending many notifications. If you open a connection to APNS server on your device, it will probably be short lived (since devices switch networks frequently, and don't stay connected to the internet all the time). Therefore, if you try to send many notifications frequently, and each time use a new connection to APNS, you will probably be banned (since Apple would treat this as DDoS attack).
If you store the push certificate in each device that installs your app (to allow it to send push notifications to other devices directly), aside from the security issue of storing the certificate in many places, you'll have to publish a new version of your app each time the push certificate expires (once a year), and push notifications would stop working for users who don't upgrade to the new version.
Try NWPusher.
It has an iOS framework for sending pushes and has an iOS demo application that sends push notifications from iOS to iOS.
You also need to consider Server costs (other than maintenance and development time if you code your own server).
By sending the push directly from the app device:
- you obtain a much better scalability (since you don't have to centralize everything on your server)
- you don't have to pay for server cost or other service's cost
You can use for iOS:
- https://github.com/noodlewerk/NWPusher Pusher
And for Android:
- Send push notification GCM by java

How do iOS Push Notifications work?

How do iOS "push" notifications get delivered to a particular device without that device needing to poll a server?
For example, let's say I have received a new message on Facebook. Facebook notifies Apple that my device should receive a notification as such. But how does Apple know which device/IP to push the message to?
Each device can be updated with data using their own unique device tokens. This picture explains everything . .
It was too much for me to put in a comment so.
From the documentation.
Apple Push Notification service (APNs) propagates push notifications to devices having applications registered to receive those notifications. Each device establishes an accredited and encrypted IP connection with the service and receives notifications over this persistent connection. Providers connect with APNs through a persistent and secure channel while monitoring incoming data intended for their client applications. When new data for an application arrives, the provider prepares and sends a notification through the channel to APNs, which pushes the notification to the target device..
I suggest reading the documentation for more information and how to use and configure. It's all there.
Push Notifications
I created an infographic to explain the workflow of push notifications. Hope this is helpful.
Device does not keep polling the server for the push notifications.
To keep it simple, consider an iPhone is connected to internet. On connecting to internet iPhone establishes connection to Apple Push Notifications server this connection is open connection which means data can be thrown to iPhone from server the moment data arrives to server.
Apple does not use HTTP protocol for Push notifications but if you understand HTTP Protocol its almost a similar methodology.
http://en.wikipedia.org/wiki/Push_technology#HTTP_server_push
There is a really nice exaplanation of push notifications in this article.
In iOS, apps can’t do a lot in the background. Apps are only allowed to do limited set of activities so battery life is conserved.
But what if something interesting happens and you wish to let the user know about this, even if they’re not currently using your app?

iOS Push Notification service batch size

We are trying to send a notification to ~500,000 iOS devices. We have currently set the batch size to 200 and are noticing that Apple refuses our connection after 300k messages or so. We suspect that Apple interprets our connection/tear-downs to be a DOS attack. What is a good batch size for us to use? Also, any tips on sending notifications for such a large number of devices?
Your push notification server must maintain a persistent connection to Apple's socket stream push server without disconnecting too often.
I recommend writing your push server using Node.js, it was designed for this stuff.
However there are a few things that can also cause Apple to disconnect your push server.
With Push Notification, there are two types of certificates - development and production.
An app signed with a development certificate will generate a development push token whereas a an app signed with a production certificate will generate a different production push token even on the same device.
Your server must make sure it does not send a development token to a production socket stream connection to Apple's push server.
Mixing the token and environment will cause Apple's push server to disconnect your push server.
How you separate your push token is something that you need to build into your server.
Hope that helps.

How to debug if the message went through Apple Push Notification Service?

I am experimenting with Apple Push Notification Service with node.js as server. So far I've got it to work and when it works, it works as intended.
However, sometimes the message doesn't arrive. I am not sure if this is natural (maybe I have bad connection and the message is not coming through because of that), or if it's a bug in my code. The situation is really random. Sometimes it comes through and sometimes it doesn't, even though I didn't change the code or anything.
I am using node-apn (https://github.com/argon/node-apn), and according to the debug console, it's being registered as successfully going through--at least from the server side. Here's the debug message:
apn Initialising connection +19s
apn Initialising module +1ms
apn Connection established +383ms
apn Sending notification +0ms
apn Socket drained +1ms
apn Socket writeable +0ms
This message appears both when the message goes through and when it doesn't go through. So I guess node-apn itself is doing its job all right. Which means it's either:
There's something happening between my server and apple push server
There's something happening between apple push server and my iOS device
But I don't know how to find out what's happening. So what I am trying to find is a solution to:
Find out if my push notification is being registered by APNS
Find out if APNS is sending out all the notifications to my iDevice
Anyone know how to do this? Thank you so much!
There is a well written technical note from Apple about the different sources of problems with the push notification services:
http://developer.apple.com/library/ios/#technotes/tn2265/_index.html
Use tcpdump on ports 2195, 2196 and 5223, as data pass over them:
http://support.apple.com/en-us/HT202944
Use Wireshark to read the dump files:
http://redmine.streamco.org/projects/smartswitch-public/wiki/Debug_VoIP_call_with_Wireshark

Resources