How to configure the iOS app to receive the push notification?
This article is the second part of the push notification series. If you haven’t seen what is the first part, it’s all about what is push notification and how it’s working in iOS.
Before start 🚦
In order to set up the push notification, you should have the apple developer account which is not free. please find the more details apple developer link.
Introduction
This article is going to explain how the iOS app can be configured to receive the remote push notification. with a few steps your iOS app can be able to receive and display the push notification.
Lets start
Step 1
First, create the new iOS project and select your team for your project, If you purchase the account after creating the project, please head down to Xcode preference and download your manual profile to sync with the developer account.
Step 2
After you created the app and assigned the team, now we need to enable the push notification service to the project. To do so, first, click the app target and then head down to the Signing and Capabilities section and click the plus button and search for the push notifications and add it.
Step 3
Now our project is ready in terms of setup level, time for do some coding to configure and receive the push notification.
Go to AppDelegate.swift file and add a new function called registerForNotification, this function is going to be used to request and authorise the notification service. after authorisation is done, we just need to handle success and failure cases in the main thread. In the success case, we are calling the registerForRemoteNotifications function from the UIApplication Singleton class.
Once the above steps are completed, we need to add another function in the AppDelegate, this function is provided by apple for us to get the Device Token. Go the AppDelegate and add this function func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data).
Based on your system design,
You may require your implementation to send the push notification for iOS
Once the device token is received we need to call our database API to pass this device token, so the backend can store this information in the backend and later when they want to send the push notification then can get it.
To send the notification, we still need to call the Apple API with the device token and notification details. In the future, I’ll write an article for this, since this article is only focused on the iOS implementation.
You may require a third-party push notification service to send out the push notification
If the third-party push notification services like Firebase or OneSignal can be used. Based on their requirement we need to pass this device token so the third party will provide another token which was later used to send the push notification.
Once we received the third-party token, we also need to pass that token to our backend so it stores this value in the database and when they wanted to send the push notification they will use this token to communicate with third-party push services to send the notification. A third-party notification service will communicate with Apple to do the message delivery.
But in this article, we are not going to do both steps, since we don’t have our backend. So we will use another alternative way to send the push notification. Please wait for it, you will receive another article for this.
That’s it, we have reached the end of the article, connect your device and install the app you will be asked for permission. Once you give the accepted permission to get the Xcode and see it, you will be able to see the device token in the output console section.
Important points ⚠️
Please note that you should run the app on a real device otherwise you won’t get the Device Token. I’ll write another article on how can we test the push notification in the simulator.
If you are not getting the device Id, as a debug (🐛) please check these
- Make sure you have selected the correct team
- Double confirm you have the latest manual profiles.
- Make sure you have added the Push notification Signing and Capabilities
- You might be running it on a simulator, instead of a real device.
Conclusion
This article explains how the iOS app can configure the push notification. Where to go from here, here is the next part of this push notification series which is going to explain how can we send out the push notification without backend involvement(But this can be only used for testing purposes if you are planning to release your app to market you should try to implement the backend service to handle this).
I hope you guys learned about how the iOS app can configure the push notification service. Please give your comments so that I can improve it. Don’t forget to give me some claps 👏 that’s the energy drink for writing more articles.
Here is the article’s project file can be found on GitHub
Enjoy the day…. See you in the next article.