Ilker Furkan Kahyalar

Ilker Furkan Kahyalar

Software Engineer


Home

Podcast

Projects

References

Contact


Subscribe


Essentials of iOS | Push Notification 101

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.


Getting started

Screen Shot 2019-08-07 at 21.21.28

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.

Generating certificates

Screenshot 2019-08-07 at 21.59.21

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.

Screenshot 2019-08-07 at 22.10.07

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”.

Screenshot 2019-08-07 at 22.23.11

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.

Screenshot 2019-08-07 at 22.27.21


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.

import UserNotifications

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.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert]) { granted, _ in
        guard granted else { return }
        DispatchQueue.main.async {
          application.registerForRemoteNotifications()
        }
    }
    return true
}

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”.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
        completionHandler([.alert, .badge, .sound])
}

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.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.reduce("") { $0 + String(format: "%02x", $1) }
    print("Token: \(token)")
}

Note that 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.


Testing

We are ready to start our project, but only one more thing left. Open the downloaded testing application (PushNotifications). 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.

6


Closing

I hope you liked what you heard 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? 😎

Buy Me A Coffee

Until next one,
Furkan