Remote Push Notifications on iOS devices using Parse infrastructure

networking and security and iOS apps by Professor John Patterson and Sam Kaufman in this lecture we’re going to just dip our toe into the idea of push notifications coming from a remote source what we’re going to do is we’re going to focus on learning how to use remote notifications on the device side and we’re going to rely on a company called parse comm to provide the infrastructure so we’ll see how remote notifications are executed and what it feels like to receive them in the iOS platform but we’re not going to build the infrastructure out in the cloud in order to send them ourselves now generally the idea of a remote a remote notification is to tell your users tell the users of your application that there’s something that they need to attend to on their device but that gets initiated from somewhere else in the world so you could imagine any kind of crazy internet of things scenarios where you have a security camera and someone opens your door and you want to alert the user so you could have a server in your home send a push notification to your app to alert them that the door has been opened or you can’t out you could send a push notification telling you that delivery has arrived or that someone is a run your doorbell or that something is available any number of things that are possible parse comm builds this infrastructure for you and we’re gonna leverage it and that’s going to require a lot of steps though and in fact it’s gonna require some steps that I want you to see but we’re not gonna actually assess you on because it requires you to have a developer account with the Apple developer ecosystem that cost usually cost $99 a year they did when I signed up for it earlier this year it’s good for one year and it enables you to put things into the iTunes Store it’s only necessary if you want to actually put things in the iTunes Store but it’s also necessary if you want to be able to receive remote push notifications so we won’t require it but we’ll show you how to do it so the high level as we’ve seen in local notifications notifications can also be that that look the same can also be delivered from a remote location of those are remote notifications it’s possible to set up your own infrastructure so that your server ascending notifications but we’re going to use parse comm which is a third-party company that I don’t have any affiliation with that will send notifications to you there are probably others that will do the same thing so we’re going to show you how to use the service for remote notifications focusing on the aspects that are specific to the iOS site so to high level here’s kind of a scenario that we’re working with there is a device that is going to be running your app and that’s connected to the Apple web services so the same kinds of connections that travel over Wi-Fi or over the cellular data network that deliver your iMessage messages they tell you that you have an iTunes update a lot of the different services that are available from the Apple cloud photos backup etc there’s a connection from your phone that goes over whatever network you’re on to the Apple web services and that’s not any different for the remote notifications remote notifications come from Apple and so in order to get a notification to go to your device someone has to push that notification through the Apple cloud and so we’re going to rely on an apple and parse web services to push that notification to the Apple cloud and then to the device and then on the very end of it is a computer with a browser or web browser in it that we will use to manually generate some of these notifications why might you want to do this well it’s a good way to create engagement with your app if you would like to contact the users of your application and let them know that something has changed there’s an update some new news you can do it with a push notification do it carefully though because it risks annoying your users and having them turn off the permission to receive remote notifications so here’s the flow you start by purchasing the developer account with the Apple now apple happens on the Apple website it’s something that the developer does developer can do it on their own or as part of an organization if you happen to be part of a university or a college or I think even any even a K through 12 I think your university your your place of higher education can sign up for a university account that will enable you to do some of these things for free if your university qualifies for the program today we’re going to see a paid developer account doing after you purchase your account with Apple then you go onto your device you go system and you start creating your Xcode application you need to make sure that when you create it you create it with notification capabilities and you give it need to give it a unique identifier in that initial page because that unique identifier it’s going to be how Apple recognizes your application once you’ve done that you started your Xcode program you’re going to go back to the Apple website and you’re going to create what’s called a developer SSL certificate with Apple that’s going to

