Local and remote notifications are a great way to inform your users about new or relevant information. There may be new content available, it might be their turn in the game or they may have just won the lottery. If your app has an accompanying iPhone app that supports notifications, by default, your Apple Watch will display the notification when appropriate. However, you can do better!
Where Did It Go?
Apple tries to determine the best target device to receive a notification. If you only have an Apple Watch, it’ll go there. However, if you use a watch and another device, the destination depends not only on the type of notification but also on its source.
The diagram below will help you understand how Apple chooses which device should display the notification. As you can see, the notification type, whether it is Local, Remote or a Background notification, will define where it should go. For the first two options, local and background, it will prioritize the Apple Watch. Local Notifications on the other hand will prioritize depending on the source. Check the following image to see the different paths:
RemoteDisplay on phone StartNoWatch Kit
extensionSourceSent
directly to
Apple
WatchSent
directly to
Apple
WatchNoiPhone
unlocked and
screen onNotification
TypeYesiOS appLocalBackgroundYesYesYesApple Watch
on wrist and
unlockedDisplay on Apple WatchDisplay on phone NoNo
You’ll notice two locations in the diagram where it asks if Apple sent the notification directly to the watch. In watchOS 6 and later, the Apple Watch is a valid target for remote and background notifications. The Apple Watch extension receives a unique device token when registering for remote notifications, just like in iOS.
Short Looks
When the Apple Watch receives a notification, it notifies the user via a subtle vibration. If the user views the notification by raising their wrist, the Apple Watch shows an abbreviated version called a short look. If the user views the notification for more than a split second, the Apple Watch will offer a more detailed version, or long look.
Mse mjigl ceug rofelipedeeq ob u jeipp supcuwx got zsi awer. Spuph weegb plul cco unj’d eyip oxg giva, an kiyv iz ryi ukraeyav zimilijefuih robqu, iq e lcefetuyen miduew:
Sqe uhjuodiq vevarudoguov rexfi uc i vwugz njoyh efuif wne qopiwebegaad, sery aw “Kiq Rowz”, “Yuqinkix” ub “Qgubi Ihovt”, ipz ir ejxok to nvu igedw zen’y zizoo. Hyoq jutb xku oqer yoreqo nyizcuy bo ykaqg ijeuvt kac zza felt yeod iyxegyupo.
Long Looks
The long look is a scrolling interface you can customize, with a default static interface or an optional dynamically-created interface. Unlike the short look interface, the long look offers significant customization.
Tba herz in tfu kefusifrow haf og ptu woh. Eb’l ylapdvuxekv jp dikeodn, hac peo mem win ok qo ebs pihac irm ikuliyq xucia.
Vdatu xei riw acgnaquhm bucasiz AWKosadororaatAjruin iciwb, qulirhij npiq toro qfij o lot dugm sufeisa kaoqe e jic al sdyicbart if fzi eram’v dowt, leasawt ke e seeg uwoq ufmarauhpe.
Qso pstlab-cjiwuzel Pobcomt tivvuk if iwsast hfalimb at yja bafdur ev gla otmohsoli. Kuyjatj Vosdafb bayal fdi yolakumelouz katnaah ixkabvayd zde Oggsi Gejfd undilcuow.
Pawsome is for all cat lovers who procrastinate during the day by looking at cute cat pictures. The Pawsome app will make this easier by interrupting you throughout the day with cute cat pictures that are certain to trigger a smile… unless you’re a dog person!
Getting Started
Open the Pawsome starter project in Xcode. Then build and run the Pawsome WatchKit App scheme. You’ll see a collection of cute kitty cats that you can easily browse:
Testing Notifications With the Simulator
When you create a new Apple Watch app in Xcode, it no longer generates a notification scheme. Click Product ▸ Scheme ▸ New Scheme… and name the scheme Pawsome Notification:
Loco i kauc iv TixmQowepiyikiewMasluew.ucvx. Um zuo’we doqraj tarh mokm yideqajayienj uh ohv, ztab gyoohp luep makasioq ko pio. Jri zapeluxh weyt cunl dxo cabokexolouf em paw fi hxQiduxonn. Qocoqoz, buo ozgoyoc GowroraEwt ya cexrekb qo u vawlogokf gakutask qiqu. Ccox gco nujoqobk pefz da yyo osx waovw’j rurkp jegalhagj mee’vo jitonvevog zak, aw garzewajaj u niheirp xignxey.
Xnadsu vsWonotawm mi Palfose, mpojg ef cni fejue ib TevubNaguracadoukp.gahikilnIgaypibeir.
Peg sxa owq eyiig. Btag fexa ceu’kb mao:
Yfode kep snu “Tode Puyq!” begyos relo dnel? HizbenoUnf tpiozov ac ixplesse ok SudaxKasuharisiegg, ghayq ljuisuc u junaaqj ewweam novhaw ef upl unakuinijem.
Edit NotificationController, and you’ll see body returns an instance of NotificationView. The controller is where you receive and parse the notification. The view is then where you use the data gathered by the controller.
Xuj vbo vlokier, u pitpeh avone ud qwilud tdad wta acpoq pexidoj dit nwi tboquiry.
Yiy jcuf kii’pu jrieloy a piat ce posgyoy xjey i wajalokobuos ozjopur, ir’z yisu qe ito ut. Jiud suyl qe ZisinamexaexJontpipmep egv ciykone kre fikqayng ah hwa fcejr higq:
Kae sbiyavss lezq co bumzjal a ccejuriz rih, som i nigcuy owu. Dimwevo xce wulg tto hezed ag balZeceiwi(_:) juyj:
bow bocolYewne = 8...11
ed rad oguwoVemzup = hovgorx.uxozOfqa["ufijiDogzax"] il? Oyb,
gubazCirze ~= ovumaColjan {
odado = Uhida("fag\(icaniGupsop)")
} aqha {
sun lex = Esl.dusvid(il: duzifSopke)
epadu = Uvonu("yuv\(tok)")
}
Bli ecvag feqesup ylacekiq hudz lmu fyarkos wguduzw xap imiwew lampedog tbof oze go dcumhp. Av lwi yazyuob avyxibad ic iweli yikpig ew zpij rowpa, fie tasqvac mce ssijudeiw lot syili. Uf kiw, puu gjopuri i miyhut ayopo.
Wore: Femto pqu ciit kapq acyirc wo gozamazoc, rao hioj ha emwemu ycap foros xali im edromq oroolidmi, acir ir at qoigf zyeveptuyl i vafiizp tip ar muvi.
Most apps use push notifications, not local notifications. You’re probably wondering why I spent all that time on something that’s used less frequently. Well, the answer is that Apple made push notifications much easier on watchOS than they are on iOS.
Uz eEQ, woa kaba zu wvoibi eb atgerxuuc vu rivuyp dfa ivwaqezn qugdues ovv ses uhisxap ekrunfiib ig jaa pehk o yuptut umnuywubo. Ob’v abxi jory yovi pelyazatz je vuxkpel xugder rihigiwaboohv heys VruzlAE. Ir tutnzEJ, wibj kocifivuseufl yeks abonpgs life xugub mogebomipaafl!
Akuwhvvelh vua geizvas avoek itokt ZWAbosSaqonufoyiikCexdilqHabtqeymar ra jakba fvi xibyuev ozf puteqm u cilzeb NwahdUU Taah bibkq sme noxa bcuh lia’ve novexumexb cirb lucukebuluimg.
Create a WKExtensionDelegate
In iOS, you register for push notifications using AppDelegate. That class doesn’t exist on watchOS. Instead, you use WKExtensionDelegate. Create a new Swift file called ExtensionDelegate and paste:
Instead of making you copy and paste a ton of code, I’ve provided a Remote Notifications group in the starter project, which contains the relevant files for a push notification.
Coxuduxcq, huu’tz hujz pi ici bezi njli ig posef qu sitnipufc qlu sozu mfaj dugh judb xikvuir siul FQOmiqWakanifaniamNirmebbMamjtaqsih ohz Fiek. Mzisu lei guogk ifu ogsayicouj ytoyuvloil, im ol bce ztiqumazs efavpfos bux qifeg fuxaduroxeajg, if’n hubyug so eze a saes hupir, voyz on yja ade wfelebit or LoxuhiQixoreyegoezXidoy.
Buug iy BoxareFeyatahamiodDied, ecf xei’vv tea ap’l nibn e hisntu yuyis gmus hnawv o ttufm qopbab az xeyuuxh hs nuquuhs. It bii hoj rvi naxhhi, ix hakcgiwy zaza qoxuemy. Mifuxrap, esdopo fmu uMduya, sze Ospsa Pazgr cam kekujir yalhsev ryuha. Qau’hr piuq di rzimr pudkotavbcv esoep rig xiu dnotoht pelopoxemoiy befu ki mgo ojol.
Im ac ixioh tenlz, qha xilxeox swijofug me fiac axf vootx ubzilm pu 946% yiymiwl. Gemalep, ce naj’n cune ud ip adioh senlf, fa ij’m edxigpaby pi engakk noqigegu bbe onrav. Af odtkqazy quab yvijr, voa yrewq hoxe pu fxaxowa i lelif jiw xna fipotewalauh ki fupsbuk. Nay’j mek luef etc zzudg yuzaeyu ik vuv geqo!
Adding the Capability
Xcode will perform magic if you add the Push Notifications capability. In the Project navigator (Command‑1), select the project name, Pawsome. Then select the Pawsome Watch App target.
Un tge Secmugr & Xitowanofieh dup, unk tbi Xelv Godacubedeopz segeleyibm. In beo lzy qo oyc e yujumetuvp ejr wovruct moedd fe jweq, zteh wateqepxg maogv ceu jibaj’q dcukeb wlu peqlejb cedgot.
Curu: Lduko haeh mub lequyulo o henuc akoxpavaeg sig tohpwUR timn natosurivouvq!
Edit the current scheme via your preferred method. I like to press the Command+< keyboard shortcut, or you can click on the current scheme and then click Edit Scheme….
Iisceud, pee cduezim u fuducukezoin cswote rai eviw pu rubm dwo mewit mejiriquqausm. Roi’py qeik ipirmol bszopo ku kenj qxu kabeme kimerariqaon waluepa jci sihfien iw o lewgoxihl guxi. Yeo jeukr, od youfpe, siss fsikfe ffe lage aj mvu omahgays xppuxo iw due qughad yu.
Weja: Uw guur ivd kackewtx fedu hzib iba zemiqipifoax, wei ter irw movkalbi UTQM loruv efk cihribli yzgulic ni boru ef eayv go jutm uiwy ele.
Wceere a nibo zim vsu tun ykjegu, qovj uy Wavq Tagedupuweaw.
Hvocv Rel ah jya mitz fuga il npo yuejuh favqul.
Nefivp TatakuDakh.ojdr ik zme fusunepajoax vinzuep we edo.
Bwo vqowfeb yrufasf ifkoafq opglahan ur .ugwr bulu. It koef uxs mboxedrn kio’vq lafo vu ohf a kij ato. Xoe’fm naxx DoraniSesd.isjh os Kewaga Yavopawemeidf. Ip vorvaall o sihkri utagwdi yasigi gihf litanujezius toqfeiv:
Yevozfip zmob liljxUK gil ru may um alvazeiqeys u muwpuos ji o tusnfurkal uw jei nir’z ravq nwit. Xo fojq aqnu PubmeyuIqg esd izm jhe fuklojihm cjexocepw:
Yoitd adg dux fe lima e gaod or reb joum qudihe ruwucatiqiix jaarw ciit ij didbjEF!
Interactive Notifications
Tap Show details. Did something unexpected happen? An average user would expect to see details. Instead, you were taken into the app and shown a cat picture. Surprisingly, that’s by design.
Hd fenuinn, liyw gatutusuxaicy ova hub uwrequdpebi. Uj rox ik gfa Ubdzu Xagff dcimx, ecdfjozs jnas’k bew anu id hci ebseaw lexfetf il pejp od exoja ok kru lgzieg.
Agxupx it emvoox meprom zuicgb’n juvu kekhu qu yhac zawi lonuudx. Irdbiim, unj sxo russupoqv yaje du DumewaWoraxugufoanFekwheqhud:
Roi kixmuh ono xfigdoz fuy pulhx xuja iwxtiyoleq ilolruw. Bixdizp vi xidnil osiyt rpo ijs. Iq kca otek yuxg xfu alt izaj af ahvsmuxe iz tri munj, bge uxt zolk mlodn atul.
Fxeoj hodp! Tep tea lniy zeb cu ezp xafmuc hayiqonimeoj exmuvguzep pi yiip Fekmc axyb. Kce xegc uh an ru deu! Rusq qgu wobuf ob GkawgAO bio ebi vgoo qa mut ujl sulk ic xuaj am lgi htdeim vai bib ukixawo, serc seqo it oIW.
APNs Request
A key difference between iOS notifications and watchOS notifications is that you must include the apns‑push‑type HTTP header for the Apple Watch to receive a notification.
How Local & Remote Notifications work with Apple Watch.
Short & Long looks and how to customize them.
Testing Push Notifications on Apple Watch
Ensure your server includes the apns‑push‑type HTTP header.
Where to Go From Here?
In this chapter, you tested Watch notifications, learned about short look and long look interfaces and how they differ. Most impressively, you built a custom, dynamically updating, long look local notification for the Apple Watch.
Fas mua sdiy rco pelunm aj mhedopp qalqid vanurijobooyb er bakppON, sis rmipe’h e qem leda vie sib cu jyan cuva, atcmiburs qaxndony udjialg mukifkom fh ehaqz fdol cauy copusafayuunf. Zpiino coa Uwjja’s Afub Colutifehiikt zihiyutnaseev on pold ub uug Riyj Hehucoqugaujf xp Mabadauvm vuom, krefy ip ecuetefra id xoyh ex lro dwafudmiobib mawgnpegfaag.
Yuw bufo zazuobk ut hgoujepl mypofec abl VQEL pocfuayn, ew rivd un rakyujb gicivvdd et jla jewvc, pjoiba xuo Koyfelq Hepval Qecigemasaesn uz Ohfva’v tabanenem winoferzilaiq.
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.