Send push notifications from client side in Swift
Introduction
This section explains how you can send push notifications through your iOS client with Back4App.
This is how it will look like:
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:
- Xcode.
- An app created on Back4App.
- Note: Follow the New Parse App Tutorial to learn how to create a Parse App on Back4App.
- An iOS app connected to Back4app.
- Note: Follow the Install Parse SDK (Swift) Tutorial to create an Xcode Project connected to Back4App.
- An iOS app set up via Back4App Push Notifications via Dashboard tutorial.
- An iOS device, iPhone or iPad, running iOS 10 or newer.
- A paid Apple Developer Account.
Going forward we are going to assume you have completed all steps of the Back4App Push Notifications via Dashboard tutorial, even if you use the iOS Project built with this tutorial that is available at our GitHub repository.
You should have basic push notifications working and also be able to send pushes out via the admin console.
Step 1 - Enable Client Push
- Go to Back4App Website, log in, find your app and click on
Server Settings
. - Find the “Core Settings” block and click on
SETTINGS
. The “Core Settings” block looks like this: - Scroll to the end of the page and click on the
EDIT DETAILS
button, as shown below: - You will see a checkbox called
Allow Push Notification from Client
in the end of the edit page, tick that box and click on theSAVE
button, as shown below:
Step 2 - Subscribe your device to the News channel
- Assuming you have completed the Back4App Push Notifications via Dashboard tutorial, you will want to modify the completed project from that tutorial or download it from our GitHub repository. First, you will add a channel to your
Installation
object. You are going to do this by altering the methodcreateInstallationOnParse
in yourAppDelegate.swift
file. Open your project’sAppDelegate.swift
file and add the following line of code - ‘installation.setObject([“News”], forKey: “channels”)’ - which will set the installation object’s channel array to contain one channel calledNews
.
AppDelegate.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
func createInstallationOnParse(deviceTokenData:Data){ if let installation = PFInstallation.current(){ installation.setDeviceTokenFrom(deviceTokenData) installation.setObject(["News"], forKey: "channels") 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") } } } } }
This will allow you to send a message to everyone who subscribes to the channel called
News
via cloud code.Make sure your version of
createInstallationOnParse
is the same as the code below. - Next, we will add a method to your app delegate to send a push to the
News
channel everytime the app launches. Open your project’sAppDelegate.swift
file and the method below and make sure this method is fired off everytime the app launches by calling it fromdidFinishLaunchingWithOptions
.
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
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let configuration = ParseClientConfiguration { $0.applicationId = "PASTE_YOUR_APPLICATION_ID_HERE" $0.clientKey = "PASTE_YOUR_CLIENT_ID_HERE" $0.server = "https://parseapi.back4app.com" } Parse.initialize(with: configuration) UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge, .carPlay ]) { (granted, error) in print("Permission granted: \(granted)") guard granted else { return } self.getNotificationSettings() } sendPushOnLaunch() return true } func sendPushOnLaunch(){ let push = PFPush() push.setChannel("News") push.setMessage("Push From Device") push.sendInBackground() }
Step 3 - Test that you can send Targeted push Notifications to yourself via the client
Open your app from the simulator while leaving your physical device closed with the lock screen on.
You should see the pushes appear on your device’s lock screen as soon as the app opens on the simulator.
Final Thoughts
You should have a firm understanding of how to send pushes from the client.
You can combine it with a pfquery to target users based on some sort of property like age, location, or object id.
Just remember that if client push is enabled it can be exploited and can’t be turned off without restricting all client pushes.
It’s recommended that you tick to pushes from Cloud Code, but it’s still good to know.
It’s done!
At this stage, you can send push notifications using Client Push through Back4App!