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
Prerequisites
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 gather the player IDs, and save them to a database like Firebase so that we can get those whenever we want. Right now we are sending our push notifications to this device, but we don't send our push notifications to this device only, so we want to know what device that we should send our push notifications. And in order to do that, we're going to gather every player IDs, every device that we have in our app, and we're going to save all this information into Firebase so that we can gather them when we need them. So, there are a lot of ways to do that. And even though we can use any other databases as well, since now we have the Firebase, I'm going to do that in Firebase. And of course if you're working with parse or any other database that you want, you can do it with that as well, because all we need to do is just to find somewhere to store this data and then reach them afterwards. So, I'm going to comment this out because I don't want to send this same notification every time we run this, and I'm going to gather the player IDs over here. So, let me show you how to do that. Of course you can find this in documentation as well, but I'm just going to show you because documentation can be messy sometimes or can get complicated. It's not messy, it's structural but it's sometimes hard to find what you're looking for. So, how do we get the player ID of the current device? Of course, we're going to use the one signal methods. So, I'm going to create a status over here, and the status will indicate whether this device is subscribed or not. And in order to do that, I'm going to use something called OS Permission State Subscription State. So, this is One Signal Permission Subscription State. And I'm going to get it from one signal.get permission subscription state.
So, this will give me the current subscription state. If this is subscribed, I'm going to get true or if this is not subscribed, I'm going to get false. But I can get player IDs out of that state as well. So, let me create the player ID. And I will call the status and dot, and as you can see it gives me so many options over here. What we want is the subscription status itself, this one. So, if you come over here and say dot one more time, you can actually get whatever you are looking for. So, in this case we are looking for user ID. Even though it says user ID it's actually the same as play your ID. So, I'm going to print out this player ID and see if that's the real thing that we're looking for. So, as you can see it's an optional string and you can check to see with if let as well. So, for right now I'm just going to force and wrap this, but in this case I believe it's safe to do that with if let. So, let me run this and see if we can get the player ID. So, here you go. Now once it fires up, I'm going to see the logs and I'm going to just filter this with player ID, and here you go, now we have this player ID. Of course we can check this so that we can be certain we're getting the same data, and let me refresh this. And as you can see this is the same player ID that I'm seeing over here. So, I know how to get player IDs right now. So, if I save every player ID to Firebase, now when I need them I can just get them. So, let's do that actually. So, after getting the player ID, I'm goin to create a firestore reference, and let's see if we have one. We have imported Firebase over here, and let me open my Firebase firestore as well. Rather than saving under parse, I'm going to create a new collection. Of course you can come up with some other algorithm as well, but I'm just going to keep it simple. So, let me create a fireStore reference over here. So, this will be firestore.firestore. Remember this gives us some instance of this fire store.
And then I'm going to call this firestorereference.collection, and I'm going to call this collection player ID. So, you can call whatever you want, but after that you have to call a document and this will ask you for a data and a completion block. So, this will be our dictionary. So, I'm going to call this player ID Dictionary, and I'm going to save the email of the current logged in user, and remember we can get this from of.of.currentuser. So, this one, and I'm going to force and wrap this and say email, and I'm going to force and wrap this as well. And aside from email, I'm going to just save this player ID. So, I'm going to say player ID, and this will be the player ID that we're looking for so this one. And here you go, that's how we save player IDs to the fire store so that we can filter the results regarding with email and we can get those. And I'm going to do this if let thing because it won't be safe to force and wrap this. So, I'm going to call this player new ID, and as you can see it's not optional anymore. So, I'm going to delete this, and I'm going to take everything from here to there, and rather than saying player ID over here, I'm going to say player new ID, and I'm going to have a non-optional string. And I'm going to use this Player ID Dictionary to save in my firestore, and then I can just call the current email to get the related player ID. So, as you can see this is our Player ID Dictionary, and let's say, it says that it's string to string and I'm going to cast this as a string to any dictionary, because firestore asks me to provide a string to any dictionary. Now if I come over here as you can see it's string to any dictionary and now it's done. And if I hit "Enter" over here it will give me an error if there is one, and I'm going to check to see if error is not nil. And if error is not nil, let me close the string down. And if error is not nil, maybe you may want to print out the error or you may want to display the error message to the user, it's your call at this point, but I'm just going to test as if we can actually save this data to the firebase. So, let me round this in our simulator, and let's see if we can see the data in the firestore. Now it's over here running, so let me refresh the fire store, and let's see if we can get the new collection from here. Here you go, now we have the player ID. If I hit on that, you will see that Kirk@metallica.com has this player ID. So, it's working fine. But there is some sort of a trick over here that we may want to do, because let me run this in our iPhone as well to test this out, and you will see it will work fine. But I believe every time we run this app, it will just upload a new value to the fireStore and that's not what we want. So, let me go over here and see if we can see my iPhone's player ID as well. Here we go, now James@metallica.com has this player ID. But let me run this from scratch, and let's see if we can see two results, but we won't be seeing this I believe because we're not checking for anything else. So, let me run this in my simulator. No, not in my simulator, but I'm running this on my real phone, and here you go. Now, we have two James's in our player ID collection, and even though they're having the same player ID, we don't want two James in our player ID collection, because it will get complicated. So, I believe we have to figure out a way to check for this. If we have this, I won't create a new one, if we don't have this, then I will create a new one. So, it's fairly easy to do, and it's all related to the firebase not the push notifications, but we are going to have to do it anyway. So, I'm going to stop here and within the next lecture, we're going to take care of this problem.
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.