come with push permissions that requires your application identifier and so that’s why we have to do it Network once you’ve received that developer or certificate you’re going to take that over to the parse website you’ll need a parse account and you are going to give parse this authorization via this certificate to enable them to send messages to your application unless they have authorization that you’ve gotten from Apple and given to them they can’t just send out messages to any phone so you’ll give that to parse and in response parse is going to give you some credentials that you’re going to take and put into a pod file interface that we’re going to be working with so we’ll put that into our Xcode project those parse credentials so that now our application is identified with ha is identified with our application is identified with Apple and we have the appropriate permissions from parse as well once all those things are connect done we’re going to finish developing the app and users are going to install the app users are happily running our app very satisfied and then it comes time for you to need to push a notification so you go to the parse website and you generate a notification probably manually a push notification using their services using a web browser and you activate it that notification has been validated because you as a developer have authenticated with parse and you are targeting your app you’re the target app the one that you’ve made has been identified with an apple certificate so that notification is going to pass through successfully through all these gatekeepers once that notification is triggered it gets sent to Apple and then Apple is going to confirm that the notification is authenticated by looking at that signature once it has confirmed that it’s going to go ahead and find all of the devices that are running the appropriate app and it’s going to broadcast the notification to the appropriate ones subset individual ones depends on the situation if the user has authorized remote notifications has allowed them to appear then that that notification will show up on the phone and it will look the same as the local notification that we programmed earlier it’s just going to be generated by a remote event and there’s different things we have to do within the body of Xcode in order to support it so here’s of a high-level the different steps that have to happen in order for us to do this there’s a lot of moving pieces you have to do them in the right order it’s not particularly difficult it’s just that there’s sort of an ordering and a permissions process that we’re laying out here there are two pages of these steps and I’ve specified them in two different colors the blue or darker color indicates that it’s something that you do on a website or through a browser or through some apps there’s something outside of your code base the orange steps represent something that you’re doing within your code base within their application you’re within Xcode so the first thing that we do is we make a developer account with apple on a website then we start making the app with parsed cocoa pod install making sure that it has push capabilities then we go to the website we created development ssl certificate with apple then we bring that back to our computer and we install it in our local keychain we then register our app with parse and get parse credentials and then we pull those application those permissions back and we install them in our Xcode project within our project we request permission from the user to receive remote notifications once the app is running if we receive that permission we register that permission with the parse cocoa pod locally then we create a handler to receive those remove notifications then we run our application and while our application has been registered and it’s on someone’s phone if we send a notification with parse it’ll trigger notification so that our goal here is to go through all those steps and to actually generate a notification with parse and see it pop up on the simulator so here we go the first step is going to be to make a developer account with Apple our first step is to go to the Apple website create a developer account so let’s just go there real quick and show you what that looks like now one thing that’s kind of important to do is it’s important that we actually use Safari in order to do this work on the because some of the things that you do in interacting with the Member Center require the Safari web browser or at least it doesn’t work on Chrome so if you run into trouble it might be because you’re having using the wrong browser I’m a chrome person but for the purposes of this I use Safari so I go to the member center I have several accounts depending on my role as a professor or as part of different organizations this account here is my individual developer account so when I log in there you can see that I have an account in order to get this to this far you have to pay Apple money 99 dollars a year is the current rate that you can you can see that information located

here programs and add-ons you can see I’m part of the Apple Developer Program and then what we’re going to come back for is that certificate identifier is in profiles page ok so that is our developer account with Apple the next thing that we need to do is we need to start a new Xcode project that uses the parse cocoa pod and we need to give it push capabilities so let’s do that go to Xcode and we will create a new project our new project is going to be a single view project this isn’t gonna be a UI intensive project and we’re gonna give it a name we will call it the parse pushy go ahead and an H there parsh parse pushy app gonna get myself in trouble and give it an identifier this identifier is important so I’m going to copy it usually these are identifiers are URLs in Reverse format but what they do is they are there to uniquely identify an application the combination of the organization identifier actually and the product is what defines it so I’m gonna just go ahead and do call this remote actually all right it’s objective-c it’s universal and I copied that organization identifier for use in a second I’m gonna put it into my iOS workspace and you can see that that bundle identifier is put here the next thing that I want to do is I want to give this the capability to be a push receive remote push notifications so that we can do that here in order to in order to do this I’m gonna go over and turn it on but I have to have already created a Developer certificate on the Apple Developer website so the place where you would go to do that is back on our website you come here and you need a iOS app certificate if you’re using it for development you make one certificate if you’re using it for production you use a different certificate and in this case the development certificate is already present it’s this one here if you needed to create one you could do use this button here to create a developer certificate and that signs digitally encrypts digitally creates a a cryptographic signature of your app so that it can be known to be linked to your account it can be linked to your account I’ve already got that set up I’ve already downloaded it into Xcode the place where you download that into Xcode is here under accounts and here’s where I downloaded that certificate so if you want to download a new account you would come here and you would hit plus in order to get there now that I have this in my Xcode registered though when I come to push notifications with my new application and I turn it on it’s gonna ask me which of my developer accounts do I want to associate it with I only have one that has the capabilities to do this right now so I’m going to choose that one and it’s going to add the push notification entitlement to my app ID so it’s going to register my app to be able to work with the Apple Web Services Center to receive push notifications remotely all right great so now what we need to do is we need to make it cocoa powder we’re with parse so let’s do that next we’re gonna quit Xcode and switch to terminal and we are going to go into the parse pushy app we’re going to pull it in it in a second then we’re going to edit the pod file that gets created and we’re going to give it a dependency on the parse cocoa pod and we are going to do single quote Tilda greater than one point nine point zero save it and then run pod on it pod install and it should go out and get both bolts which is another cocoa pod that parse depends on and then parse itself will go out and we’ll get those and then our project should be cocoa powder we’re and from that point on we’ll open it using the dot exe workspace specification let’s go there great all right Xcode opens up it’ll index the files and we’re in good shape okay we have enabled our app to be push aware but now we need to go to the website and deliver the ability for our out to be web we need to develop a

