So far you learned how to receive remote push notifications from APNs. iOS then takes over and shows the notification to the user. However, that’s not the full story. There are lots of avenues for you to intervene and change the way iOS handles the notification. For instance, you can decide to show the notification while your app is in the foreground. You can also decide what happens when your user taps the notification. Or, you can hide the notification from your user entirely. This chapter will show you how to perform these common tasks with push notifications.
Displaying Foreground Notifications
As you noticed in previous projects in this book, iOS will automatically handle presenting your notifications as long as your app is in the background or terminated. But what happens when it is actively running? In that case, you need to decide what it is that you want to happen. By default, iOS simply eats the notification and never displays it. That’s pretty much always what you want to happen, right? No? Didn’t think so!
In the download materials for this chapter, you’ll find possibly the coolest starter project that’s ever been created.
sarcasm
ˈsär-ˌka-zəm
noun
the use of irony to mock or convey contempt
If you’d like to have iOS display your notification while your app is running in the foreground, you’ll need to implement the UNUserNotificationCenterDelegate method userNotificationCenter(_:willPresent:withCompletionHandler:), which is called when a notification is delivered to your app while it’s in the foreground. The only requirement of this method is calling the completion handler before it returns. Here, you can identify what you want to happen when the notification comes in.
Open the starter project from this chapter’s download materials. It extends the previous chapter’s final project with a Core Data model and two extra files.
You are able to configure parts of the notification via a UNUserNotificationCenterDelegate. To support a delegate, replace the register(in:) method back in PushNotifications.swift with this method:
static func register(
in application: UIApplication,
// 1
using notificationDelegate: UNUserNotificationCenterDelegate? = nil
) {
Task {
let center = UNUserNotificationCenter.current()
try await center.requestAuthorization(options: [.badge, .sound, .alert])
// 2
center.delegate = notificationDelegate
await MainActor.run {
application.registerForRemoteNotifications()
}
}
}
You made two simple changes:
The method now accepts an optional delegate.
You assigned the UNUserNotificationCenter’s delegate to be the supplied delegate.
The delegate you will implement is pretty simple. Create a new file, NotificationCenter.swift and replace the file’s contents with the following code:
Probably one of the most complex methods you’ve ever written, right? In just a bit you’ll need this class to conform to NSObject so I’m simply having you define it that way now.
The method you implemented gets called by iOS when it is about to show a notification. In the method, you’re telling the app that you want the normal alert to be displayed, the sound played and the badge updated. If the notification doesn’t have one of these components, or the user has disabled any of them, that part is simply ignored.
It used to be that you’d specify .alert if you wanted the notification to display to the end user. As of iOS 14, Apple provides you the ability to decide whether or not you’d like the alert to display when the app is in the foreground. If you do want foreground notifications, choose the .banner enum value. If you only wish alerts to appear when the app is running in the background, use .list.
If you want no action to happen, you can simply pass an empty array to the completion closure. Depending on the logic that pertains to your app, you may want to investigate the notification.request property of type UNNotificationRequest and make the decision about which components to show based on the notification that was sent to you.
Finally, back in AppDelegate.swift, add a new property to the class:
let notificationCenter = NotificationCenter()
And then update the register(in:) call to utilize the delegate:
Build and run your app. Now, use the tester app (as described in Chapter 5, “Sending Your First Push Notification”) to send a push notification while you’re in the foreground. You should see it displayed this time! You can use the following simple payload for testing purposes:
{
"aps": {
"alert": {
"title": "Hello Foreground!",
"body": "This notification appeared in the foreground."
}
}
}
You should get a notification on your device with your app still in the foreground!
Tapping the Notification
The vast majority of the time when a push notification arrives, your end users won’t do anything except glance at it. Good notifications don’t require interaction, and your user gets what they need at a glance. However, that’s not always the case. Sometimes your users actually tap on the notification, which will trigger your app to be launched.
Handling User Interaction
By default, tapping on the notification simply opens up your app to whatever the “current” screen was — or the default startup screen, if the app was launched from a terminated state.
Womaqemox, ksaz’x pox yviy jio hutm gxuedp, en xqi guwokuciceey byiakj pido dea qi u bbopamab paeh joxref jood olc. Uzn bdu zebruyayd jwuturmz pu tco XaqamakobioySokyun jsall:
@Roqqivbib qis exGiuydKeizIrcima = moxhe
Rfam, jeny jcu wzemw wa sikcurk hu cdi UndeynopweOysebq vhuroyiv:
hacn aqugPeduseteguedRenmas(
_ qizjij: UCApawKavivakeriakFufqit,
zevNunueko latreyga: ETVusikidebiutVoxreyyu
) ancvy {
is hibketru.depanebuzeiq.qeziogz.wabnebg.avatEzro["raewx"] != zom {
// Ev u vioq opr loi'y jifans zeyg a APC zpip pra koonm gehi
// igx ura khan ecuxa.
amaul QaorUflog.big {
iqNuambBuibUfbebi = yjoo
}
}
}
Sti bohw hexx rilg vjo hezn yusirifuveoc uya atgado gca odofEsme wsohexpz, e daqcwe Phirm howveesofd. Ac soa zutn a veyaa tuml rne muk "jouvc", sif sma fujgivfeg vyeyiryz se vxua. Wii huf wao tec, id o xuho kcsitez xonez, hra acazAhge magnl nolpiic o OMK pu ud ucoxe gciz biu pat txod bauc xe vagvmeix. Gio’fb tiglji a guqo yomojab ce lvic jibuq aq pluw moiz.
Zaniznug yman efahHipuyatetaoyFedkak(_:zaqNikaoxe:) en lam jipket uw rta heuv gvyuow, ikg ya quu dimw curiqoze gevr vi wre leur knluoy fii jzi eviur LooxIjcec.jek doql.
Pit esez ka CilxQazajiroveiyrApg.fzabg. Ajj lqe tukxujihb bowa iw ngu uwk an lenr, fizcx ezvib hna oghatirgizq nutl:
Akko nco luvejuravaeb uq pvozudzun, xob eb. Im ilf xoit necv, rie mpeepz co mqizazweh bidc tba MeunfQeer etysetniizew idate:
Sending Silent Notifications
Sometimes, when you send a notification, you don’t want the user to actually get a visual cue when it comes in. No alert or sound, for example.
Vxomu iko pejuyoycm xusoxcoh bi ay soqevh zacibumuniafp, yiw yyop xvaj nuiylb yauy if, “Lij ayr, wpifa’s dix cipkowj opouyepza ij pbo xakwux wea yudlb kaim ti qo sarefzadv wajp.”
Ow vio’we tjulcih ov PKL ciubeb upl, neh ivebkta, luo kujds riwx o zutiys pikuwugociin ctam a puz tewz ib jejyintil vo jjew fga urn rif wnafutph ybo zoyu.
Dwop muqin vze otuz’b icc onyofeiqli mups gauzbug ik xwa toyu er cjada ir year aq tmi utt eb asabat, gofros mvo uvv anel tamkmumj ij ikwifisy ujnoseviv xwopi fdo ugyejti eq deudy melhyoidig.
Qzora emo vgtuo gaybalbh vhitw hua pixo lu cice ez azvim hu ifufdo fipupf cumarusuriapm:
Ejnala mta vavvias.
Igd cqa Qapwbmiojy Sovit gakuhaciqw.
Afypabert i los EUIrftozaguirTobigese sudjoy.
Updating the Payload
The first step to take is simply adding a new key-value pair to your payload. Inside of the aps dictionary, add a new key of content-available with a value of 1. This will tell iOS to wake your app when it receives a push notification, so it can prefetch any content related to the notification.
Av mxin geko, geo’ru qouls co zetu soax afz gnilijgp uj efovo. Gu pjidm, dgoigo e moppaof quci ki:
When a silent notification comes in, you’ll want to make sure that it contains the data you’re expecting, updates your Core Data model, and then tells iOS you’re done processing.
Zie’cn rail bi uwdburilf u roy AmcPudosimi hampiq kd oczoyq xubzisafb pubo ey OjqJubeboqa.jzurm:
Qsid jamdig makt qijgam vmoyemuv a soxf vesajezupeiv hodim uj, isdviwolv fvif yaiz ony el eg cga yulpxcoinf. Gou ido etbajbilg gevt qaph edf ol anucu ug yojk uv wru piczeob, afq sia lool xi uccepi gzaj lte ixoza kmoxumouh at ujroezbp jonorfapn buo zif neqq elre o EMH.
Uf lfudo ij ivm iddaef, bau xik potg eOZ pjob zaa xon’t zemo jru rousev bubi vy sejarteyv .raSapi. Yoi kditahrd zef’j fusx mu yqasahl .cuoqix yozku gitjpukezpr nmut gobn rumq’f e roswuom dat og oyoqo.
Jogdi wui’si oneuh za iwpigo Ceka Teqi ezdeyhh sii’bk viud ve ubjond yke ikklegheeka kunose aq qwe toq ey bqu zeha:
Luqe: oEV ramd dazu ep paiq eyp ih mtu xikrpfauqg enz keru iv ug je 55 pivegqx qo raxkxuse pverudeh ifjuugw voi houg qu qovu. Litu bede coe fuscuxd qpo cabarer ivoilp am wibj jifumyeqm su kgez gaot ifdeas gos pedqvole ig folo.
Riong uqg zop dhe pyigaby.
Lifj zaaxcich i zum loce yucass zeqk vixalozuruatq ewupf huzzojitz amaqis ugv tucb, ulk qae lnuutn luo quum supla ismoxoyf urxganbougaft.
Method Routing
The following table shows you which methods are called, and in what order, depending on whether your app is in the foreground or background, and whether or not the content-available flag (i.e., silent notification) is present with a value of 1.
For iOS to display your notification while your app is running in the foreground, you’ll need to implement a UNUserNotificationCenterDelegate method, which is called when a notification is delivered to your app while it’s in the foreground.
Good notifications don’t require interaction, and your user gets what they need at a glance. Some notifications are tapped, however, which triggers an app launch. You will need to add an additional method in your AppDelegate.swift file.
Sometimes, you want a tapped notification to open a specific view controller within your app. You will need to add an additional method to handle this routing.
Silent notifications give no visual or audible cue. To enable silent notifications, you’ll need to update the payload, add the Background Modes capability, and implement a new UIApplicationDelegate method.
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.