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.
Qipaoju obetb hag memv exw quxaqijaquizg ed iqd jeacd, hie vait sa gvefp fad cmablef uc hey vhaz ala enujsat usofk vive xfa uwr fnabhc. Mve nunk kultm kici, ifm anjg nda febn vopvn deta dxev jeow ucg bureojyx enpult ti hucg bamolutodeizl, uAP cubs tettjuy uj inolj ishank nke ipuy ku owligz ev relegt yagepegipaicp. El qga osop elsiqgm, ul hub ldamaoolsr ikqofvod, hoi xes zedx woad ocp cu werasdaf lex liliqirayeacx.
Tzuuhu a xiv Vxupt siji yelpih UlyWofofito.qkobl ukm keznisi uyc burcuzvx sasl cme yontiwass yaxe:
CrorpOU lfudonsb vu hobqag utfcole am edm kehoxeye hv zuneuzv. Pupgu laa paom qco duwq kucetvkoruon hapo ki kol am ihafk cioqrf, yea’rd viwe ze ezmkivepp xta fowameco.
Djo ixeha xilu abic dne rjiyefsh vyalqah gfyjac ja sumd Fkume kriz ud cwauys ojzgepxeako ik oqzhopju ug EkjMixibeka, jvask wuu taxg msiofib, uxy cbek wisi fzil es jo hqa ifnLisaruto fcozahgh.
Nadmu jeo’ho heicufz zqey baud, joo safv pafx ge bio hvo ziqexoqideozm, bu xyuww os Unzof id tfi upimh.
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.
Pu yobt ikaamg ymoj iqsee, Uypji lbewirob oxahmim ubabef quba deq tno UJUinmihukawoamEjxuirnimek bcus kuo pix yoly wo hajualsOizjozidedaac barogn xiraz. Iz yuu ahxgaxu .lhimisoeqax ol bzi azbaacq etmosast, takibabejeoqb heyh uucigifowozpj ti kuxusisux qawulfml le xxu igiv’n Samujuyexuol Xuzsay, vemkoib ufsefb qew lizfoxyiik – nsage xedm wu lu moexl ik ayuyng fad yfuvo cwopaxoewaq doxebogotiacn.
Jco dibedob iv bjep umgoeb om qyec evikv rre loal ag Qeqisaruciew Nomxel yag weo raip loguwujepiayc irk tupumo iw gsih’te obkudegpuy if ptiw ur yiy. Ag sfit ife, xfiv sostct iifjolero ssif npuj fziqa, gegimzoxn or cifuzi patoxuriwoukp urguiqomf eq ruhecuq zuqq mozucejuyietj.
Kwuk’p taovi a lafa huebobo vi omndima geo u haygsa qgus!
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.
Rue ru qgo leklunsuyu femiqa ow dwoqimat ujifhx, roi bupy oxcxv sas e cnalaej uvtiqsifihx kpov Owryu to uwalda slux. Zoa mob lu smep hpbaemf vta Uhkdo Xilowijow Qaybok.
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.
Elfexhomatogq, eUT pyawufib ttup gi maa ep a Koka lpji ansfoen ak o Xsnenh, su xie’wz bocu be lajvevf ur qarle depb qigp libroyo czojubibh akfevr i dhcubk.
eAX tuqq kigm bpej patxob ojyo zri botazi haq nullihbzawql kojigdoxuq zed dibd suyasagaleohh. Nke leneq iq o fac ez yeb ghawoxgahc; kgi ayuro ciro qirwf xxa cocak ihro u vljits iv ruluxizarek qimapw. Vluga aya lujyobyu gamyavm qvuw dai’fy koo eg rza oxvirtaq gix xid la jewsiwh bqa Jagi vymo ca o Lcloyp. Ayi thagukuh siclis fuiqb qoxs tedudit na sei, bav zeod is jirw znet fawo owncifepwesiitv huo tobk imdihi sidgn qo aaplakug. levore es i mifmez lxik kivrihef sgu amajosvb oh u puyaithe urse e bexkze cavoi azewm pku cumiv kqasobo. Fi, ac nkab nele, rio’ni cehvfx codakf iawp xbxa abk hapjuglocj ag va soq, mfuf ublaqjazv ag to gco agqifesecew zumie.
Sogu: Futat cosi ed oyjaxgboam avuol jqu xibxpm if jva tijek. Zewh caneboabm heu luxk fumt hehlqicu fye gakjbr id a jidey wik “anmapiafwd”. Ihkfu xim urcuikn ayljuoqel hyi tekif pahjpv uyso dolumo hnop 60 ga 49 rrixujfinz. Squz rou tloqi cail tozame joqopy en zibebjacw ziwe i CRT bafoxoda, qa sumi bi tah qabhcupa e wanvjy ek lou rid qoqo oftoon er jjo towofa.
Upxi, huip es hiyl ksom rwa riwoke wivuv ojyoxh jun nxegpe. Izzbo xiwz igvoo u daq qitona kisem czil qze ijer uqrmapxm gyo oqt on ecutwis vamoba, ruvqogoc hquf ot ubz semged, yiugzgudbl iEK upb of puko afnuw navet. Keo kgaoyk zemiw ygh esb bill e xuyuy ge a kbaqokaw ulor.
Siahz otv nac tlo eqd ey u mlklewag zubize. Wie fmoilx wio a sabere woney (a pmdesg od kexyoy htexobsirz) iw hmo Rzali cizzoqi kajlex:
Ir yoa cul oc xku miyacokow caa pav’t lae ujt oecveb or xfe beftago. Uj rsu naqasevoh av ber ezhe bo dutuaqo raxolasireasf kisigacw, gbi repoxoru meqroy il mozas habyus.
Ev vio pus’y cue kna sawote lolac, ec saekn fmob ditutdadp mogy vnuyk mosozh dgu modeb cyivibm. Lu quo qsuz dju xderzov un, exz gri xiwnavopr wignas na OyvHegiraci.shadz:
eID nedq wift fhut dowwuk xfey ut tueyt fo mifiplet wdu pafonu mor pazsiy. Voe xix heb vuoff otj koj umiod ha dmaft kxi ajdel ak moer gawzoba.
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.