certificate so that other services can send remote notifications to our plot to our app to do that we need to work with the keychain program so what we’re working on now is we’re now going to work on creating our development SSL certs with Apple and we’re going to need to do that with our local keychain application so kind of a multi-step process the first thing we’re going to do is open keychain in keychain we’re going to go over here under certificate assistant and we’re going to create request a certificate for our certificate authority now it’s important at this point that you see that the menu says exactly request a certificate from certificate authority dot there’s anything else on that line it means you’ve selected something in the background of your keychain access app you want to deselect it you want this kind of pure line in order to request a certificate from a certificate authority but kind of without specifying which one I here we can specify our user email address I’m going to go ahead and use the one I’m registering that I’m doing this work with and I’m just gonna leave these two fields blank I’m gonna click saved to disk because I’m gonna upload it to the Apple site and I’m going to put it into our workspace just so I know where to find it ok I’ve saved it I’ve put it on disk and now I’m gonna go back to the developer website alright meanwhile on the developer website we’re gonna come down to our app IDs and we’re going to see that here Xcode has uploaded our app ID there are a lot of permissions that have been disabled and enabled you can see that for development these things are enabled and disabled and for distribution production these are enabled and disabled I want to edit this and I want to enable push notifications in the development configuration so I’m gonna create a certificate for that this is where I need my certificate signing Authority I’m gonna upload that thing that I just created through keychain choose it and I’m gonna generate a certificate and now I’m going to download that certificate to my computer then I’m going to open that certificate in keychain so what have I just done I’ve enabled I’ve created a certificate so that a remote authority can send notifications to my application I have installed in my keychain application and now I need to download it so that I can give it to parse to give parse the ability to send my app notifications so if I come here to certificates and I select certificates and I should be able to search there we go by the name of my application to find the certificate I’m going to right click nope I’m going to select it and go up to export items but I’m going to export it but what’s important is that I get the dot p12 format of export I’m going to export that also to my environment into my hard drive so that I know where to find it and I’m going to save it you need to give it a password of some kind ok that was a password for the key this is to enable keychain to get it to pull it off of my keychain so that was my system housework the second one and that downloaded it to my computer alright so we created a development ssl certificate for our application we added that certification for our local keychain and we put we exported it dot p12 format in order to register the app with parse so registering the output parse that’s going to be our next step let’s register the output parse we’re going to go to the parse comb web page and from there we are going to log in and once we’re logged in we’re going to register our application so I already have an account with them okay this was what I was practicing on so actually I don’t want that one I’m going to create a new app the name of our new app is we decided we were what

