iOS Push Notification
As an iOS application developer according to application requirement we have to implement notifications in our app to perform some activities or let’s know app user if something interesting happen. In this article I am trying to let us familiar with iOS push notification and clear some confusions.
What is notification?
Notifications are objects that can carry some data and be broadcast to multiple receivers.
Types of notifications in iOS
A normal notification
iOS also broadcasts notifications of this type to your app while
your app is in the foreground, informing you of various system events that are happening, such as the keyboard showing or being hidden.
A local notification
This is a notification that you schedule to be delivered to your app at a specific time. Your app can receive it even if the app is in the background or not running at all, and the app is started if the notification is delivered while your app is not running. Like alarm.
A Push notification
This is a notification that is sent to an iOS device via a server. It is called a push notification because your app doesn’t have to keep polling a server for notifications.
iOS maintains a persistent connection to Apple Push Notification Services servers (APNS servers), and whenever a new push message is available, iOS will process the message and send it to the app to which the push was designated.
— — — — — — Apple Push Notification — — — — — —
In this article our main focus is to understand iOS push notification which were send by using APNS.
Short notes:
- Push notification supported from iOS version iOS 3.0, UIRemoteNotificationTypeAlert appeared in iOS 3.0
- Ways to send push notification to iOS devices are: APNS(Apple Push notification Service), firebase(google service), pusher, pubnub etc.
- Payload size: The maximum size of the payload depends on the notification you are sending:
— For regular remote notifications, the maximum size is 4KB (4096 bytes)
— For Voice over Internet Protocol (VoIP) notifications, the maximum size is 5KB (5120 bytes)
Note: If you are using the legacy APNs binary interface to send notifications instead of an HTTP/2 request, the maximum payload size is 2KB (2048 bytes) - Storage of notifications
What if when there is no network connectivity on a device when the app server sends a notification? Well, in that case, the push notification server stores the notification for a limited period of time and tries to send the notification repeatedly after a certain period of time until the device is available to receive push notifications.
The APNs stores only one notification per app. If a second notification to the same app arrives, then the first notification is replaced by the second. The notifications are deleted if the device remains offline for 28 days. - Port used (source)
APNs use four TCP ports, which are:
1. 443 : Used for communicating with APNs when communication using port 5223 is not possible.
2. 2195 : Used to send the notification to APNs.
3. 2196 : Used for APNs feedback.
4. 5223 : Used for communicating with APNs. - Notification Badge:
It means count of notification which is coming with notification payload created and send by our server not APNS.
we just have to set/update the value in our code like this
UIApplication.shared.applicationIconBadgeNumber = 0
— —— — — — — — — — — — — — — — — — — — — — — — — — —
Requirement to setting Up Your App for Push Notifications
- Enable push notification in your bundle ID
- Set up a provision profile for your app with push notifications enabled
- APNS certificate
- In your app, register the device for push notifications for your app
- Collect the device’s push notifications identifier for your app and send that to a server
How does it works ?
.pem/.p8 file
Developer have to generate one of the above file and give it to app server to connect with app.
.pem file
To generate .pem file Used to setup Apple PUSH Notification
https://stackoverflow.com/questions/21250510/generate-pem-file-used-to-setup-apple-push-notification
Some PEM file Testing tools
http://pushtry.com
https://www.pushwatch.com/apns/
http://apns-gcm.bryantan.info
Pusher
.p8 file (key)
Add key on developer.apple.com account and download .p8 file
iOS code
Something to clear you and remove misunderstanding about Push notification
- We can send notification with only text, media notification, grouped notification and notification with action buttons and categories.
- For testing & debugging push notification on device(Can’t be test on simulator) backend(app server) must have setup development setup(i.e. development .pem in use) otherwise distribution .pem file.
- Device Token:
a. An app-specific device token is globally unique and identifies one app-device combination.
b. On iOS 7, Apple has gone one step further and made sure that device tokens are now different in every single app install. This helps further protect users’ privacy by removing another phone-level identifier.".
c. The token is only valid as long as the user does not reinstall the app or restore the device.
d. Never cache device tokens in your app; instead, get them from the system when you need them.
e. Development and production build device token may be different.
To learn how to implement push notification in iOS and its code go here or if you want to refer document, this is the best
Thank you, Happy coding!