Push Notification Swift 5

Michael Tran (mytee)
4 min readAug 5, 2019

Swift 5, APN client and server

Client and server for Apple Push notification with Swift 5. Supplement for the lecture week 6.

Not to repeat the lecture. The following are practical side of the class.

Prepare for the client

1- Ask user for permission — either silent or non-silent notification -.

Add the code from line 2 to 8 in the func as above. It will pop a dialog box to ask user for permission.

2- Register the app with APN server.

Add these 3 functions to the end of AppDelegate.Swift, before the last }

3- Enable background task with Remote Push,

and also Push service.

If you find any error at this step, then likely you didnt do the preparation steps at the Apple developer site for

  • app ID: It must be specific, no * in app ID
  • Push certificates for development. The distribution is optional until you plan to release the app.
  • download and import the certificate
  • download and import the provisional profile that matches with the app id. Without this, your xcode will refuse compiling
  • run the app on real device, not emulator

4- Add the codes at

  • add “import UserNotifications” to the top of AppDelegate and your main viewcontroller
  • add to viewDidLoad()

UNUserNotificationCenter.current().delegate = self;

  • to the end of you viewcontroler, so that you can deal with the notification while app is in foreground

extension ViewController: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print(notification.request.content.body);
completionHandler([.alert, .sound])
}}

Prepare for the Push server.

It is worth to note again that we need our own server to send our push notification request to Apple Push server, which inturn sends the push notification to our app. We dont have any alternative send the notification directly to the app.

  • The first step is to create and download the P8 file. It will download something like AuthKey_Z692H6MNDF.p8. The Z692H6MNDF is the key ID, will be used to send the Push.

We have 2 options. We can either download the test server from Khoa Pham, onmyway133@gmail.com. This guy is just great. His server can run on multi platform. Download here https://github.com/onmyway133/PushNotifications/releases.

You can review the preparation process in his github as well https://github.com/onmyway133/PushNotifications.

Add the p8 file, key ID, and your team id into the test server app as illustrated in the link above.

You will also need your bundle ID and device token as shown in step 2 when you run the app.

Let review the lecture note for this section.

We can actually do some fancy things with payload and notification UI/service templates. This could be covered in a lecture at later stage.

Or you can make your own Push server with Nodejs https://github.com/node-apn/node-apn.

You dont have to use the many year old examples with pem files. In fact, just

1- Create a directory, open a terminal there.

2- Run “npm install apn — save” at the terminal.

3- Save this code to a file, say server.js

4- Copy your p8 file into this directory.

5- Edit the file, use your key, key id and team id at lines 5, 6, 7.

6- Edit line 13 for the token.

7- Line 23 for the Bundle ID.

8- run “node server.js” at the terminal.

You should expect to receive the notification on your device.

The server can use express, mongodb to register the device token once, next time, you dont have to send it again. With an id, ie mobile phone registration. You can get the saved token from the mongodb to send the notification to the device.

There is another option is to use Firebase Cloud Messaging service so you can send notification to both IOS and Android but it is not covered in this note.

The sample code is at https://github.com/myteeNatanwit/pushTest.

--

--