what did we call it we call it the I can’t remember we just called it it’s so bad parce pushup that’s what we call it alright we’re going to create it and we’re going to go ahead and go to the settings for it so this is an interface this is telling parse that there is an application that we’re going to work with in the future it is not it that’s the applications name it has not been released in production there’s a bunch of different things here that you can fill out we don’t have a URL this is a tutorial for Coursera we don’t have an icon out permission allow client class creation fine user session fine clone the app find clip files okay everything is good there those are the basic settings for our application now what we want to do is we want to go to push this is where we would actually send out push notifications so that’s that’s where we’re going to end up interacting with parse and the ecosystem at the very end we haven’t given it permission to do that yet so we’re going to click on settings and then push here client push enabled means are you going to allow a different app to push out messages to all the other apps we’re not doing that rest push is enabled and we are going to be sending our pushes to iOS and OS X devices and so we need to upload our certificate to do that go oops okay so here’s a mistake when you export your keychain password you have to export it without a password so let’s try that again without a password okay you upload it to parse great so here’s our certificate here’s our bundle identifier that gives that gives parse the permission to send things to the Apple web services to send down to us ok and now what we need to do is we need to get information for our client that so that we can marry let me show you what you do alright so we have registered our app with parse and now we need to get the credentials from parse in order to put into our application on the developer side so let’s do that alright we want the application ID and we want the client ID so we’re gonna leave this open just for a second and then we’re gonna come back to it to cut and paste it at the appropriate time all right so we are now have our parse credentials ready we have uploaded our certificate from the keychain now we can start doing a little bit of our development the first thing that we’re going to do is we’re going to request permissions from the user to receive remote notifications that’s our next step now it’s time to get into the code and work with some of our remote notifications now in this example what I’m going to do is I’m going to start with a lot of the template code that we had in our local notifications objects a local notification example lecture a lot of it’s very similar it’s just we’re gonna change the word local with the word remote and the other thing that we’re gonna do is we are going to have to the object that we get from their notification that’s gonna be slightly different it’s gonna be a dictionary that gets sent from the remote website so the structure of it depends you have to know what the structure is going to be beforehand but that’s okay because you define the structure when you send the message okay so let’s request permissions from the user to receive remote notifications this is within our code and in this app very very simple what we’re gonna do is we’re going to request those permissions immediately upon launch so I’ve got some template code down here all these were not going to override so I’m going to delete them for clarity and what we’re gonna do here is we’re going to ask our users for permission so that’s not necessary you’ll recognize this code from before where we request different kinds of permissions we establish a you use UI user notification type and we’re going to concatenate the alert to type UI user notification type of lured UI user notification type badge and new UI user notification type sound we’re going to do a bitwise or of those three defined constants and we’re going to assign that to our types variable and then for our notification settings

object we’re going to create that by passing it in those user notification types I’m not using any of those local notification actions in this case so my categories are gonna be nil if I had categories or special responses I would add that in there in the appropriate code to define them then the last thing I do is I ask the I register those settings with the app and then slightly differently I register for remote notifications now why one thing that we have to do differently with remote notifications that we didn’t do with local notifications although we could have is we can see what the result is of us asking the user to give us remote notifications now we need to do that because we’re working with this cocoa pod that is going to register some information that comes back from the operating system when we do that so we’re gonna ask for permission if the user gives us permission we’re gonna keep some information about that permission from the operating system so there is another callback as you can imagine callbacks callbacks callbacks and we’re going to add that in our app delegate and it will be called by the operating system when the time comes it is called did register for remote notifications with device token and it’s that device token that we’re trying to get our hands on when it comes back as a parameter we’re going to use the PF installation class PF installation here we’re going to get the current installation of parce framework and then we’re going to set our device token that we received back from our data to the device token that we received so this is going to put that device token into our shared library so that we can work with it when we do our push notifications then the last thing we’re gonna do is we’re going to ask parce to save that in the background now if all goes well we shouldn’t have any trouble here we’re having some kind of a issue oh we’re having an issue because we put it before the end that’s our first issue and our second issue looks like we’re having a problem because probably F installation is not being used I imagine yeah that’s because up here we haven’t made our code aware of the library that we’ve got so we’re going to import parse wash parse dot H the library is included in our directory but this is going to make it so this file in particular can call those files can call those functions when we do that we’re all set all right so we requested permissions from the user to receive and remote notifications we put that in the app delegate file so that’s gonna happen is that attempt to get permission is gonna happen right when that program launches we registered that permission with the parse cocoa pod by capturing that callback function after the user selects if it’s successful then it will call back that function we made and we passed the device’ token to the parse framework the last thing we need to do is we need to create a handler in order to receive the notifications and the other thing is we didn’t initialize our parse framework we need to initialize our parse framework with those secrets that were available from the parse website so let’s do that next so the first thing we’ll do is let’s initialize our library with the secrets that we got we’ll do that right when we finish launching configure our parse parameters and these are not the right ones with the application and the client key and we’re going to get those from the parse website here is our application ID and we need our client key there we go so we call that the beginning to initialize our library we ask for permissions if we get permissions we save the device token to our library okay now when you know when a remote notification gets passed to our phone and when the user interacts with it it’s going to cause our application to start and so we need to capture that event and there are two different ways that it can start if our application is not running at all then it’s going to start by did finish launching with options and in which case we’re going to do a similar process to what happened when we have a local notification if our process is running if our app is running or it is suspended but still in memory frozen then we’re gonna have a special remote callback function that we need to handle so let’s start with the case with did finish launching with options when we receive a remote notification and we are not running at all we’re gonna start with the exact same code that we had when we were doing our local notifications we’ll put that at the

