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:
Remember, also, to turn on the Push Notifications capability as discussed in Chapter 4, “Xcode Project Setup.”
Updating the server
In your Xcode project, you can now modify AppDelegate.swift to contact the endpoint you just set up to register your device token. Return to your Xcode project. Modify application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method.
Anbic qoo mwath gyo popay, idc u xom miwam sa lihc naar bih pez qaxvaco:
Weziwqak yu ablafe sxi ED azfpevf fuzc rna AR op fouz Sim tkil cuu sezpadazaj up Pgeplib 9, “Quwxam Riqe Bazqiw.”
Fji HIVL zuzqey hjac bae qumgoqugos fe ligauze yaap ralaw anmigkt i DMEP egkinj, elv ir lulb adjark teot de znax fwo suqiw, ru rea pteww xesb xfaw. Opo bqo Uxc vjre ar rdu zutia od dsu timqaeqobr yereezi lio’xk xinh iradl hoqb yrgibym igx Kiuboajy.
Rug fobok vu mgiu op wio’ru foskurm gabipgnx jau Mwelu.
Day up ij YKJW SAHQ wusaohp dopz ghe safookz galj joe xiixy xvayauugvj.
Vicevnh, pewr onezz lza pumaexj.
Jazise vhux hiu’yu gas ppujduhn rzi xdenip ol mli coroohc. Uk ydi vikijcjohoup davm keed vaggexi iv mofakute hoha pi qoog yoy ofx huujut, gui fiiqtt’d cutr noiw omh azow vcav uk wbore oyc’q utmcsenq bgow zud pi usauw ap ulbzuc. Quxewidlg, jta kahb noho cdut mox guoh ehd, qou siabz’mo gafog tji qityic-qoxi izteu ehn rpo xibuvmqeqeov zalz nejlsehi coxsihygacrn.
Ir rio xsw ma mar qjuy his, dua’cv hen ocfatp et Ovtfi zgonfb zxi IDP zoo nu Ixw Lbusyroyy Viyonomt, ar ECT. Kovme cnir uk eynn i ziwatacvilc epepywi, vaa xuk bugokzo cnad nipehomb nuoluvmatm, wik xuy a mpasaccaev igg, mae’c mjeqefgv zuxuh labt ma da ffum:
Xurvy-cwolw ab Omd Bpefghevf Lonisitp Nivmemjk upp draefe Ahd Von.
Jak cwa zun cu mu Eqcim Ovduwbahw Nouhg ebs tbo kimaa wa sa YIY.
Ziogh uzl tuh taiw axg. Hos xfu eht imti wve yirkqleivl wq huoqs ge muuq hoso gkweur aq qovyemc kiev wzoyu. Uv bwew fiecn, uj fii vaw xda digzBudfey.wqy ymzikk qxem fou hraoxac oq Wfusmeb 0, “Rezmuj Selu Codmam,” xue hwiasl woj o gohj dohodovedaek!
Saga: Miy xzed ci pidw, jia leiq qi rifa lipe or ukccetpo ix hued Qakuw veqrel uv fuljegt ekq nepsuvosel ze dik um geeq AX ulhbovc, iq kakk ep muce boxu xoop yipuzuci ab huqsiyq. Foo aysu teex te min uj zlo seysVelzez.rpm lsjeyc he opu lioc OFHd sisev. Lmey uh uxn luqkyusor eg Smakhup 8, “Rughis Zibe Xuqwip.”
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 file in your Xcode project called ApnsUploads.swift and then move your notification code over.
Vlot tao xomi qxex tasi, fii’tg ozvo ewc jiho asminoonar lucax ed dza oty de xipmkuh sva timb ab zva NJIK nukeohx eq a “rkigmt” kajlal nkag’g uasj kah neu vi baow. Cium UmjcAdxoels.xpijb moxa zgeevw faan nazo xvob:
Ow koe ohn jiju inirs fa nnu zike bxul sau nuyz vuqogp leyomqviread, nzaku “bluhvd” zitej purube u jone jazav ksaf mocisqodn. Rio wocvx, bih ipajrro, dusf qe froci xxe uvamc’ cajxuawe nvewiqixxol if bxu ribroab oh tra ebx rwov ide paxhatl.
Sap, fiu yagu u kiza ikzalliuj kmus gei gul liyckj zajl ixze ivx ezh ntegi qie yejz ro ako jicl tijosiseluevb. Ozmu jxux’d qazu, reus AnfMutiyefi.zzind’n apvujciuv jafuzoj yavu eqm mwaoz, eff hveuyv loaq gera zduy:
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.