from client side (Swift)
11 min
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 project built with this tutorial at our github repository https //github com/templates back4app/ios install sdk prerequisites to complete this quickstart, you need xcode https //developer apple com/xcode/ an app created at back4app follow the new parse app tutorial https //www back4app com/docs/get started/new parse app to learn how to create a parse app at back4app an ios app connected to back4app note follow the install parse sdk (swift) tutorial https //www back4app com/docs/ios/parse swift sdk to create an xcode project connected to back4app an ios app set up via back4app push notifications via dashboard tutorial https //www back4app com/docs/ios/push notifications/best ios push notification service 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 https //www back4app com/docs/ios/push notifications/best ios push notification service , even if you use the ios project built with this tutorial that is available at our github repository https //github com/mpc20001/ios objc push cloud code you should have basic push notifications working and also be able to send pushes out via the admin console 1 enable client push go to back4app website https //www back4app com/ log in, find your app and click on server settings find the âcore settingsâ block and click on \<font color="#2166ae">settings\</font> the âcore settingsâ block looks like this 3\ scroll to the end of the page and click on the \<font color="#2166ae">edit details\</font> button, as shown below 4\ you will see a checkbox called \<font color="#2166ae">allow push notification from client\</font> in the end of the edit page, tick that box and click on the \<font color="#2166ae">save\</font> button, as shown below 2 subscribe your device to the news channel assuming you have completed the back4app push notifications via dashboard tutorial https //www back4app com/docs/ios/push notifications/best ios push notification service , you will want to modify the completed project from that tutorial or download it from our github repository https //github com/back4app/ios objc push first, you will add a channel to your installation object you are going to do this by altering the method \<font color="#2166ae">createinstallationonparse\</font> in your appdelegate file open your projectâs \<font color="#2166ae">appdelegate swift\</font> file and add the following line of code â installation setobject(\[ânews1â] forkey âchannelsâ]; â which will set the installation objectâs channel array to contain one channel called \<font color="#2166ae">news\</font> appdelegate m 1 func createinstallationonparse(devicetokendata\ data){ 2 if let installation = pfinstallation current(){ 3 installation setdevicetokenfrom(devicetokendata) 4 installation setobject(\["news"], forkey "channels") 5 installation saveinbackground { 6 (success bool, error error?) in 7 if (success) { 8 print("you have successfully saved your push installation to back4app!") 9 } else { 10 if let myerror = error{ 11 print("error saving parse installation \\(myerror localizeddescription)") 12 }else{ 13 print("uknown error") 14 } 15 } 16 } 17 } 18 } this will allow you to send a message to everyone who subscribes to the channel called \<font color="#2166ae">news\</font> via cloud code make sure your version of \<font color="#2166ae">didregisterforremotenotificationswithdevicetoken\</font> is the same as the code below 2\ next, we will add a method to your app delegate to send a push to the \<font color="#2166ae">news\</font> channel everytime the app launches open your projectâs \<font color="#2166ae">appdelegate swift\</font> file and the method below and make sure this method is fired off everytime the app launches by calling it from \<font color="#2166ae">didfinishlaunchingwithoptions\</font> appdelegate m 1 func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplicationlaunchoptionskey any]?) > bool { 2 let configuration = parseclientconfiguration { 3 $0 applicationid = "paste your application id here" 4 $0 clientkey = "paste your client id here" 5 $0 server = "https //parseapi back4app com" 6 } 7 parse initialize(with configuration) 8 9 unusernotificationcenter current() requestauthorization(options \[ alert, sound, badge, carplay ]) { 10 (granted, error) in 11 print("permission granted \\(granted)") 12 guard granted else { return } 13 self getnotificationsettings() 14 } 15 sendpushonlaunch() 16 return true 17 } 18 func sendpushonlaunch(){ 19 let push = pfpush() 20 push setchannel("news") 21 push setmessage("push from device") 22 push sendinbackground() 23 } 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!