The course is part of this learning path
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.
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 finalize our app by adding the push notification functionality in the like function. So far, we know how to send push notifications and how to gather PlayerIds. Now we're going to combine this together. So, let me cut this and come into our feed cell where we do the liking functionality, right? And over here I'm going to post the push notification. So, let me bring in the OneSignal and let me import the OneSignal as well. So, that's how we do it. Come over here and after liking the post, I'm going to send the push notification as well. So, this push notification will work, but rather than saying test message, I want to say somebody liked your post and this somebody will be the current user logged in, right?
So, rather than saying somebody, I'm just going to get the current user logged in from authentication module. So, how do we do that? It's very easy. We say, auth.auth.currentUser!.email, okay? So, don't forget to first unwrap this. And after that, you can just say something like, has liked your post or liked your post, okay? So, here you go. After that, we have to find the PlayerId of the liked user as well. So, how do we do that? If we come over here, we know the email of the post owner, right? We have this user email label. We can get that email label, and we can get the text out of that label, of course. And we can use that email label to get the player_ids from fireStore. So, let's do that. Let me call this user email and say, userEmailLabel.text, okay? After doing that I can first unwrap this, I believe because it won't be just an empty value at this point. After doing that, I'm just going to call fireStoreDatabase one more time because we're going to get the PlayerId of this user email, so you can just say collection, PlayerId, okay? So, it was PlayerId or PlayerIds. Let me come over here, yep, it was PlayerId. So, PlayerId.getDocuments rather than at snapshot listener because we're only needing this once at this point. So, I'm going to call the snapshot or error. So, if error is not nil or if error is nil, it's your call again. If error is nil, then I'm going to work on that snapshot. I'm going to check to see if snapshot?. isEmpty is false. And snapshot is not nil. So, once we do that, we can just work on that snapshot and say for documents in snapshot.documents. Like this, snapshot?.documents, okay? And once we do that, don't forget to first unwrap this as well. And now I can get this by saying document.get and the field that I'm looking for is the player_id, and over here I'm going to try and cast this to be a string, okay? Because we are saving this PlayerId as a string and don't forget to give a whereField over here and whereField will be email, and it will be equal to userEmail. So, we forgot to add this one. If we don't do that, it will bring all the PlayerIds in the database. So, I'm going to check to see if let PlayerId = document.get as string and if that's the case, now I want to do this push notification thing. So, let me cut this one as well. So, I will send this push notification to the PlayerId. So, let me do this one more time. Let me delete this and let me put PlayerId over here. So, here you go. Now we know the content that we're using, and we have this data error, we have to put quotation marks inside of these curly braces, not curly braces, but array braces as well. So, come over here and say quotation mark and again quotation mark. So, that's it. Now we know the PlayerIds, now we know the content, and now we are ready to test this. So, I'm going to run this on my real iPhone, and of course I'm going to run this on my simulator as well. So, we will have two devices to test this on.
So, let's see if we can make this work. So, let me minimize all of this window so that we can see our phone and simulator. Let me close this down. And on the left-hand side I have my simulator and on the right-hand side I have my phone. So, as you can see, this is my phone. So, in the phone, I'm logged in with James and in the simulator, I'm logged in with Kirk. So, I'm going to like one of the post of James and here you go when I like this, I will see the notification popping up in my phone. And as you can see it says, that email@example.com liked your post, and it's fantastic. As I click on it, it will take me to the app itself. So, let me test this one more time. Let me like this, and I will get the notification over here. And when I click on that, it will take me to the app itself. And let me test this while app is open. So, let me try to like this and let me just close down the do not disturb mode, so we can see the notifications. So, if I like this, here you go now, I see the notification. And let's test this while using another app. Like, let me go into the books app so that we can actually test this.
So, when we like it, it will pop up on the top of our screen in our actual phone and if I click on it, it will take me to my Instagram Firebase clone. Here you go. Now it works perfect. Of course, there are other features that you can implement with OneSignal, like you can handle the incoming notifications, and you can choose what to do when a notification comes. Of course, you're going to have to read the documentation in order to do that. But now you know how to send notifications, which is great so that you can integrate this technique with all of your apps. Now, let's stop here and continue within the next lecture.
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.