Intro
1. Notifications are Robust, Secure and
highly efficient.
2. These are used to notify the user
some information without opening the app. Or, we can also use them to perform
some action/ engage the user without opening the app
3. These notifications don’t work in
Simulator,
Pre-requisite:
1. A real iOS device
2. Apple member ship account
3. And finally, payload to push the
notification
Setting up:
1. Create a XCode project.
2. Go to XCode project file, tap on
target and enable Push Notifications in the Capabilities.
3. There will 2 items that will be shown
when you enable Push Notifications in Capabilities.
a. “Add the Push Notifications feature
to your App ID.”
b. “Add the Push Notifications
entitlement to your entitlements file”
4. These 2 items should be marked. If
there is error that showing up, turn off and turn on Push Notifications.
5. Now go to Apple portal-> login ->
Select Certificates, IDs & Profiles -> Identifiers -> Select your App
ID -> Now you see Application Services.
6. In the Application services, you
will also see Push Notifications are marked as configured
7. There you can also see edit button
at the bottom. Tap on it.
8. Now you can see an option “Create an
additional certificate to use for this App ID” for both Development and
production.
9. Tap on create certificate.
10. Then you will be navigated to the
creating CSR file (Certificate Signing Request) page.
11. Just follow the steps that shown
there are follow the post
12. Tap on next, you will be taken to
the page where you have to upload the CSR file.
13. Upload and tap on Continue
14. Then you will be navigated to the
download page, where you can see the .cer file to download.
15. Download the .cer file and install
into your system.
16. Now when you go back and see, Push
Notification will be changed to “Enabled” form “Configurable”
To Receive push Notifications:
1. Now the coding part.
2. For Push Notification you need to do
following steps.
a. Ask user for permission.
b. If user accepts then register for
Push Notification.
c. Get the device token and give to the
server/ any third-party item
d. Send push Notification from the
server.
3. To ask for the user permission, Go
to AppDelegate.swift file, then go to applicationDidLaunchingWithOptions method
and write the code shown as below
a. UNUserNotificationCenter.current().delegate
= self
b. UNUserNotificationCenter.current().requestAuthorization(options:
[.alert, .sound, .badge]) { (granted, error) in
i. guard granted else { return }
ii. DispatchQueue.main.async {
1. UIApplication.shared.registerForRemoteNotifications()
iii. }
4. When the above code is placed, when
the user opens the app the permission will be asked.
5. You might get a doubt how will
server knows to send the notification to which device. Don’t worry we still not
done yet.
6. We have 2 more methods which we need
to deal with.
7. didRegisterForRemoteNotificiationsWithDevieToken
-> where you will be getting device token. Use this token and send it to the
server.
8. didFailToRegisterForRemoteNotificationsWithError
-> Where you will be getting a error
Sample JSON file for Push Notification
The json
structure should be as follows
{
“aps”: {
“alert”: “Alert message
which user sees in the push notification”
“badge”: “App badge
count”
“sound”: “default”
“mutable-content”: 1 //
This is to tell the OS, there is some more action to perform
“attachment_url”: “someurl”
// The key and url can be anything to perform some actions
}
Handling Actions in the push notifications:
We have a
method didReceive(request: withContentHandler:)
This is the
method called when we receive a notification.
1. Get the aps data from the request
2. And call this contentHandler once you’re
ready to show the notification to the user.
3. Example:
a. You can download the image from the
url which you get from the request and once the download completed, assign back
to the image to the request.
b. Assign this contentHandler to the
class Instance contentHandler
c. Call the class instance contentHandler,
to show the user push notification
Yes, now we
are getting the notification with the image or what we are expecting.
But I want
to add some actions in the Notifications, let’s see how.
Adding custom actions to the Notification:
To do that
we have to user UNUserNotificationAction.
Create a
variable as shown below:
let someAction
= UNNOtificationAction(identifier: “someAction”, title: “Some Title”, options:
[])
Now we need
to add this action to the NotificaitonCategory
let category
= UNNotificationCategory(identifier: “someCategory”, actions: [someAction], intentIdentifiers:
[], options: [])
UNNotificationCenter.current().setNotificationCategories([category])
This shows
the actions as action buttons to the Notification.
If you user
touches the button where will be these actions takes.
We have one
more method userNotificationCenter(_center: didReceive response: withCompletionHander)
Get the
action by response.actionIndentifier and perform the necessary steps.
Make sure
to write completionHander() line at the end of the method to action to take
place.
These actions
also can be set from the server too.
For Silent push notifications, you don’t have to ask for the user notification,
but rest of the steps will be same
Important Notes:
· For regular remote notification, the
maximum payload will be 4kb
· For VoIP (Voice over Internet
Protocol) notifications, the maximum payload will be 5kb
· If we are using legacy APNs binary
interface to send notifications instead of HTTP/2 request, the maximum payload
will be 2kb,
· For the iOS 8 below versions the
payload will be 256 bytes.
No comments:
Post a Comment