This article is a part of the series “Essentials of iOS” which contains researches and new technologies that I implemented during while I was creating the TalTech App.
Push Notification is a way of communication between users and the app itself. Thanks to Push Notifications, as a developer, you can reach out to your users and interact with them. For example, social media platforms and messaging applications are the most frequent apps which rely on these notifications. We are all the time living together with these notifications due to social media and messaging applications, but now, it is time for creating for ours.
Steps to do
1. Make sure that you have a paid Apple Developer account.
2. Go to your Apple Developer portal, and generate a key for APNs. Note that, one key is used for all of your apps. Once you generated that key, download and save somewhere you can found easily.
3. Create the iOS app and turn on Push Notifications capability.
4. Implement the protocols in code and prepare the iOS app.
5. Download the app for testing from here.
6. Configure the testing application with the credentials you have created.
7. Test the results.
Apple’s ecosystem is very security-oriented. Due to this reason, every online functionality that you might want to implement requires to authenticate by Apple. For this reason, we need to generate certificates or keys to develop and use these technologies.
Go to “Certificates, Identifiers & Profiles” section in the Apple Developer Portal, and select “Keys”. Press the “+” button to add your new key.
When you created your key, download the key file and save somewhere you can easily find. While you are in Apple Developer Portal, make sure that you have wrote down your Team ID as well.
Creating the iOS app
Open your Xcode and create a Single Page Application. When you created it, make sure that you wrote down the Bundle Identifier.
When you got your Bundle Identifier, click to Capabilities pane activate Push Notifications section. If everything goes perfect, you should see all the steps are ticked and “YourAppName.entitlements” file has been created.
Implementing the protocols
For the implementation of notifications, you need to import UserNotifications class and you need to ask permission from the user. Open your AppDelegate.swift file and import the class.
Permissions can be changed any time by the user. That’s why we need to check to the status of permissions each time when the app launches. Go to didFinishLaunchingWithOptions function and implement the permission request function.
As a default behavior, your push notifications are not visible when the app is on the foreground. To solve this issue, you need to implement UserNotifications class’ delegate function with willPresent.
Besides these, each device has its unique identifier for push notifications. So in this next step, we need to get that device-specific identifier and printed out for testing purposes. Ideally, you need to store these identifiers and use it when necessary.
NB! This function will be fired up each time when the app launches. Because as I mentioned above, we need to check the status of permissions each time when the app launched.
We are ready to start our project, but only one more thing left. Open the downloaded testing application. Let’s configure the application and test our iOS app. First, select Token in the Authentication pane and insert your .p8 key. Then, fill out the Key ID.
Hint: .p8 keys are named like AuthKey_XXXXXXXXX.p8 and your Key ID is that XXXXXXXXX part.
In the Body section, the first thing we need to fill is Bundle Identifier. Next one is your Device ID, which we obtained in the last step.
Finally, press Send to test your push notification. If everything is correct until now, you should see your push notification with your app’s icon and “Hello” text with it.
I hope you liked what you read and I am really looking forward to seeing you in the next one! If you don’t want to miss any content, you can subscribe to the website and I will let you know whenever something new going on here!
Also you can reach out to me from the “Contact” section. Feel free to share your thoughs or suggest any ideas. I am reading all the messages and I will reply yours as soon as possible!
As you might know me, I like coffee and high-quality content. If you want to support me and my effort, why not a little caffeine boost ha? 😎
Until next one,