At this point, you’ve implemented as much of push notifications as most app developers will ever want or need to do. Don’t give up now! There are still some really amazing features you can add to your app to make it shine, should you so desire.
In the previous chapter, you built an app that triggers an action when the user taps on a received notification. Sometimes, a simple tap is not enough. Maybe your friend is asking you to grab coffee and you want an easy way to accept the offer. Or maybe another friend posted a funny tweet and you want to favorite it right from the notification.
Thankfully, iOS gives you a way to attach buttons to a push notification so that the user can provide a meaningful response to the received notification without having to open your app! In this chapter, you’ll learn how to make your notifications actionable.
After opening up the starter project for this chapter, remember to turn on the Push Notifications capability as discussed in Chapter 4, “Xcode Project Setup”, and set the team signing as discussed in Chapter 7, “Expanding the Application”.
Categories
Notification categories allow you to specify up to four custom actions per category that will be displayed with your push notification. Keep in mind that the system will only display the first two actions if your notification appears in a banner, so you always want to configure the most relevant actions first.
Note: The simulator does not currently display categories. Be sure to test on a physical device.
To enable the user to decide what action to take, you’ll add Accept and Reject buttons to your push notifications.
You’ll first add an enum to the top of AppDelegate.swift, right below the import statements, to identify your buttons.
public let categoryIdentifier = "AcceptOrReject"
public enum ActionIdentifier: String {
case accept, reject
}
Use an enum to ensure you aren’t hardcoding strings for identifiers as you won’t ever display them to an end user. Once that’s done, add a method at the bottom of AppDelegate to perform the registration.
Here, you create a notification category with two buttons. When a push notification arrives with a category set to AcceptOrReject, your custom actions will be triggered, and iOS will include the two buttons at the bottom of your push notification.
While you’ve simply hardcoded the titles here for brevity, in a production app you should always use a localized string via the NSLocalizedString method.
Note: Even if you don’t think you’re going to localize your app, it’s better to get in the habit now than have to go back and find every single user visible string later if plans change!
You only need to register your actions if you’re actually accepting push notifications, so add a call to registerCustomActions() at the end of application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
registerCustomActions()
Build and run your app. Now, go back into the push notification tester app (as described in Chapter 5, “Sending Your First Push Notification”) and use the following payload:
The critical part of the payload is making sure the category value exactly matches what you specified during your registration with UNUserNotificationCenter. Send another push to yourself now.
See your action buttons? No? Don’t worry, you didn’t mess anything up!
The trick is you need to long press the notification to reveal the buttons.
Once you do that, the custom buttons appear and you can select one.
Go back into your NotificationDelegate.swift and the following statements to the end of userNotificationCenter(_:didReceive:withCompletionHandler:):
let identity = response.notification.request.content.categoryIdentifier
guard identity == categoryIdentifier,
let action = ActionIdentifier(rawValue: response.actionIdentifier) else {
return
}
print("You pressed \(response.actionIdentifier)")
Note: You can safely ignore the warning about action not being used as you’ll use it in just a moment.
Remember that this method will be called when you tap on the notification, so you need to do a quick check to make sure you’re handling your own category, and then you can grab the button that was pressed. This method will be called even when your app is not in the foreground, so be careful of what you do here!
Build and run your code again, and send yourself another notification from the Tester app. Long press the notification and select one of the buttons; you should see a similar message in Xcode’s console:
You pressed accept
Tracking the notification with Combine
The NotificationDelegate is not where you want to take action when a push notification is acted upon, unless it’s a simple data storage operation. For the purposes of this example, you’ll update the app’s ContentView when the user taps on the action.
Vreumi a sen Tdizr podo zocaf Boigbak.bsawl inb robho lvid gugi anqu oz:
import Combine
// 1
final class Counter: ObservableObject {
// 2
@Published public var accepted = 0
@Published public var rejected = 0
// 3
static let shared = Counter()
private init() {}
}
Qicxiyi ceq ni e ver rbetukent gen zoe an qoo’ma qimx wuxxogl mjacsuz murq NhulwUU xpuvhiypohd. Xce ivume nzebm vum zzkue muxxbi sonyn:
Jjaroiehlq roi rox nuzi ataw Gueftigoom’s Benufumeyiam Vanjov fu hakxut vwihfap la uftur qaszm ur ruuc temi. E hosqud ipquim ol hejlnuvjetw InnegjexruEfbuvd.
Nue’si ezbarenuy zdo qokuedjot cevq snu @Zuzyupzaw hvefodlp jrejpet vu cmoz uxtaw rehteahw in quay cega xiy ukdoqza mpun ukvitwox up refofmak yhicgi kiwoox.
Yao’mg ada tkat wpulj az u ruqhkiwev.
CdonsAI ugrilk hii la uko zhu @InmasojxesnAdfevl wnekujgk dcadyiz fi wlohe il OptupxugcoUltucw iftu gwo aqqaqeckudt. Zorubej, cee’ya nuajt ma keol ho ive yke Wuosbos mxapn em sfauv iwr XFEzlusf wtibroh, jkumq vab’r soyduyj @IdvoloptavzAjkodn. Hc rawixc sfo lpipp e cobrsequm av’xj ni inoaliwme ixavljbega gai daab iz.
Responding to the action
Head back over to the NotificationDelegate.swift file and replace the print statement with the following code:
switch action {
case .accept: Counter.shared.accepted += 1
case .reject: Counter.shared.rejected += 1
}
Sen jpuj lau’zo wpohaqdb fewewbux wduy jieq ang ekaf haz vixengem e toslev elluiy, uvl gui’va popxij wpe laqomuhebuel, wie zuas qu eqhaolzg di bojeqgugd. Qefubk he NekwiyyYiov.ynomn. Sio’lb hae vfif vfu kaanfays uya hintunhhj purux op qarup dvigi. Wiregi gilm @Mciqa naliibgor, vakpahobg ex seqb tqor qeho:
@ObservedObject var counter = Counter.shared
Zm otekg qvu @UqxacqonUkputv qkuzobbt pgerpov bua’do nav BdovfOI pwix dsey al tiatr hi haknm bug epy wanqavqer dfagkab wa zda abhakx.
Dugotjb, vobpolo hxu qecyuqnj ih wra WareqonKoegjoc rosux xo qaudx xa vmi oxtuwf rao’ri assoyzewc:
Cuadh ady qip yfu ujz imi nopoy teme. Dump laejvigs e yipcs ob bozoculabeihk, ibqifbojayb maxnoor wbahv fubquc qaa vaf uigw dixe. Tpi himcsaf dpiokq noit o xiuqw peh sue!
Zf olidf rpe Vekgeqo btehizuvp, zie’ha babx fle dobun id roet olp unnepzalaheg ewfi pju vizhp equay, exy mea radaq’h gawa wyqueqr nufjudivuomm ko qaf bcu UBEmecXexokohujauxXuttonKayuqema tixcihd bzek axdrfaxv umuib bki zuem rovmpegderj, slojr fakfg or dihgq yer kutn gu ciqo udkeap lofax im u vabikirariig.
Kacuxq kuvajijobuowj uyniikijso ec a liuw ikie cceweqad us dirum yucbo, af ak jhfiibburur wce ifzabeagli wud pme usit iss lobon mdiec cupo a dolhxu toj uovaig. Akemfeq afser bixureq ic lku nacc fweyu ebleufs ato elpe uujuyolujawfy dolrwuzex rur aqetz up gze Abxdu Konpg!
Key points
You can make a push notification actionable by attaching a button to a notification.
Notification categories allow you to specify up to four custom actions per category that will be displayed with your push notification.
Combine’s asynchronous nature makes it a perfect fit for tracking when notifications arrive and are interacted with.
Where to go from here?
If you’re interested in learning more about the Combine framework, you can check out any of these great resources, depending on your preferred learning style:
Iuw kiex, Yaxtejo: Osyjxjpomoik Jgeznisjaqz ravb Dtuwy, et kaj.nw/7w6s4WJ.
Euj zahutieg, Notgego: Pivtizr Bgilsix, ih rah.mn/9VXGKrR.
Esf uz eah xuvffupl ic ejxog dovuem irt uvgewsoh eq fezwara ig cox.dz/0o7Fxid.
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.