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 add an if statement to our PlayerID saving function so that we won't create a duplicated value of this firstname.lastname@example.org when we run this Instagram clone from scratch, okay? So, we have to check if this player ID exists in our Firestore, right? So, let me bring in this Firestore to our viewDidLoad and I believe we have this Firestore over here. So, let me do that over here so that we can use the same Firestore in the get data from Firestore function and in this viewDidLoad. So, let me run this over here. And rather than creating new Firestores we're just going to call this Firestore database, okay? And down below it still works because we can easily reach the variable that we have just created, right? So, over here I'm going to use Firestore database. And before we go on we have to do a query over here and say playerId. So, we have to get the player ID of the current logged in user. So, at this point we have to choose between adding the snapshot listener or getting the document for once. I'm just going to get the document because I don't need to add a snapshot listener because there won't be any changes during we use this app, right? So, I'm going to say getDocuments and it will only give me the related document just for once and that's what we need. And over here I'm going to filter the results before I say getDocument. So, I'm going to use a whereField like we did in the previous section and I'm going to say email is the current user's email, okay? So, remember it asks a field for us and I'm going to provide email over here and the value will be the current email. So, I'm going to get this from auth.auth.currentuser and I'm going to force unwrap this and say email. So, this collection will return the documents related to the current email that we have over here. So, I'm going to call this snapshot and I'm going to call this error, and if error is nil, I'm just going to go for is nil this time rather than is not nil. Of course it's your call, you can go for is not nil and display an error message to user as well. But I'm going to keep things simple and we will get a snapshot over here. So, you can check to see if snapshot is empty, is false or you can check to see if snapshot is not nil because we're going to do what we have been doing over here. So, we will go into a for loop after we say snapshot.documents. So, I believe it's best to check to see if snapshot.isEmpty is false so that we would be much safer. So, over here we can come and say for documents in and I believe I have forgotten to add snapshot is not nil, yep. So, let me add this one as well, so snapshot is not nil. And then I can safely come over here and say for documents in snapshot.documents, okay? And I will force unwrap this when I do that. So, can I come over here and say snapshot.documents? Yep, I can say that but once I do that it will ask me to make it optional or force unwrap this. So, I'm going to force unwrap this, that's why I'm doing all these checks. So, I'm going to say for a document in snapshot.documents, okay? And as you can see it makes it optional but I'm not going to make it optional. I'm going to force unwrap this, and over here we need a value, right? We can get this out of our document and we need the player ID. So, remember before we were working with Arrays and getting all of these different values but this time we don't need those, we only need one values. I'm going to say document.get and the field that I'm looking for is the player_id. So, that's the one, yep. So, it will give me the player ID and I'm going to say if let the new player ID, okay? This player ID thing is can be casted as string, as optional string, okay? If that's the case it means that I have a previously created Player ID. So, if that's the case, okay? So, let me call this player ID from Firebase so we won't get confused because we already have a player ID over here. So, player ID from Firebase, if this exists then I don't need to create a new one, right? Because I already have one. So, I can check to see if they are equal or not. So, let me just print this out and let me just test if we can get this. So, let me say playerIDFromFirebase, okay? And I'm going to say playerIDFromFirebase. And later on if we want to do a merger, if we want to update the current document we will need the document ID as well. So, let me create it for any case. So, I will call this documentId and it will be document.documentID, okay? So, remember that's how we get document IDs. So, let me run this on my simulator to see if we can get the player ID of the current logged in user from Firebase. Of course we are doing all of these things like player ID dictionary and saving it to the Firebase again. So, it will be a little bit complicated. It will save the same thing in the Firestore one more time but we're going to take care of that anyway. So, as you can see we can get the player ID from Firebase and we're getting it twice because we have this data twice. Now, of course we have to take care of this problem. So, what I want to do, let me just cut this from here, okay? And under if lets let me paste it over here. And rather than printing this out, I'm going to check to see if the player ID, if the current player ID is equal with this player ID from Firebase, okay? So, that's what I'm going to do. If they are equal then I don't need to do anything else. So, let me say if playerId and I'm trying to refer to that player ID but I believe we have to use playerNewId because that is not an optional string. So, if playerNewId, okay? Is equal to playerIdFromFirebase then it means that we have the same ID in the Firebase already. So, we don't have to do anything over here, right? If that they are not equal then we have to save this. Then let me cut everything from here, okay? And paste the playerNewId thing under over here. So, this will create the new dictionary for me. And if they do not have the same value then I'm going to upload it. So, let me check, yep, this is getting the current user email and this is getting the player ID from Firebase, if they are not equal, we are just uploading them to the Firestore. And in here, I believe we have to say self.fireStoreDatabase in order to refer to the Firestore database, okay? So, that's good. If they are not equal, I'm going to upload the value. So, let me test this, let me come over here and delete the duplicated values. So, let me delete one of the Kirk's and let me delete one of the James as well so that we will only have one Kirk and one James. Since they will be equal, it won't upload a new one to the Firebase. Of course we can do this some alternative way as well. For example, if they are not equal, but suppose that the player ID has changed, then we can do an update. We can do a merger as well, right? So, let me test this, and as you can see we don't have any data over here duplicated. So, it works. But also we have to figure out a way if they're not equal, but if we don't have any snapshot, if we don't have any data. So, what do we do? Right? Because this time we're going to go over to playerIds and we're doing this whereField in which we are trying to figure out the current player ID of the user. But if the snapshot does not exist, if snapshot is empty, it means that maybe this is the first time this user is using this app, right? And in this case we are not saving the player ID right now. So, we have to make sure to find the else statement of this if snapshot is empty statement, okay? And I believe that's the one, nope, we have to open an else statement over here. I believe this is the one. Yes if snapshot is empty then we have to save this data to the Firestore again, right? Because we won't have the data in the Firestore at this point. So, let me copy this one and paste under else as well. Not, cut but copy and paste over here as well. So, this will just save the player ID for the first time into the Firestore, right? So, let me just make this right by deleting this unnecessary indentation over here, and here we go. Now we are ready. Of course we can improve this algorithm as I said before, we can do a merge up in the first else block and we can try to update the current value or we can delete the current value. So, here you go. As you can see our algorithm is working. Since we have the player IDs over here, it won't save one. So, let me delete the Kirk's ID and let me run it one more time to see if we get the Kirk's ID in the Firestore. And if that works out as well, then it means that we have a complete algorithm. So, let me come over here to Firestore and here we go. We really have the Kirk's player ID saved in the Firestore. So, that's it for right now, I believe now we are ready to go into the like functionality and find the related player ID and send a push notification to the liked post, to the post opener of the liked post actually. So, let's do that in 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.