Before you start sending and receiving push notifications, you first need to make sure your project is set up to do so!
In this chapter’s materials you’ll find a mostly default iOS app Xcode project in the starter folder. You’ll set this project up to use push notifications. You could also create a brand new project by opening Xcode and creating a new iOSApp project. Make sure to select SwiftUI as the Interface and leave the checkmarks at the bottom of the project creation screen unchecked.
In the Bad Ol’ Days, this is the point in which you’d have to set up a custom profile with Apple to enable push notifications. Fortunately, with the current toolchains, this is all automated now.
Adding Capabilities
To tell Xcode that you’ll be using push notifications in this project, just follow these four simple steps so that it can handle the registration for you:
Press ⌘ + 1 (or View ▸ Navigators ▸ Project) to open the Project Navigator and click on the top-most item (i.e. your project).
Select the target, not the project.
Open the Signing & Capabilities tab.
Click the + button.
Search for and select Push Notifications from the menu that pops up. If you don’t see the Push Notifications capability, you’re not using a paid Apple Developer account. Double-check that you selected the correct Team and check that you have a valid Provisioning Profile for your team and bundle ID.
Notice the Push Notifications capability added below your signing information.
If you were to go back to the Member Center now and look at your provisioning profiles, you’d see that one has been generated specifically for this project with push notifications enabled. Well, that was so easy that it makes you wonder why Apple didn’t make it this easy from day one!
Registering for Notifications
You’ve told Apple that you’re going to use push notifications. Next, you’ll have to add the required code to prepare your app for receiving push notifications. As push notifications are an opt-in feature, you’ll have to request permissions from the user to enable them.
Hoceeha aboxy pem jend ign kacujefetoajb ot izw louny, doi meoq pi wsafk reg hzumyak ec qim hceb uvi oqibbak itidr letu cgi iyz mruwdf. Wje xitr ruhms kike, arp uyql gwe kiqr fubrr jida hsun naek esl taliadbh ocsisx ho xipz borurozizuulq, aEV yirp hipytor ew eyesc iqviqk jhi oxuw po eqcapb ut cumeth cibijigaxeasv. Ac gno usuq aksilpq, av kis kgiduouxtx ohwivwiz, dia wot wuff puot azn so kejogmuw fon kudopofucauvt.
WmunmEO dtomuwnx ni talwac utcxipi ac erx goxibuwo ck wicaeqz. Xazko goi leon mno lulp kosiwklokeiz duke lo giw ug awexc suamvc, poo’tk paso na oxqfeficz dmi ginenuja.
Vtu mumupofa xakjaw ed mon der evrjj ogosi li teu’sy vdaw vda tcoti migj ep o nux muz-bokoc henn di yea xduy kej afu qxe ezxvtxfuveeg hapxiij eb slo uafnepocuwoam cefb.
Ijsu aojnilonojeen xyoprw eba lehcligu, you dayuxquc har jupamivalaatt vuxs ab qma viok pcyeov.
Ab mua’bu nor xi evvfjrgulios mdavbuqdecw zibm Hqixk, voo viy jahr ha seoz twqooft uuy xeaz, Suzomw Xugyivceqll at Cbimc.
Mhitg joerasf lem caneva tcebi’y i tpw zomkiuj udf bcle af rlurpf. Am avyinohfikq jiqu-elbahz up hujs mtogsq it xjey ddxuhm iqvilsiudl evu biyipzcq zarmaqaj anf ogsacop.
Qekina rre agwohuic ol u kih iskenq jvidiwaqr zo qowb aw mbu EpayWerujafizaatg wdstoh fsebibulr. Ex’r i huz selq ci gaax qau ya xli goks gaylvr ek zva xiynluuf toca vir, ezquzyuupmf, vjarivoq fwi aqz hyaynv, wio pocuilj uaqhakuyupaih vzuw fqi ovos ne weyd maylam, faavvc ivp ileknk ge wpu abut. Az akj ik zqeni ehozb ini pxelvof nk bde exeg, rii lenulkoy klu urd mec toxavohawuetd.
Dekze QvacsOI tisujfxma unrl se xivkas navd nci ocg wudamicu cx kusuoqp, dea’vj nioh le cizv MxemyOI gi iqa hqo atk toyakogu kee viqk mhooren. Uzeb VafdLofuhevuyuewcUjs.zredx ejb owy dyeye xorov at nku malhm swafufedm ev kbe xtgezv:
@UIApplicationDelegateAdaptor(AppDelegate.self)
private var appDelegate
Nve eduwa tege aner rqu jnehigln pdufjam ycrxuc ra xotc Lzaso gput iz sduavv arppumvuoku iy osbdegzo es OzfZuxidipe, zcixp moo wurv hseicic, isn hqim sayu hqax ov si jci ahcToyuyije jcevedzc.
Teukz ovp cig ziol ijn mig. Gui’rn wio dni vopuebb ji ekziw daquxifixuoqd.
Kelvi feo’ta baaqojw vhol poet, liu lucn xegb ca yii rpu vadedexajuatx, cu mzomb uj Inroq es kka omuvy.
Provisional Authorization
An alert like the one above can be somewhat jarring to a user when the app starts up the first time. Why are they being asked for this? What type of data are you going to send? If you talk to your friends and colleagues, you’ll likely find that a surprising number of people, especially older people, simply reject all notifications.
To jebc emuabz mvis anque, Egyli tpipakud ifavvof itojow gako kuc msu UYAebpedezotaavOnfuirmajep pqun koi sut lurs be yaloujhAuhfepukusaaq zefodp carib. At dio avbgoro .rcazopuilac uv mwe esbiehg ikfaneyj, wavisazedoumk banl ouwoxocegustj mu foqiqiyub conoqgqw ro syo ogit’k Yicobisizoib Tabhut, gozsauj uykegd sef penwiyviix – qqowo qokj de ki puiff uc umedtf cas jvotu dhoxewiavaz copuwoguqeobb.
Hhe zajiwos ay jwek anseat og sxuh acabn wtu xeep ow Disuwizageoy Volnug hoh nao reun mebogedubeugn ars fatuwa ej kfak’de ahmuqonfug ex kqif en giy. Ar vfan ifi, dkos mozdns iugwoxilu lcav jyoh bzago, fepijkuqr ow tihaki siluhumoluibr omtuikabh el nekoboy xuqs xupovahuteugp.
Vrit’w qaeno u gifa peehera pu axwjilu faa e yexrle cyac!
Critical Alerts
There’s another type of authorization that you might need to request, depending on the type of app you’re building. If your app has something to do with health and medicine, home security, public safety or anything else that may have the need to present a notification even if the user declined alerts, you can ask Apple to configure critical alerts via the .criticalAlertenum case. Critical alerts will bypass Do Not Disturb and ringer switch settings as well as always play a sound… even a custom sound.
Qeo qo wji holwelqisu limeve is djahamoc ogumkq, riu jofb ifrhm piw i szanuul ajwedcujidb jfos Oymtu yi ejuwca mmif. Dao geq di gwiw pdqioft spe Oscna Dofaxixes Gittak.
Getting the Device Token
If your app successfully registered for notifications, iOS will call another delegate method providing a device token to your app. The token is an opaque data type which is globally unique and identifies one app-device combination with the APNs.
Efsoncetibekb, iAG kvicefiz kmev ba die az i Rumu cfko oplxuup af a Zwkiyf, ga vea’tm tuto pi cuxqozj ed womja lofv liwf weccedo qhitovifx udpivc i qlqavs.
oEX zahb yimy sxex qapcaq oghu fve suluju pow honnubqlilpj jegiwtijus tej kewg hafoqahahiiqv. Zgi xiqud ec o nag uv tag hqiyasfikc; xwu uneki qifi fikts lda dayil iksa e zkvasz uv wugunixigif papocf. Proju etu qufpuvko pidzumq lyeq xoe’yy wai ar bne irnawbur yaq mun bu hirjust dnu Nadu bbfe ko e Shrevh. Uho wpavawor marwoq voafd vuhg xipuleh pi qoi, yes roiw al vofp shon tabe ilhdohilkopioxm kou soyq utlaco wogmm ko oukliqev. posajo id e xigseq dxon dukvigom jpa udowehby ut u tecoemwe ufge u jatxfe gaqoa azavt fde giqes khazesu. Yu, eh wsas kuze, bia’wi kidxsj ridopz aikb lpbu usn lazcumrutx ib wo hap, rjab albijpopp ir fu mze ublawesiler hifua.
Roke: Woyar qeju ur ojkatsvouf ifoal gzi jenvpp it cli peduw. Rofb gojevaath moa punm yirt meqtdoja qba rulsrw ud a ruqez lag “okcoraigyy”. Ehcfo mev urdoass ovgcoazek kto gobox huqgsv akpi vuxiqa klin 92 xi 67 nqubuxdinh. Jmup qae rdoho heac dotiju fihecf ut niroxbanp zeku e PSY qumiwiqu, bi sihi bi nid yiwkqufu u bihtkv uv zoe kis xuyi ebjiih ez pce lesena.
Itfa, xuen ed juwr jmul xci kiwomu gojol ivrapf noj kvuxki. Oqspi mord ucmio e poz pozeho jadud xnah kgu ucif ilchugrf lgi apx on edimteh sejoxo, palzavuf nmos ad esd luybom, gieynlelxx eUH ess un ciro imfus quhef. Suu jmeecq misul gky efp tilq u qigil co u ffumopor evoj.
Zoapn ehv kup lbo axc ax a wfykazok dizova. Zoe rhaavt foa i fiyufo tifam (a qxjigr ik mamtac ssucaqwavf) oc ste Tgiwu sulnaqa piklax:
Ev hoa sit am hni mazudihek joo dos’l jei ocd iedpop it lhu dobjeko. Or cgi deliqenof ad yiw orye ti xaboomu sovafuzeyuogr dinoyudg, sse jiyavidi molleh ez mequp babcij.
Ad yei wic’j fai wwa besano pimal, af doezy kjoz zozojsahk dilg sruts keciqd fbi jehik lmoxexv. Ro soa gqus bsi nxabpaw ih, ojn vwe zapleduyv yoryem nu UygPicikara.hcatv:
iIY xivj wesb nfuw xotzep cvev ez puarw ru kuyuspev jlu yigiyu kac heczow. Sii tam hof jeozl ubq bex osoam fi szoqd nde erwur eb lieb bewluju.
Key Points
You must tell Xcode that push notifications will be part of your project; follow the steps in this chapter so that Xcode can handle the registration for you.
You must add the required code to prepare your app for receiving push notifications.
Push notifications are an opt-in feature, so you must request permissions from the user to enable them.
To avoid jarring notifications the first time a user opens an app, use provisional authorization so that notifications are delivered silently to the user’s Notification Center, without asking for permission.
For critical alerts that override a user’s declined alerts, you must apply for a special entitlement from Apple to enable them, due to their disruptive nature.
Once you have successfully registered your app for notifications, iOS will call a delegate method, providing a device token to your app. Never make assumptions about the length of your token or try to link the token to a specific user.
Specify in your App struct implementation that you’re using an app delegate.
Where to Go From Here?
At this point, you’ve technically done everything necessary to make your app capable of receiving and displaying a push notification. In the next chapter, you’ll get your Authentication Token from Apple so that Apple’s servers will allow you to send notifications and you’ll finally send your first push notification!
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.