iOS

Send iOS push notifications from your Parse Server - Swift

Introduction

This section explains how you can send push notifications using Parse Dashboard through Back4App.

This is how it will look like:

Push Notification Via Dasboard App

At any time, you can access the complete iOS Project built with this tutorial at our GitHub repository.

Prerequisites

To complete this tutorial, you need:

Step 1 - Create Your Push Certificates In The Apple Developer Center

Pay attention to the steps below because you need to get them right in the exact order. If pushes are not being received there isn’t much we can do to debug besides going over the steps again

  1. Go to the target and go to Capabilities - click on push notifications - then turn push notifications on. This creates your app id and sets your entitlements.

    iOS Push Notification Settings In Xcode

  2. Go to the Apple Developer Center and login to your account:

    iOS Push Notification Settings In Xcode

  3. Click on Certificates, Identifiers & Profiles.

    iOS Push Notification Settings In Xcode

  4. In the certificates section hit the plus sign. Choose to create a apple push notification certificate for sandboxes.

    iOS Push Notification Settings In Xcode

    iOS Push Notification Settings In Xcode

  5. Pick your app id that matches the app id used in your current Xcode project.

    iOS Push Notification Settings In Xcode

  6. Now you will be asked for a Certificate Signing Request or CSR. You will generate your CSR from your Mac computer.

    iOS Push Notification Settings In Xcode

  7. On your Mac computer open your Keychain Access.

    iOS Push Notification Settings In Xcode

  8. Next, request a certificate from a certificate authority.

    iOS Push Notification Settings In Xcode

     

  9. Pick your user email, then make sure you save your certificate to disk - save it on a folder on your desktop called PushCerts.

    iOS Push Notification Settings In Xcode

  10. Go back to the Apple Developer center. Upload your CSR and hit continue.

    iOS Push Notification Settings In Xcode

  11. Download your Development APN certificate into the same folder called PushCerts. Call it apn_dev.

    iOS Push Notification Settings In Xcode

  12. Lets start the process over. This time we will create production push certificates. You need both for testing and release. Select Apple Push Notification Service SSL (Sanbox & Production).

    iOS Push Notification Settings In Xcode

  13. Upload your CSR your previously created and hit continue.

    iOS Push Notification Settings In Xcode

  14. Download your Production APN certificate into the same folder called PushCerts. Call it apn_prod.

    iOS Push Notification Settings In Xcode

  15. At this point you should have 3 files in your PushCerts folder. Double click on your apn_prod and your apn_dev files to add them to your keychain.

    iOS Push Notification Settings In Xcode

  16. Open the keychain and find the files in the keychain. Click on each on and hit export. You will want to export them as a .p12 file into your PushCerts Folder. Name the development one cert Dev_PushCertificates.p12 and name the production cert as Prod_PushCertificate.p12.

    iOS Push Notification Settings In Xcode

  17. It will ask you add a password to your exported file. Just leave it blank. You will have to enter your master key to sign the certificate though, and that’s fine.

    iOS Push Notification Settings In Xcode

  18. Now that you have added your .p12 files your folder should look like this. If you have all these files in your folder you can go on to Step 2. If you don’t have any of these files go back to the beginning and figure out where you missed a step.

    iOS Push Notification Settings In Xcode

Step 2 - Adding Your .P12 certificates to Back4App

  1. You’re almost done. Aren’t you excited? Go to Back4App Website, log in, find your app and click on iOS Push notification.

    iOS Push Notification Settings In Xcode

  2. Upload the dev cert and the prod cert and hit send for each.

    iOS Push Notification Settings In Xcode

  3. After you’ve uploaded both certificates your screen should look like this.

    iOS Push Notification Settings In Xcode

Step 3 - Setting up Your Xcode Project to receive Push Notifications

  1. Open your project’s AppDelegate.swift file to create a push installation object. Add the UserNotifications framework at the top of the file.
    AppDelegate.swift
    1
    
    import UserNotifications
    

     
     

  2. Add the code below inside of the didFinishLaunchingWithOptions function, and make sure it is before ‘return true’ statement.
    AppDelegate.swift
    1
    2
    3
    4
    5
    6
    
         UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge, .carPlay ]) {
             (granted, error) in
             print("Permission granted: \(granted)")
             guard granted else { return }
             self.getNotificationSettings()
         }
    

     
     

  3. Add the following code snippets to your AppDelegate.swift file below the didFinishLaunchingWithOptions function. This code will issue a request for push notifications permissions when the app first launches. Make sure to say yes to this request or your app will not be able to receive pushes. It will also handle the resulting token when the request is approved and save it as an installation object on Back4App.
     
     
    AppDelegate.swift
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
     func getNotificationSettings() {
         UNUserNotificationCenter.current().getNotificationSettings { (settings) in
             print("Notification settings: \(settings)")
             guard settings.authorizationStatus == .authorized else { return }
             UIApplication.shared.registerForRemoteNotifications()
         }
     }
    
     func application(_ application: UIApplication,
                      didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
         createInstallationOnParse(deviceTokenData: deviceToken)
     }
    
     func application(_ application: UIApplication,
                      didFailToRegisterForRemoteNotificationsWithError error: Error) {
         print("Failed to register: \(error)")
     }
    
     func createInstallationOnParse(deviceTokenData:Data){
         if let installation = PFInstallation.current(){
             installation.setDeviceTokenFrom(deviceTokenData)
             installation.saveInBackground {
                 (success: Bool, error: Error?) in
                 if (success) {
                     print("You have successfully saved your push installation to Back4App!")
                 } else {
                     if let myError = error{
                         print("Error saving parse installation \(myError.localizedDescription)")
                     }else{
                         print("Uknown error")
                     }
                 }
             }
         }
     }
    

     
     

  4. Test it by running your app. You should see this in your simulator.

    iOS Push Notification Settings In Xcode

     
     

  5. From here on out you must use a physical device, an iphone or ipad. Push notifications do not work with the Xcode simulator. If you do not have a physical device you cannot go any further in the tutorial. Once you have your physical device attached to your Mac computer and Xcode, try running the app on your device through Xcode. When you see the push permissions request hit approve.

Step 4 - Test your app

  1. Go to Back4App Website, log in, find your app and click on Dashboard.
     
     
  2. First check that your device’s installation record is visible in Installation table.

    iOS Push Notification Settings In Xcode

     
     

  3. Then Click on Push > Send New Push and create an audience for your push notification.

    Push at Dashboard

     
     

  4. Write your message and look at the preview by clicking at iOS option.
     
     
  5. If you have already reviewed the push notification and you want to send, click on Send push.

    Send Push

You may explore the other options for Push Notification at Parse Dashboard.
There, it’s also possible to look at Past Pushes you sent and the Audiences you created for them.

It’s done!

At this stage, you can send push notifications using Parse Dashboard through Back4App!