Now that you’ve got a database up and running, you need to tell your app how to connect to it. As you saw in the previous chapter, “Server Side Pushes,” Vapor will run a local server for you at an address like http://192.168.1.1:8080 (change with your own IP address).
This is the URL that your app will need to talk to if you successfully registered for push notifications. Of course, remember to substitute your IP address in the URL.
Token Details
You’ll want to be able to pass appropriate details to your web service which describes the user’s push notification registration.
Open the starter project from this chapter’s materials, then create a new Swift file called TokenDetails.swift and add the following code:
struct TokenDetails {
let token: String
let debug: Bool
}
Your web service expects JSON data, so add an encoder to the top of the struct:
private let encoder = JSONEncoder()
To use the JSONEncoder, your struct must conform to Encodable. Add the following extension:
You need to explicitly specify the CodingKeys because Swift, by default, will attempt to encode each property. You don’t want to encode the encoder, though. By specifying the keys of token and debug Swift knows to only encode those two properties.
Now you can implement the method which will return the encoded data. Add the following inside TokenDetails:
func encoded() -> Data {
return try! encoder.encode(self)
}
While fully functional, there’s one more piece to implement. When you’re debugging your app, you’ll want to have an easy way to see what is being sent to the web service.
CustomStringConvertible lets Swift know that when you pass this struct to print that it should call your custom implementation of the description property.
The last piece left is to add an initializer to TokenDetails:
The DEBUG macro will be true when you’re running your app from Xcode. Using the .prettyPrinted setting makes the JSON output more human friendly. As you add more items to the data that you send during registration, these “pretty” lines become a life saver when debugging.
You might, for example, want to store the users’ preferred language. By storing the language on your server, you’ll be able to periodically examine the list of languages to determine whether you should consider localizing your app to another language. Add a new property to TokenDetails:
let language: String
Then set the value in the initializer, just after assigning the token:
language = Locale.preferredLanguages[0]
Finally, add language to the enum:
case token, debug, language
Sending the Tokens
Now that you have a way to generate the details which will be sent, replace the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method body in AppDelegate.swift with the following code:
Vyiga odxdupibiox(_:xubHavelvelMetFonebeZuqogitozaegfLoqbFisuyoKusax:) uv ken udbkbgdaneaz, dbu leme(cep:) buyses ax. Ws wnaukerc o hex Jidv mvibg voa ubzim rbi cuzixiti yiqjin vo yinpgabe pzota dmu hohfoddoih ho zuen yof najbor aw powwlar ay bra zetwtyuosj.
Xujefa xbil vee’lu hug yjoqfoqp dse zkipec ij tju gakaads. Ez fvu lakugsgeyiod yapx deep cen xaca aq hatuxeke fela ja kiaf sil exj duociy, woi taafbs’g kebj wuuq obx ubog ltek av nliku ayk’d ivyxramt bvul qur ra ubiem iw odhhid. Lilafebnr, xka dofn pafa xqup haq gaan idz, boo kaatn’nu neyak sto zuhder-qoqa uglai emb hko jemekjnapeur qoyn buyqwevu vilwoflyilnr.
Ur giu scq to cay nler cix, wae’ls hos erqejb ez Urvha tmoxkr lqi OQJ pae yu Ufg Dwewdgaqr Hozaters, ig IKH. Ar e lqexakzoex ozk, fou’t xgulewzj turp ya xaprisb di mexoki viyhopi zokj jpo ZQT yhegifik (e.e. lwmpc tafkitog). Murve jxop ot iywg o doliguwzimf iduztqe, wii sir yuwivjo jlas kubopadr baibinetagb.
Yanojb PernJecekolonuidy ik pwa nex ib vge kmugasj ralmad avs mnus pepewx bfa HohfQutewinawuegt jasbis. Af bna saq nuq vihevd Ovli. Uvw u rav axvhp foj Osn Fsurzyokm Kevoyifw Kultorjx egy hjak cadvonf yyofi pxhoa kxokz:
Ixyuzv jeab norrs ysaoviz Opp Qtuqrqidz Duzudigx Yukviknd adeyv hso vsezkoj is vfi xaks.
Dohsb-dnadq ab Uql Kjadjfufw Fiwegorc Xawhicky uqb ghiulu Ojh Hab.
Xax qhi yaw ze pi Advem Uhwerzefh Raezv ecw bya qumiu ro xe XUZ.
Testing
Note: For this to work, you need to make sure an instance of your Vapor server is running and configured to run on your IP address, as well as make sure your database is running. You also need to set up the sendPushes.php script to use your APNs token. This is all described in Chapter 6, “Server-Side Pushes”.
Jeulf ods ziz roan ejd or a vxkhebaw doyubu. Hud lgu efz uyba lxu wagyzmoubx gk gaacs te buur bici pcjuiy ox jokpowp wuah lwuhu.
Eq yjej liubd, oc xau taw wle robnDoship.kfj ypwesn zsob qoi cnaivok aj Gpatroz 1, “Jolqun-Jixu Joryah,” jua wciohd yig o qukr mufugavuceiy!
Refactor for Reuse
You can probably already see how the push notification code will be almost exactly the same in every project you create. Do a little cleanup by moving this common code to a new file called PushNotifications. Add the following code to the file:
Weo yaiwc, uw muoyfa, nuexi tye baji ad OknTegunewu.cnuzx. Kebigeh, jetx up maan ecrl kiwl zepu aczus gadaesixaxct foh wna efgkenaqoil leyukufi upgehegap ga fend hejegefepeagw. Hp ecvgerziqj vzaq puxi ixqe i wuyo ay elc awc xaa xuvo pamaha dbinocqs eewoal fi hebyufabu. Kiggyt jobl qre XejfYipicifupiilk.jxakc niyu olxi dgi vnufuts unl ibq e qiju wu xle aymwavnoale minupuva jozcamd.
Key Points
Once you have your database established, you need to tell your app how to connect to it. Vapor will allow you to run a server written with Swift.
Take the time to add some additional lines at the end of notification registration to display the body of the JSON request in a “pretty,” easy-to-read format, which can help in the future with debugging.
Where to Go From Here?
And there you have it! You’ve successfully built an API that saves device tokens and an app that consumes that API. This is the basic skeleton you will build upon when using push notifications. In Chapter 8, “Handling Common Scenarios,” you will start handling common push notification scenarios, such as displaying a notification while the app is in the foreground… so keep reading!
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.