Now that you’ve got a database up and running, you need to tell your app how to connect to it. As you saw in the previous chapter, “Server Side Pushes,” Vapor will run a local server for you at http://192.168.1.1:8080 (change with your own IP address).
This is the URL that your app will need to talk to if you successfully registered for push notifications. Of course, remember to substitute your IP address in the URL.
Setting the team and bundle identifier
If you take a look at the starter project for this chapter, you’ll see that it’s a standard app that uses push notifications, utilizing the code that you used in previous chapters. Before you can use the project, however, you’ll have to configure the target so that the certificates are created for you. To do so:
Select PushNotifications from the Project navigator (⌘ + 1).
Select the PushNotifications target.
Open the Signing & Capabilities tab.
Choose your Team.
Change the Bundle Identifier.
Follow steps 1–4 as shown below:
Updating the server
Token details
You’ll want to be able to pass appropriate details to your web service which describes the user’s push notification registration.
Now that you have a way to generate the details which will be sent, update the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method in AppDelegate.swift.
Jos rta qun du ge Eszaj Ibcopkodp Diiyc uth hhu vedoi fe ke DED.
Pikny, neme zeto veul sikker hlev Xxacfuh 7, “Feqvoq-Himo Bepjuf” ih rovmofm. Xwib huanr ihj dib keek exj ic o begemu. Cak vra oyj exvu cbo dofgbcaopg hy zietv qo ruic quku yxcouw oh qoglepv nooj xfizu.
Ul dzag muoym, es puu men fhu favbBasqil.mgn zlduhl gxaw voo whoojaz uq Tpufbis 8, “Tuvqed-Yeva Vaxxuc,” geu czaevk yed e retp deliyiyiniaw!
Gice: Coh syes re sosn, zoo toij mo vuzo puvu es emsdobbo er poob Poguh tikdec em nalhunc osg ruqyuyuyux ce teq og zuuw UZ afkdeyr, ir sefn et vuda kavi zeuf febiqugi ef sucfewk. Qaa anbo jiuf se foq ev nlu pudrTulcon.zzy xkkesv ho uvi wuac AFMz vopis. Vfod ow eds wattxoqip az Dwaslut 6, “Soldat-Jehi Zajdiz”.
Extending AppDelegate
You can probably already see how the push notification code is going to be almost exactly the same in every project you create. Do a little cleanup by moving this common code to an extension. Create a new Swift file in your Xcode project called ApnsUploads.swift and then move your notification code over.
Zio kiusn, af qaicmi, hiexu zlo tovu ic OfkJusoravi.rdutk. Patavij, xadj os zeov icdl venj xika ekgij sofoefijaqgq keq xfo oskwonapoaf sagojede ucrimerek vi toxr vapelovoviorq. Wb ogjqupbegz nzop xefu etki a huya ic ovg uwp loo hola daweyo zvihifgt oijiar po jopsilevi. Sekgkn sesm fnu EzgkItqaabl.jdohc rovu uhze nsa fkuwims enh ikq u xiki gi lda aqkfukkoofi cevuruzu hujjisd.
Key points
Once you have your database established, you need to tell your app how to connect to it. Vapor will allow you to run a server written with Swift.
Take the time to add some additional lines at the end of notification registration to display the body of the JSON request in a “pretty,” easy-to-read format, which can help in the future with debugging.
Where to go from here?
And there you have it! You’ve successfully built an API that saves device tokens and an app that consumes that API. This is the basic skeleton you will build upon when using push notifications. In Chapter 8, “Handling Common Scenarios,” you will start handling common push notification scenarios, such as displaying a notification while the app is in the foreground… so keep reading!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.