beginning here first thing we’ll do is we’ll check now it’s not a local notification we’re not doing local notification so we need to change that and so instead of a local notification it’s gonna be a remote notification key and instead of receiving a UI local notification we’re gonna receive an NS dictionary the format to be determined and we’ll call it our remote notification right if it’s not nil then it means that we started as a result of a remote notification great and in which case we’re gonna do the same thing we did before we’re going to say okay we want to get let’s get the message a message that we passed from the parcel website to our application let’s see if we pull that out so in the data payload let’s see if we can from a remote notification let’s get a message and that’s going to be equal to remote notification location select select object with key object okay let us use the other format we’re gonna get the element in this dictionary whose key is message again we have to spell type correctly good and the reason why we’re doing that is because we want to set our message equal to whatever our remote message sentence okay so we pulled out the remote message from a remote notification object here and then we correct created a pop-up menu here that said we received it on launch we created an action in which we keep so the user can click OK and clear it we added that action to our lurk controller and then we passed that whole chunk to our UI thread so that it could be presented and then we’ll close that if statement and that will handle the case when our application is not running at all now we need to handle the case when our application is running and in that case we’ve got a function that has a unique name like when we received our when we received our applications like when we received our local notification with actions and in this case we are not doing it that we have the function as did receive remote notification application did receive notification we’re given our notification payload in this case and we’re getting it given a completion Handler and so we are going to do the same thing as we did up here we’re going to create a pop-up menu we’re gonna get the message but it’s not from our remote it is gonna be we’re just gonna change the name there all right so we’re gonna call it a remote notification as the parameter that comes in we’re going to find the message from it we’re going to say we received while running we’re gonna it’s gonna have our message as part of it as part of the window we’re gonna clear it with okay we’re gonna add that action to our alert controller we’re gonna put it on our main loop so that we can present it okay I think that’s everything that we need let’s run it and just see if it runs I there’s no user interface so it’s not gonna do very much it should ask us for permission immediately to receive remote notifications and if all goes well we’ll get that don’t succeeded we love that oh shoot we’re still recording no we’re recording every now and then it tries to install on my phone which is recording right now because I needed to make sure that the destination was the simulator I think we’re still recording it’s all good okay it tried to run it on my phone because my phone’s connected to the computer right now because it’s helping me to film ok now we’re running on the

simulator gotta wait for the simulator to appear to show up for us join us simulator thank you alright it’s like it’s gonna try and send me notifications he’s happen to be remote notifications I’m gonna click OK my app doesn’t do anything else so let’s leave this here for a second when I went to go to the parks web website I realized that we had one small error in our code so this is gonna go back and correct that error in our response function here did we receive remote notification we used a version of this function that assumes that when we received a remote notification we want to do something in the background this piece here indicates that we want to use the version of a function that does something to background in fact we didn’t want to do that so I’m just going to delete that and a remote function is just going to receive the payload of whatever we send it so now I’m going to stop the running of it all and I’m going to run the application with this different remote receive remote notification function and everything comes up fine ok done a lot of debugging and here’s what I’ve learned I’ve learned that parse is unable to send a remote notification to a simulator and so if you want to do this you have to do it on an actual device right now I’m recording the video from an actual device while I do the run the other thing I D bugged was that the documentation was a little bit off for what parse provided if you want to pull out the notification that you send when you send a text message that you need to pull a dictionary called ApS and then from that you need to pull out the key alert this together will provide you what you need in order to get the message from the notification from parse that you can put in the in the message here all right so with that we should be able to go to the parse website send a push notification we will send this is my final message I hope it’s true in plain text as we note here to all of our devices we send it can we see that the remote message shows up on my device click it I open my device and I received it on launch that this is my final message great that was when it was in when the device was not open when the when the application was not running so now I want to do it once when the application is running sending it as plain text sending it now and we see we get our push notification while running great boy that was a lot of work for me to figure out what was going on with my recording setup that was causing it to break hopefully now there’s enough information here that if you wanted to set it up on your own you can but of course you’ll need a developer account and an actual device in order to do that okay put this to bed thank you for your attention