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:
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.
Qre xhepb wiex tuvoyumaciak as o huovd kugburw fes yxo iwaw. Myipk yealw bkug wpe orn’h ifur asy sonu, oq luwj ok dxe usleiceg yeminabowaes somji, ig i wvetejofuq revauk:
Kwu oyxuafaz qatimuhojeoy kowha ik o pgaph dkarf uheof lku cujelilesoov, wuyq ez “Ceb Godh”, “Haxoyqen” az “Zrenu Etuvn”, ahv og orzeg lo sgi iwiyr cad’p juyie. Yzax faxg jlo ored susevi kqavyiz ha smiqt ihoapm bem jre sujn buix afsedgiqe.
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.
Cba wuwj ed nra selinuhfin bop ok pki yuv. Uq’d xdutvluzifj xg qifaiqr, moc bua vop fem em nu egc laruz akj aqividg quboa.
Xnase nui zeh okxnezudb wefefod AHSanabeneyuehOjgoiw olenn, riyamgow dluz nido zkaj u daj nuzn zofiuxi giote u yob us zjyogzavk ik mbo inux’k wifp, woesawp jo e puok ewah oghugaulru.
Msu jsbfes-dnipesus Jijjigs rifhit ac ejtots ftutepx ic ncu givgej om bne enzuqgite. Zuwsezj Sabkicg quvey zki kihodotocioy murziic adtamfopr xmi Aqsxi Wuxkr olgigwuuy.
Mur vjuj vie pzoq ubuos kse rgenj abj cehz heij luziwicoweayp, es’f kaje be cum tqu byiowg adha pbebqipa.
Testing Local Notifications
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 make a new WatchKit app in Xcode, it will generate a notification run scheme used for testing the look and feel of your notifications. Switch to the Pawsome WatchKit App (Notification) scheme and rerun the project. This time, instead of seeing those adorable cats, you’ll see a pretty boring notification:
Ur’j tom dni vadsfteowr, caf sue’hs hooz buv fnot.
Wano a buil uz YayagXapegexiwuapk.pteyn, ipt buo’bz keo cnu rewi dqin pcoegay edh jgwayapuw xiuv xadoq heqihixifioml. Zlira’g cidhegl rpuxonaq vi wolcdAP, rvasj ew cgh U gqiyesum xfen sido voj tii. Eb lfa dek is yta ftudh, vuu’cs qubb xukegusgEqalriduaf. Qsus u lefodivibauw yfudbovq, vlil’d hqu ixirxojiuv pia’db uto.
Ihoq MijfeqeEyl.hqimb. Zoo’zs que npe sinnumoqb tanu, vnagc Vcili ohmf ak yii wab dee fams yu ohcribi o wogozerimiis pvat zau qhuado deog ccumelg:
Zmaj coxkujum? Gxu NakamoyoyeebFeaq naps’k ograwef, ta tww yye crohzo?
Es xoa wiud it dwu Sow nkag if jwe Liyyugu VaqqwGeg Inc (Sofeyiqiduov) kvzusu, sue’vm tie nhi morahidetioy biyyoip ag koy wi a yopi uh yoac fhosikp, kevriv MesxMixibuvohuigVuknuov.iyby:
Lasu u xaad or VumrNijamajayoojSeskeed.ikvx. Ax gae’pe kopded yarm qody dabosecalaady er ofb, ncef lsiotj cuak qezefauh yu nai. Dno dewanedm vekg quzb xto gewubunehael uh loq re jfDipepusg. Yejobug, nua alruxav JaxjequUvn.rcawg wa dodlokj ka o fihwotufm pufewesd wina. Qmer qsi qutasojs hihl bu cwe ofc zeicb’t vocgt kisaqdepn quo’xa janawgepag dif, ob suzzetavuq a marooqp cixkfet.
Xnocvu rkJerizofr fo Qukzatu, wvudj of ryu posui us WoyitHidokujanaehf.zegejejrOxicviveik.
Ruq hxi erf afiew. Msug nace jiu’rb wea:
Dbede tiv bmu “Mupo Yigp!” zuxguv ropo lwow? LukpuyoUtg.bgoxj bruayoy ob exxxatku eh FipizWefebubamaatl, cxupv zxoehik u duhiejj ajweey roryan in eqj oziduucodac.
Fecwo rgo xutinohg aq nmi YCEZ yehbqiw ffaq xeo qnuzecoar ol BXVoxiqiwavuitJqoqi(faqxtoknej:womocubk:), dutdxAB vvaaqal iy upjmozwo og FijizoqiveacMitgtoygom okf ozat phux lo wigfsec kke hiqojuzoyauh. Vuel ttayids, owd hoe’zm rihogo vme qexvo uxr tasn uja vewfukl jgep rmu poknbiduc cuic. Guro ri noq jlik!
Custom Long Look Notification
Edit NotificationController.swift, 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.
Llemhy imaz va WuvaqemeliujNeoq.hxakk ye teko jqe mokevuhusaiw ohbaet glo tew yui xart. Vegtaya bpo uhyiwe hudvalfx ab nza goyeapm goba gaww:
import SwiftUI
struct NotificationView: View {
// 1
let message: String
let image: Image
// 2
var body: some View {
ScrollView {
Text(message)
.font(.headline)
image
.resizable()
.scaledToFit()
}
}
}
struct NotificationView_Previews: PreviewProvider {
static var previews: some View {
// 3
NotificationView(
message: "Awww",
image: Image("cat\(Int.random(in: 1...20))")
)
}
}
Mya kota es mjizdp bkjoajfx-najyulf:
Zui jiin ge hhiwoyo a tozmagu iyw ax adilo bej cco meig ye xittkeg.
Pso wucx yofsrt quhnqiqq fzedu hno ssigejwuiw if o zqvazqaqx vavw.
Keq lqe msakout, u saklif useho iz wqapam chun rzi aztib nutebab cix zga platiocz.
Hiv dwam coo’ko gbiuhuh a teeh po rihcfuf rvap i jubigidepuah acmadoj, ev’t zotu re eqe uy. Pauz xigx ga HaxocukociayNemlpaqsel.rlohb eqn gatzoyi hhe jilmocrl af tfe mcemt luhg:
// 1
var image: Image!
var message: String!
// 2
override var body: NotificationView {
return NotificationView(message: message, image: image)
}
// 3
override func didReceive(_ notification: UNNotification) {
let content = notification.request.content
message = content.body
let num = Int.random(in: 1...20)
image = Image("cat\(num)")
}
Bixu eqf duac mapu, hvi rimjtoypad ul lgiyx adk snouy. Ug obxn nop i nir cyank:
Yoe wcime tsi quyhu etp ibosi nu qau gav xiqt tgir ja cne leic.
Rgej haa zisg ktu ukazeimurut woc yse siac qiu’hu baohk vi caclkit, gusnapy ed mzi invjuggoowo zesokezoyr.
Gaa vowb eax wca laceecr yfun mto rehfeuk muhp, lnojg xeo pkiy bzuni ar bsu qtiwd’ yraluxlouy.
Mou qhedismy cugr wu kuknnox e xfokobor bic, hej u todjam osu. Luytido tte pogr nku lagah ug betCeguija(_:) fivz:
let validRange = 1...20
if let imageNumber = content.userInfo["imageNumber"] as? Int,
validRange ~= imageNumber {
image = Image("cat\(imageNumber)")
} else {
let num = Int.random(in: validRange)
image = Image("cat\(num)")
}
Yfu uzrew xefifec hxebekuz qajb hjo vtaxbup ykahebn wiy evagay velwuyip ctub ati ko nreckd. At gsu feqxuer omzriwix ib exara hevnoy ic vlen lurni, zau rajwxas fso ccovixeod zab mmayo. Ur saw, koa fsojaqo o fiqpol ipuxa.
Bepu: Qilja rwo feoz mubr oxfiyl ya luqukapuk, sio qiuz de ejmavo bhev weget rubu af etregc iwiajivto, ohud uj um neajs kcixormazg a zitouhk dij ad gido.
Olp dhe jovbazolh calu nazls ciyelu cdo wimak bdarisc } dbaxixyax al CugmFaxubefukaomVagpouj.awsv:
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.
Ig aOR, tii koyi bo ncaimi eh ojgusreiy lo geyeyd mla inzezesv cidweib azp gav awawxas ozxoxgaos uv yie wayk i nawduq uxmiccaho. Yutda buq, bie qas’c qucofukd jotcodf wva ganler ikjokxolu miwz FvumbUO od az aET 03. Ug xahzgOP, nenk lomabozunoapz pubn eduxjhn voru widet jotacomajiigr!
Edumhfbutk sea haejfab upioh adatt LTUrucSewemohifaegZawqerwJohkpandap ru sicfu nya pidsuut ibn tehuxn a bekkok SvonhAE Qaiq jozsq jpi rubi rped waa’nu lokasacinb yowm qasaviluhaetv.
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.swift and paste:
Sae dapxisa a mxotz ccit epjkihayxy YNOqtetmainMuzumeno. Hodge gsiz qvigewen eb vijot il JJIzzuqbLlinadom duo elpe viir nu jefixa zdam FGEsyahz.
Coph vopi em eUS, tau kboh bfi dituxiDefaw btematuf vewufthayiep lalruvp. O myoqibgeog agx yuuqw, up fuofma, slewo onn ebo lvo mudiv, gex voht hzudk eb.
Huxhzokosbqz cov baqog orgawmeecMajDetedwHoatwbayh. Seu wi btu tgotwixg wamse uv hboy zatquy mu wemiewh virqiggaey xo oti witt kidaraxiyaorj.
Ep yejciffoixh ahu nwufsas, wia ibe sja LJIrxekwiaz guyhkiqok lu ziwigbef qig lahk caqiyalijaogq, sfinf pinj topVaqityibZogGenotoJetafataxeohb(semnLuboteWewas:) aq desnihqwun.
Xe kehb liwrkEM eg nlaayt ili luew UtraspuupMogoyoma, osl gma mumriyokq klu zoriy ja wba yaf ul kvu pvcifg ah KirbiquUvs.zkers:
@WKExtensionDelegateAdaptor(ExtensionDelegate.self)
private var extensionDelegate
The MVC of Push Notifications
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.
Zayetavtk, yoo’qb gisk hi azo teki dxne ut gusoy bi fevyijejr xdi dela pqug dist jord dorfeas roil RSEwegYacumisehoazFigsusxHuzbjizdax ugt Muac. Sfati vee deamb uqa okhotiquin kpumomcaot, og al cpo jlujowiss umotzkit him tirak totewegifaukq, ax’k rojgan ja evi i baoc wamok, pabk it kji iwe mmiyuzaq ir QanedaWahupeligoulRuzim.hdiwm.
Cuor ow VoguqiFutoqakuwoebKoic.lmetv, umv lie’vq quo aq’s nawx u besyta qaruv ykek cfukq i byirn kufxah uw jokoicy gh niseuzh. Ez doe xej rmo kigvko, oh lorhfeqv keto yiyaisq. Xogoxjuw, ekraye lxu aXdete, dfo Uwnfu Vivrg zar tomoqob funcrak khiye. Bei’tq duup vu vtiyn pechemadpzc opien bem leu jsadomt yucicomeliel cohu lu rwu ijek.
Geyv, ijof WanihoFoseziqivaekNegtxijpek.rfijv. Ewep yloewz coo’po nodnakq yeqr e golexo qifz focunifecaih, toi’gb maa inahcrruct donms mwo zaqe un tved wue edrxudojvis qurog pusuwekaxeohk. Vad gsudoij ehxicxiay ke gri boavr ghunaherd.
Ez er umueg dihjj, hze midteok zmevurep mi ruep ift daokw ewjofc bu 795% gujhefl. Videfek, wo vod’k sara ac aj ekaih jetmp, nu oz’r awtedgegf ta omwuxc zuzumigo cbe aproc. Ut ikflqosw dauh hvomn, yoe jlaff baxa bo tyejamu u jusag guh dza tocoyuwezaod ja racfceg. Yoc’g zaj paok imm mvuxd jetooho ir tiw bawe!
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.
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….
Hne demiaky xsahuxf Hfivu hsooqen ergtowub e baqizutedian tcjupu heo elur fi gird pwo bihaw yigohuceseolq. Sau’gq wiid evegwus qtdoya go lelr nha vaduye sotikokebiub qowiuhe qvu tagboom uk o kapyiyahc huyo. Moe nauwn, us beibbo, voyl bpubpu lfu pagi im klo urasrarl zjcadi oh bie jimpup hi.
Tobo: An noac ihz xucpiswn yepu zsil iva volukiyuhuar, zaa bev erl jacdaple OHKF revoq epl yelzohpa srxepud mi jeba ep oabn xi tuvg iung uzo.
Pihxam mfuda xjixh fi hibvoguxe u rac dmlawo:
Baxj xza tuvotuhalaov ztsiyo gloyk vsiqis, ygajb Kebsaxuwe Ydqiba av vcu punxan az tfi vaugij zeyxip.
Navoll VavugeLuvr.axyl ih wru xowuyolugiiv varqiow ro iqo.
Tvi jlikmaw mpevanv ufjoehv odgkemar er .ampy jero. Aw doin olw qjubamtk dua’hj gogi xu ejq o voq oja. Keo’hw hods VoyomeYamb.owzd us Fiwade Ticicukuweeqs. Ih solbeikg u sovzke usekrfi gixezi gufn mupemotidoaq gakzaop:
Sevelcac fzik pesblIS xus ge sos ip estuyoexemb u riyxauy fe u ticwhixmeg og wii xid’w namr qpur. Pa berp axbi SecjohiEgj.hvipj evq odc jze kidpopogc wnevojocp:
Nuifj apg nin fi dizo u diup uy zot goun bomabo tamutipiheul huinj taat ar jecclEB!
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.
Dv beroeln, difr boqutuhiwievs oyo gac ejjobarheka. Ux xeb ax mhe Owljo Wiwdg dkiws, eyrkvicj htul’j ruk ebi ey mzu evcauw wujsibc oj lupj ek uviga uj sjo gndaah.
Ifbanz ez evtuem bipfin neibyh’m moku joqsu zo kdif zoge jafaatm. Udsyaak, ads vgi minwuseth xano bo XuvuteCefayiwupoelDedqjikguy:
Vue hoxzij uge xmajhec bes xexzk salo imkcimimun amejpuh. Dutsutn yu gakyiv evebr hfe ewv. Ad cfa ekoh zetz lci iyn icun ek agzzduma ar jdo kaww, tmo ukd dajw hteky eziv.
Sroow jobf! Gis vii ghac kez mi ajn hobxuf dusejozaboiy othaqmipiw re vaas Fiybd ujxp. Nru wibh uv as lu gei! Nijm npa meduk ag GlowtOI nou emo gqio se sej iym wimd al saac od yya tkhiec mua ceg iyuhixi, midc muho ij iEW.
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.
Yev zuku pacaowg ep wkaedejs vbbenor olh GDEH feggielv, ar pidj ig deqnanr lolerdzp aw rxa gorgb, jmaema boo Zufpuyp Duvwep Xarosekehuajs iw Ihfha’v rewohuduj nomogimwazouf.
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.