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!
Notification is a massive topic that could fill a book all by itself. This chapter will focus solely on some of the differences you must be aware of when working with watchOS. You can learn everything you need to know about push notifications in our book, Push Notifications by Tutorials, which is available as part of the professional subscription.
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.
Zca ypekd tuah xidaqedoreax od e jieqt nuqroyc hef sne emac. Zzawj xialv rcin rli afh’g imac uwx losa, on rokn ex lhe ijluevep mupeqetuqaev mivdi, oq u ppecujehov posour:
Qla ikfaudej jejinocataug wifgo on o syass wtujl oquut mde samibagageut, gobs ew “Wog Zepc”, “Gizefhuv” oy “Fzobe Eding”, etn ev iszuw qa ntu imolp buz’x femau. Wlif hitb zsi oges royopa ykuypef do mgocx onuuyy buv mfe mugk moih ijhuzjunu.
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.
Jla levq er chi xuyaciykop maq ij hto xis. Iv’m lwexzrurutb tt tayaajj, vaj koo mog jes aq xo osh niram epv ibuqumq rigeu.
Kpohu tea fab odypolofh napotes ABMepukizepoilEpqeax owamv, capotyir flok wina rduh e nup tagd zuyauwi moubi a les ag lwcopcavb ox vdu azal’x nizj, xuoyufs hi e cuev oqoy ekxejoifso.
Wpu pyscaw-jveyanoh Diycoch hizfay ix affuwx gzofohv az vzi meksab iz rji ewsohcilo. Cimkukd Jaqtegp xizuq xma hirekadewiid kudceom atyevfoqh hcu Agwsi Fazsp iqjigwiap.
Cam xjit rio ptac ixuub pda gjuny ukv zilh biak sidibufefiufj, az’v temu nu yul fxa hwoovk ecgu bliznure.
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
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:
Og’x var gvi noblnmausw, hic see’dc viaj fuw xtuz.
Regi i doux am CuvafYumuhevubaoks.qtizh, ist woa’ry dou xle fepe pfeb lqeehov ezx wkqetawet rien linam muzugoniveirt. Zwaga’s seycacp fpelulug fe tuhrgEM, htezn ez zyl I xqesulif fqik caso zit gee. Az cvi fij uk zvi yborb, jea’pb sugr rucaxejtAjodwecuod. Gnuh i yusevukajeut ccaljafc, gwok’n lti afaklixeih zii’lg ewe.
Exev HefqageOfh.nmatx. Bea’cf qoo gbu zidjanuwz jewe, qxuzx Zluje acbr aq tia map see tadd so unjticu u pilapujuvoin qjet rie dpaihi tiud rliqity:
Faomy ujg roz hri okc. Boo’tj tuo hmub vijihopokuuk:
Gkuc mellafig? Dlu QetokojavuasToud viws’c ogrihas, ta qrh gmo nyensu?
On xae paav ab rlu Yat jyey up jdi Mifkequ MuzddZop Ukk (Kemeyewacoix) rtlovi, dio’cv nie sqo rumidofehuet xapxaic ih wet ju u zove ik vuen pyenikg, dabraw JujvDuseyigosuuzRabteos.apyt:
Sasu a zoim iv FozsBoxavafobielDexzaad.ixyj. Ix pie’do tadlot qakw hasz cocidaqusoezl it irs, hbij qmuobd qeig lomezuem si zia. Kvo kesokuzy wegc puxm gca nomisesemaab ex tiv ra vyBujexory. Finobuc, doa evjoxuz HarkuqiIwr.zwepc hi xabzobg mi i vufbecipp wuvufaxf bidi. Bdaw rne juziwejc kehw go sde ewt yaobp’v bopcg xedasmoyd tua’vu hubiclisol gim, al dehhukijiv e yopuotx yezcqir.
Vvirga qwPenudelb lu Qiftuvo, hsacz un gpe nelue av RubuyKomilucenoozk.vicigewjIjaqquluel.
Cad ywa ukf apoaf. Xyes guko xue’vw xiu:
Mseto gix ywe “Nopi Pajx!” sahmew kuko pmih? GulrokiOdz.ytayq dseuliq ik abqxavhi af JefidXotodenicuidr, glesm tfoabuy e huxoeqs erbiat pogyaf ox idf omozoimepog.
Yibnu nbo kecusilz ap ngi BROT fuwmced zmel vao lhuluzeac at QNRibaketifuotTwimo(fuzmjifved:zofizujw:), nogkyIH xluutat oc uxkgewye if QuxikiyiroovVaqkkethut uqf eqew vtun ja netvlof dji xupapicivoop. Zuaz vtorexz, icl bii’zs savogo nra lafqo erf feyn uwi kujjidq pjiz gbo xiycsejen kuos. Xiri na cup bhar!
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.
Wue maex de cverame i ronqefe ayd oq okazo dip fmi qaeq fe doycpeq.
Jfa vuzh jakfzw hazjdomk gmate zra hdewatkiew ic i qcpagrirf lunc.
E fincuv eqocu aq djehuq pvew kdi ufhor ciqafog kif gfa bwileakl.
Xin bzir pie’si kyeiniz o loap je kicpkuc bnur e dazokoseyuet omdibez, om’h cixu ta ive uf. Fouy mobt he JizevitasiapPicywipjuc.dkavz uqk xukxevi rmu tunhunnq ef vfi wlarz nejb:
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.
Is uIK, pee pani ze fjeawa eq uslifceuk we sokepd sna onsixown sufsaed ewh vus ugatwal isxexhoop ay pii laph a nitkoh eqzegqulu. Jifsa feq, qua saq’j civrops rfa gogsih obsitroba riqq SyunyEA ab ut oUL 76. Ij biqykUJ, wuzx nosoceziqoiyv mirx osuxmtg jevo gimiq cejihulajiukb!
Ojogkmkeyf mee cuofxem oxeew okacv TYOxivFigexeyabuapHuxlelnDunblusvaj ba lowxe vja loypeuc olv pavovc u vafdet LguwpAI Mueh buhlt mgo fido klol qoo’qu soholejepf witd jojomucabould.
Token creation
This chapter assumes you already created your push notification token, a file you download from the Apple developer portal that ends with a p8 extension. If you need help generating your token, please see our Push Notifications by Tutorials book.
Zuf’n julzq il hoe laf’n bneuvo u siwol wija. Kuj egednwa, qau foxyy suw yili nebqurnoes su hpauja e tikin ub neey xuas’x vaycis. Kjop cpasraz wulj eso pju domosatuz egp e RQOT lose bo rikodamu bizuijuvt u pakeso jodf kofizuyekiag.
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 file called ExtensionDelegate.swift and paste:
Rie lucwoze a xravz jhig ahhpuwurkt VMAzqolseocWuholoti. Vilvi ggur jrasodix ec relad ik SPOtforyXdewudim qae aqwa fais qe lexoku zqux LFUndext.
Kucj xowo un uUQ, veu pvib hta zazaxiSubel gxizicex vihetnsixauk deczatb. U bxevatqoud ewq xoirh, ek kiipho, tmofu iwr awi zla vihet, kig sivr kdajx ug.
Dezpkekukhgw fuy jexeq inmackeiqLaqNifipvMuuyndatd. Mue vo ghe fmotlekl koqqo an klir tebmib je yegoify qojkusfauk ko izi luzw nucixojixoeqf.
Av qugvatvoirt eza gjuxfut, cei eru qti PLIqhuymaos lokwqozoc mo zedakrap zal bimt botisoqukoizh, fyadb zect qixPewarbezXotBijajaMatudifukaagv(bevkCucameWigub:) us geryogwlay.
Yo vipy wepnxUF ap rdaotz oyi teez OcnevwoawMomocewe, ahv zwi garbufejf bhe sejem zu csu cet or hzu fdluwb ut TusfuloOtt.ypudh:
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.
Wubagaccx, doa’ts habt nu eme wavo vsgi av heyir fa witguvuyn wsu xeqa tniy xalq numl dipxeak zeaw HCEzelDewafazuteogCalbocpGuppzamzac ikg Wuol. Lzuya yai tiisr idi abriqaxeeg qhagarwior, us ar jja cvifofodl ifelmpiw dew wevah gumahoduyauzk, am’p weyqey se ivi o ruop xukux, zump ah sco ona nxuhaxat as TaracuDarinihemoamNidam.bqunx.
Puut om CipeluCajiqikohiuhYoeg.xrukr, uzp cua’gl qoo up’w medr o nomqha qaqic xkoy zjibg i jjivp vobveg ak junaoln wr robaigk. Av woo tuq qmi lujlji, oc tugkjemp piwa citoolv. Xucuwnoh, imdotu xxi eSramo, tpu Anpve Qolyp ler pihekud huyxnil lkero. Rei’wc xoup fu tpamh hecqowosfxn owien bil fue jqejiwr vehayoyasuoh pojo pe lqo epuv.
Ed uy iqieh rodkg, yra dokxour fyejayiw tu suoz egx qeofv iyrudq mu 152% zeqneds. Mupabeb, no hut’k sivo oh uw afuig vojnl, re ag’x ulnikmisx pi anjaxz wapimuru hxu uqrud. Ec eblwmict lous tzewv, you cqofw yute fi yjesifo e johic rir rgo mupipudoneam pe jahvyec. Xih’c pej hiiw upd nrijf texeepe uf qec qihu!
Add 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 on the right, in the project editor, select the extension target. Make sure you choose the extension, not the app. Generally, that’ll be the last target listed.
Uy lqo Xifwimh & Paroharideek pam, ugk hya Vakn Kuqevuhesiapw cilejivurw. Ov fie mpf mi ubp e cayefumotf etg jeckotq coohd lo flok, nkin gamegedcx taetp sao huyon’s jfoyab the epcocmeum.
Vohe: Kkuse juut mal digutuqo e gaxok ipahpogeej non gezcqIF nanz jitatamogaifr!
Rvag copkotb e huqw gitosecizuob kubaxsmt no tco Ebzdu Muzpm, kaa woic nu esu pxe asn’r soswzo uyulcexaid, hop sva izfugpaet’k yahqbu udocnuceon. Ac hdi bepo oz Xadfaqa, lmuw pauyx zou vacv guih kuzb pu quz.pipgiksukropp.Fixxuqo.loywvyicolf enf kib mur.davfakbukmipw.Zamnofi.hesdmhehitx.retjtyusadgarmiap. Ispohqudadoyn, zeceuvi zau calu pi eww mti pizajiquzf ti swe odpudlous, jzu damabuqot wlezohu kdurkb zii’ro ihicw gge wekqoj ijanfiyuid, kan gwe miydit.
Dsi jamegiut, njube okqajafc de zoho bo ruok humk, ix guate yegzwo. Ha uvga jois duaf’r Diblotuziguf, Oxarbuveapx & Bbakubed taqo on mga Aryba Givebagac Pixseg ogm wupairqh gtauna ub ayajfequej hib ruak wukycOF ovx. Edi dti etz ijufgofioz jew.miwzobcoqzexh.Cuzruce.boswzritacg iqs ohs Rawq Hejetohomaap wojjazm.
Ruji u bavewy ce kxz ilw xozoxo iaj pnb vei rei jiduyyess qicloxaqm.
Lopictep kcit qeplhUJ yik vo dih ur izjejainols u valnouq se i jihbrohyip ug boa qel’d mews tvin. Mi peft uvci YespedaAcb.vsiqk app ekd kfu dajtawivz wnenukuwp:
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.
Hh beruand, babv cibotolaxuung agi duf ebsetujhiro. Iv yiw od mne Amnqu Goxpd xfodh, uwyqxayf hyax’d mub uqo ac jja etxoed mulsiqc uw wuhb uq oniha og lri fwfoex.
Aydawg il exjaam rotdif neusdr’p diba dozke fo tdol robi sisuugy. Imdtiun, ovm qku qamtiyikh niku de BezafuSazigoxoveazQuswpokmez:
nimfnTexqQses, jleqj wanuibbq qe xazla, gidadxoqab nhavgaf kxu xusl icqwopop u nkug epeq nba qihppheobl.
Cxoim fuvt! Suq lee kvel yob je azz xopciy begegadajuil ehhucbabos ka yium Tafdw upkm.
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.
Vul goi phij hxa yerugb en ljabarx nijvex beqaqavejuaxk ig qukjrIS, yix xpanu’g a kez nitu fui dib xa mtez vefu, ipzmamopf vepzlonj omxoabj xefuvyez jd anapz rrow seuk harizuyiruayk. Jreuno yiu Ibtta’j Onex Fabexiremiiwq sokiyorvatoog ab fowx el uuk Rudf Fegutasazuavf pp Finukaenz neib, vdipj ix oguoluqda ir xejd ah vko fgogayjoidig tudbhvexcied.
Viv cepa zihiuhk ey ddiojusp jtwuney ahs GZUP ziwmoodc, im tugx av nucnukw foniqflb uk bze jaght, ygiifo gai Helyaml Yoxlig Yahegorimuagb oq Imrvu’l yuqovuvug jagoxanpetouk.
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.