Notifications with Code
Start course
1h 25m

This course explores how to enable push notifications in your iOS apps. We're going to walk you through how to do that using an Instagram clone that we created in a different course.

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills


To get the most out of this course, you should have some basic knowledge of iOS.


Hi. Within this lecture, we're going to see how we can send a push notification from Xcode. Now, we know how to send one from this dashboard, right? Manually. But we're going to do it automatically, so that we can learn how to send it through Xcode with code so that we can send a push notification when somebody likes some post. So, let me come over here to documentation and show you how it's done. So, it's sending push messages, but it shows me how we can send it to the dashboard, right? So, we have already done this, so we don't need this documentation at this point. What we need is iOS native documentation so that we can see all the SDK related codes. So, let me come over here. And as you can see, there is a section called how do I send a notification to a single user? So, that's what we need. We're going to send a notification to only one user. So, whenever Kirk likes the post of James or James likes the Kirk post of Kirk, then the other one will get a push notification, okay? So, that's what we need. And don't worry if you cannot find this documentation, I'm going to show you how to do it anyway. But it's a nice thing to read the documentation and see the other modules as well. So, what we're going to do, we're going to take this player ID, and we're going to send only one notification to that specific player ID, okay? As you can see, it says that you have to use this method called include player IDs, and you have to include that specific player ID that you want. So, if you come over here to User-User Messages or transactional messages guide, you can find what you're looking for. So, I'm going to go over to User-User Messages. So, there are two steps, actually. First, we have to gather the player ID, then we have to send the message to that player ID. And we know how to get this player ID right now from dashboard, but that's not what we want. We have to get that from the Xcode itself so that we wouldn't go over here every time someone is trying to send a push notification, okay? So, I'm going to open this mobile SDK reference, and I'm going into the iOS Native SDK so that we can learn how to gather these player IDs and how to send a push notification using that player ID. So, this is iOS Native SDK documentation, okay? So, what I'm looking for is to send a push notification, and I believe it's under here somewhere, so let me search for it. So, let me come over here. Yeap, this is a sending notification, so if I click on that it will take me to this post notification method. And that is exactly what we are looking for, post notification, okay? So, it's really easy method, so you can easily send a push notification. As you can see, it says that, you have to call OneSignal.postnotification, and then you create the content and then you include the player IDs that you want. So, I'm going to copy this one, I'm going to come over here, okay? So, let me come over here to my iPhone XR, because I'm sending this not to my iPhone XR, but I'm just sending this to my iPhone, but I can use a simulator to send it to my iPhone, okay? Even though simulators do not actually support push notifications, we can still send one, right? We are not receiving push notifications in the simulators, but we can send some push notifications using simulators So, what I'm about to do, I'm just going to put that code that we have copied into my Xcode, I'm going to run this using my simulator. So, I'm going to delete the existing InstaCloneFirebase from here, so that it would ask me if I want to receive notifications and I will allow it, even though I won't be able to receive one I will be able to send one. So, let me come over here to my FeedViewController because that's the View Controller that we will see when it first loads. So, under viewDidLoad, I'm going to try this code, okay? So, I'm going to say post notification or push notification as a command. So, let me say push notification, and I'm going to paste it in. So, in order to do that, I'm going to import the One Signal over here as well. So, once it's done, let me come over here and see... It's done. Now, let me paste this code in. So, in this code, I have to specify the content. And test message is okay for me right now, but I'm going to change this so that I can try to send my push notification to my iPhone, okay? So, let me paste this in. So, that will send a push notification called test message to my iPhone than my iPhone XR loads. So, let me run this and also at the same time, I have to open my real iPhone as well, so that I can see if I get the message or not. And once we open the simulator, it will ask us to give permission for push notifications. I'm going to allow it, but we won't be able to receive any notifications. And I believe we're logged in with Kirk, but I'm going to make sure that we are logged in with Kirk anyway, so I'm going to say and give some passwords that we have created before, I believe it was 123456, and sign in. Here we go, now we are logged in with Kirk. Now, if I come over here and refresh this page, I will see my simulator over here, subscribe. But as you can see, it says that even though it has a player ID, it says that iOS simulator unsupported. So, we cannot receive any notification using this simulator. But we can send one, as I said before, so what I'm going to do, I'm going to open this, okay? So, let me run this one more time to see if we are really logged in with Kirk, and it will just run this One Signal code, okay? And once it's done, I will receive a push notification in my real iPhone. And here you go, now I received that push notification. As you can see, if I tap on it, it will direct me to my app. So, here you go, that's how we actually send push notifications inside of Xcode. So, what we have to do right now is to find out how we can gather the player IDs so that we can send the push notification whenever we want to whichever device we want. So, let's do that in the next lecture.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics