Reading:
How to Add Push Notifications to Your iOS App using Xcode 8 and Swift 3 Essay
Share: Twitter, Facebook, Pinterest
Free Essay
Dec 2nd, 2019

How to Add Push Notifications to Your iOS App using Xcode 8 and Swift 3 Essay

Sending impel declarations is one of the best ways to re-engage users forthcoming a while your app. However, adding impel declarations to your iOS app can be a summon accordingly of the mere equality of iOS versions and all the exchanges in Accelerated and Xcode. A lot of declaration arrangees we got used to in the departed are not bestow in the newer iOS versions, which adds to the laziness.

There are entirely a few manages that pretence you how to tool impel declarations for iOS apps but most are either outdated or too perplexed to fashion heads or tails of.

Another whole is that a lot of manages don’t clothe the demonstrations of Xmode 8 and Accelerated 3 and don’t propose ill-versed compatibility for older iOS versions that livelihood Swift. Worse stagnant, they don’t encease any instruction on the user of the new APNs (Apple Impel Notifications Service) Auth Keys. And that’s a swell-with-fill-with-shame accordingly these keys do a excellent job simplifying the steps that deficiency to be fascinated to cast impel declarations.

We’ve written a manage that allure acceleration you tool impel declarations in your iPhone and iPad app. You’ll be employmentserviceable to cast them from Node.js by using the remotouchstone technology straightly and easily.

Getting Started

First of all, you should unconcealed your iOS app design in Xmode 8. If this is your primitive iOS design, then download Xmode 8 and engender a new design. Fashion unfailing to update your modebase so that it uses Accelerated 3.

Second, you’ll deficiency an erratic Apple Developer Program Fraternity representation. The fraternity costs $100/year. Being a component is a modification if you lack to tool impel declarations in your iOS app. When your fraternity is erratic, cast Xmode to use the iCloud representation that has your erratic Apple Developer Program Membership.

Third, enunfailing that you’ve castd a Bundle Identifier for the app in the design editor.

Adding Impel Notifications to Your App

First of all, you deficiency to enserviceable the impel declarations demonstration in Xmode 8. To do that, go to the design editor for your app, rove to APNS, and go to the Capabilities tab. Then click on Impel Notifications and exexchange the rate to ON.

Xmode allure then pretence two restrainmarks powerful you that the facility to cast impel declarations has been qualifyd. It’s to-boot an demonstration that Xmode has engenderd an App ID in the Developer Center and qualifyd Impel Notifications employment for the app.

Asproof Invention Tokens

To take impel declarations from your app, inventions should be attested. When a invention invests your app, APNs assigns a uncommon invention diagnosis to it. You can use the diagnosis to cast impel declarations whenever you lack to. When the invention takes a uncommon diagnosis, it should abide in your backend basisbase.

Here’s what a invention diagnosis looks like: 5311839E985FA01B56E7AD74334C0137F7D6AF71A22745D0FB50DED665E0E882

Open AppDelegate.accelerated and add the mode underneath to the didFinishLaunchingWithOptions cancel part precedently the recur announcement to ask a invention diagnosis for the invention you’re using:

The rule is incongruous in iOS 10 accordingly Apple introduced a new frametoil denominated UserNotifications. So, it has to be indispensable to get admittance to the UNUserNotificationCenter arrange.

To do that, add this drift announcement to the top of AppDelegate.swift: When that’s executed, go to the design editor for your target and rove to the General tab. Find the Linked Frameworks and Libraries individuality, click on the + proof and elect UserNotifications.

Framework: Then add these cancels in AppDelegate.swift. They allure be invoked when APNs has either successfully recorded the invention by assigning a uncommon diagnosis to it or failed to record it:

You allure deficiency to tool the logic that allure sustain the diagnosis abideent in your app’s backend. Further on in this manage, we’ll demonstrate how your backend server allure amalgamate to the APNs and cast impel declarations. To do that, it allure supply the invention’s diagnosis and semblance which invention or inventions should get impel declarations.

Keep in purpose that the invention diagnosis may get exalterable in the coming for a reckon of reasons. We intimate that you use NSUserDefaults, a questional key-rate provision, to sustain the diagnosis questionally. That way you’ll solely deficiency to update your backend when the diagnosis exchanges, which allure acceleration you to shirk deficiencyless asks.

Another being you should recomponent is to run your app on a corporeal iOS invention when enlargeed on tooling impel declarations, specially forthcoming modifying AppDelegate.swift. iOS simulators can’t take impel declarations, so using them in this scenario is vapid. When you see the forthcoming popup, tap OK to suffer your app to take impel declarations:

After that, Xmode solace allure pretence your invention’s uncommon diagnosis. You should delineation and obviate it for succeeding use.

Do Preparatory Toil to Take Notifications

Now we deficiency to get perfectbeing expert to take impel declarations. First, add this cancel in AppDelegate.swift. It allure be invoked perfect period your app takes a impel declaration from your backend server: This cancel allure solely get invoked if the user taps or swipes to interact forthcoming a while your declaration or if the user had your app unconcealed when the impel declaration came through.

The upproper logic of your impel declarations depends on the character of your app, so you should shape it out by yourself. For development, a harbinger energy impel new messages as declarations and tapping on them should unconcealed the confabulation page. When enlargeed on this, use basis answerance that incloses the basis you cast from the backend (colloquy ID if yours is a harbinger app).

There is a contempt hiccup, though. If your app is unconcealed when the invention takes a impel declaration, the user won’t see it. So you should fancy of another way of notifying the user of a new result in fact the app is alexpert in use. Restrain out this StackOverflow question for toilarounds. For development, you could parade a touchstone that looks common to inbred iOS declarations.

Generate an APNs Auth Key

The instant being for you to do is engender an notoriety key. That key allure fashion it feasible for your backend server to confirm its asks forthcoming a while APNs when it lacks to cast impel declarations to your inventions.

This rule used to be cumbersome. You had to swell out a Certificate Signing Ask in Keychain Access, upload it to the Developer Center, download a proofed certificate, and then ship-produce its special key from Keychain Access. You to-boot had to transmute twain touchstoneimonials to the .pem format. The certificate had to be renewed perfect year. Worse stagnant, it was solely adapted for one design, Development or Production.

Luckily, Apple has made beings plenteous uncompoundedr by introducing APNs Auth Keys. These keys never delayer true you repeal them and toil forthcoming a while all deployment designs. To engender an APNs Auth Key, go to the Developer Center and rove to Keys – All. When there, click on the “+” dot to engender a new Auth Key.

Go to the instant page and character in a spectry for your key. Then enserviceable APNs and click on Continue at the proinstitute of the page. Fashion unfailing the APNs restrainbox is chosen.

When executed, click on Confirm on the instant page. Apple allure engender a .p8 key smooth for you. The smooth allure incease your APNs Auth Key. Then download the .p8 smooth and obviate it for succeeding use. Don’t overlook to transcribe down the Key ID – you’ll deficiency it when you allure be amalgamateing to APNs.

Send Impel Notifications

Now you’re very cease to your motive. So, let’s cast your backend to amalgamate to APNs and cast impel declarations. We’ll pretence you how to do it in Node.js. Primitive of all, restrain that you own Node.js v4 or newer domiciled on your questional computer and run the forthcoming mode in a directory of your choice:

This allure enlarge a new Node.js design and invest the apn package from npm. The package allure confirm forthcoming a while APNs and cast your impel declarations. Now you’ll deficiency to delineation the .p8 key smooth you downloaded into the APNs folder that you’ve equitable engenderd. You can spectry the smooth apns.p8 to sustain beings uncompounded. Then, engender a new smooth in the APNs folder and spectry it app.js. Use your cosset editor for this lesson. Now departede the mode underneath into the smooth: Precedently you run this mode, fashion unfailing you’ve executed the forthcoming:

  1. Configured the key: Id possessions forthcoming a while the APNs Auth Key ID (availserviceable at https://developer.apple.com/account/ios/certificate/key)
  2. Configured the team: Id possessions forthcoming a while your Apple Developer Representation Team ID (availserviceable at https://developer.apple.com/account/#/membership/)
  3. Configured invention: Diagnosis forthcoming a while the invention diagnosis you engenderd forthcoming prevalent your contact and restraining the solace
  4. Configured declaration: question forthcoming a while your contact’s Bundle ID, which is admittanceible in the design editor

Now it’s period to touchstone perfectbeing out. Lock your invention and run node app.js. If you superveneed these steps correctly and did perfectbeing proper, you should take a impel declaration to your iOS invention.

If you interact forthcoming a while the declaration, the forthcoming allure answer in your Xmode solace accordingly didReceiveRemoteNotification got invoked: We trust that you institute this tutorial gentle to supervene and you can now cast impel declarations to any iOS invention.

Recommended stories