Send via Dashboard – Swift

In this guide, we will show how properly set your app to work with Parse Push notifications and how send a notification from Parse Dashboard.

 

Step 1: Register your app to receive notification:

To make you app support Push Notifications, your app must use an Explicit App ID.

    1. Navigate to the Apple Developer Member Center website, and click on Certificates, Identifiers & Profiles.mainpage
    2. Select Identifiers on the iOS Apps section.
    3. You will see a list of your App IDs. Click on the + button.
    4. Enter a name for your new App ID under App ID Description, choose an App Id Prefix, and under App ID Suffix, select Explicit App ID, enter your iOS app’s Bundle ID, it should match the Bundle Identifier in your Xcode project configuration and in the Info.plist file.
    5. Enable Push Notifications under App Services. Enable other services that your app will need.
    6. Click on “Continue”, confirm that all values were entered correctly. Click on “Submit”.

Now that you’ve created the App ID, you need to configure the App ID for Push Notifications.

    1. Select your new App ID and click on “Edit”.appiddetails
    2. Scroll down to the Push Notifications section. Click on “Create Certificate” under “Development SSL Certificate”.configurepushnotifications
    3. Follow the instructions in the next screen to create a Certificate Signing Request (CSR) using the Keychain Access utility on your Mac. This is needed to authenticate the creation of the SSL certificate.
    4. Upload the CSR to Apple’s servers, then click on “Generate”.
    5. Add the certificate to your login keychain.addcertificatestokeychain
    6. Open the Keychain Access utility, and locate the certificate under “My Certificates”. It should be called “Apple Development Push Services:”. Right-click on it, select “Export”, and save it as a .p12 file. Do not enter an export password when prompted!exportcertificate

You will need to repeat the process to make the Production SSL Certificate.

Now we need to configure the Xcode project.

    1. Go to your project’s Capabilities page, then turn On Push Notifications. If there are any issues that require your attention, Xcode will let you know.4_capabilitieseditor_2x
    2. Go to the General page. Modify the Bundle Identifier to match your App ID’s Bundle Identifier.

Step 2: Register your app to receive notification in the back4app Dashboard:

    1. Go to your app back4app Dashboard
    2. Click on “iOS Push Notification Settings”back4appdashboar
    3. Click on “Choose File”, select your .p12 certificate, then click on “Send”.applepushcertificates

Step 3: Implement Push Notifications Functions:

      1. Go to AppDelegate.swift file. In the application: didFinishLaunchWithOptions method, insert the following code after the Parse initialization if you are building a app for iOS versions < 10:
        // Notifications types and settings
        
        let notificationTypes: UIUserNotificationType = [UIUserNotificaitonType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
        let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
        // Register app for receive notifications
        UIApplication.shared.registerUserNotificationsSettings(notificationSettings)
        UIApplication.shared.registerForRemoteNotifications()

        If you are building a iOS 10 app, import the UserNotification framework , add UNUserNotificationCenterDelegate to your AppDelegate:

        class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
            ...
        }

        Then, use the following code in didFinishLaunchingWithOptions:

        if #availabe(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                if error == nil {
                    UIApplication.shared.registerForRemoteNotification()
                }
            }
        } else {
            if #available(iOS 7, *) {
                application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
            } else {
                let notificationTypes: UIUserNotificationType = [UIUserNotificaitonType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
                let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
                
                UIApplication.shared.registerUserNotificationsSettings(notificationSettings)
                UIApplication.shared.registerForRemoteNotifications()
            }
        }

        Then, add the willPresent and didReceive userNotificationCenter functions:

        @availabe(iOS 10.0, *)
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void {
            completitionHandler([.alert, .badge, .sound])
        }
        
        @available(iOS 10.0, *)
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            completionHandler()
        }
      2. Next we have to subscribe the app in a channel for enable the background’s notifications. Insert this code in the AppDelegate.swift file:
        func application(_ application: UIApplication, didRegisterForRemoteNOtificationsWithDeviceToken deviceToken: Data) {
            let installation = PFInstallation.current()
            installation.setDeviceTokenFom(deviceToken)
            installation.saveInBackground()
            PFPush.subscribeToChannel(inBackground: “globalChannel”)
        }

        Then, add the didReceiveRemoteNotification function:

        func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            PFPush.handle(userInfo)
        }
        Now the app can receive background notifications.
      3. It’s also possible to send notifications to just a group of people. For this, we have to register the app for the group’s channel. When you send a notification to a channel, all the app’s installations that have the channel in the channels field will receive the notification. To register a installation in a channel, we use the following code:
        // We want to register the installation in the back4app channel.
        let currentInstallation = PFInstallation.current()
        currentInstallation.addUniqueObject(“back4app”, forKey: “channels”)
        currentInstallation.saveInBackground()

        Now, the device will receive all the notifications directed to the back4app channel.

      4. If now we dont want anymore to the device receive notifications in back4app channel, we just need to unregister the installation in the back4app channel:
        let currentInstallation = PFInstallation.current()
        currentInstallation.removeObject(“back4app”, forKey: “channels”)
        curentInstallation.saveInBackground()

        We can check whats channels the installation is registered using the channels field of the PFInstallation object:

        // Lets see the current registered channels of the installation.
        let currentChannels = PFInstallation.curent().channels
        // Do something with currentChannels.
        

 

Step 4: Sending notification from Dashboard:

    1. Go to your app’s Parse Dashboard, and click on Push.
    2. Now chose the audience of the notification. You can send notifications to everyone and for a specific platform.
    3. Write the notification text, then click on Send Push to send your notification.