iOS
Send Push Notifications
using CC and ObjC
26 min
sending push notifications using cloud code with objective c introduction this section explains how you can send push notifications using cloud code through 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 to complete this quickstart, you need xcode an app created at back4app follow the new parse app tutorial to learn how to create a parse app at 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 1 set up your ios app to receive push every parse application installed on a device registered for push notifications has an associated installation installation object the installation installation object is where you store all the data needed to target push notifications for example, in your app, you could store which teams one of your users is interested in to send updates about their performance saving the installation installation object is also required for tracking push related app open events the simplest way to start sending notifications is using channels this allows you to use a publisher subscriber model for sending pushes devices start by subscribing to one or more channels, and notifications can later be sent to these subscribers the channels subscribed to by a given installation installation are stored in the channels channels field of the installation installation object after that we will go over sending targeted push notifications to a single user or to a group of users based on a query 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 2 subscribe your device to the news channel first we will add a channel to your installation object we are going to do this by altering the method createinstallationonparse in our app delegate file open your project’s appdelegate m file, and add make sure your version of didregisterforremotenotificationswithdevicetoken is the same as the code below we are adding one new line of code ‘\[currentinstallation setobject @\[@”news1”] forkey @”channels”];’ which will set the installation object’s channel array to contain one channel called ‘news’ this will allow us to send a message to everyone who subscribes to the channel called ‘news’ via cloud code appdelegate m 2\ test it by running your app on a physical device you cannot run this on simulator you’ll need an actual push token to update your installation record so a physical device is a must after it runs successfully you should see this in the installation section of your dashboard you can check by going to back4app website and click on your app’s dashboard and then check the installation table under the the channels column you should see ‘news’, showing you that you are now subscribed to the news push channel 3 create your cloud code to know more about how to get started with cloud code look at cloud code for ios tutorial create a js js file to put your cloud code into you need to call it main js main js in order for back4app to know this is where you store your cloud code define a cloud function, using parse cloud define parse cloud define , to call the push notification inside the function we will call parse push send parse push send to send a pushes to the ‘news’ channel it is required to use the master key in this operation the following code executes these steps parse server 3 x // main js 1 parse cloud define("pushsample", (request) => { 2 3 return parse push send({ 4 channels \["news"], 5 data { 6 title "hello from the cloud code", 7 alert "back4app rocks!", 8 } 9 }, { usemasterkey true }); 10 }); parse server 2 x //main js 1 parse cloud define("pushsample", function (request, response) { 2 parse push send({ 3 channels \["news"], 4 data { 5 title "hello from the cloud code", 6 alert "back4app rocks!", 7 } 8 }, { 9 success function () { 10 // push was successful 11 response success("push sent"); 12 console log("success push sent"); 13 }, 14 error function (error) { 15 // push was unsucessful 16 response error("error with push " + error); 17 console log("error " + error); 18 }, 19 usemasterkey true 20 }); 21 }) 4 upload to cloud code go to your app at back4app website and click on dashboard dashboard find the cloud code cloud code and click on functions & web hosting functions & web hosting it looks like this 3\ upload or create a new file (you can also edit the current main js main js file directly on the browser) then, click at deploy deploy as shown here 5 call cloud code from your ios app next we are going to write some code to call this cloud function from your app you will need both the simulator and a physical device to complete this task you will call the cloud function from your app running in the simulator and you will see the push appear on your physical device your physical device should actually be closed with the lock screen on in order to see the push a push will not appear on the screem if you are inside the app that is sending it when you receive the push open your project’s viewcontroller m file we need to include parse in the view controller by adding the following code ‘#import \<parse/parse h>’ at the top of the file viewcontroller 3\ next in the viewcontroller m file we will call an alert function from the viewdidappear method the alert will allow you to trigger the cloud code code which will send a push to your device be sure to include the following block of code after the viewdidload function viewcontroller m 4\ run your app in the simulator and when the alert asking to send the push pops up hit “ok” on your physical device you should see the push appear on the lock screen 6 call cloud code from rest api the rest api provides a quick and easy way to test if your cloud function is working just use the code below in your terminal or command prompt click on to know more about how to get started with command line in linux , macos or windows to test the push notifications, just use the rest code while the device is closed 7 send targeted push notifications using a user object going forward we will be using a different ios project that has a basic signup and signin features already built we are going to be using this ios project that we can show you how to detect if a user is logged in, and if so save their installation to with a link to their object id for querying in cloud code you can download the complete ios project built with this section’s tutorial at our github repository but you will still have to do all of the setup from the previous tutorial that explains how to send pushes fromt he back4app dashboard get the new version of the app setup and signup or login to the app first make sure you download the working template from github at github repository we are not going to walk through all the steps of building this app, instead we will focus on setting up the cloud code and why it works once you open this new app be sure to put your own app’s credentials in the appdelegate m file appdelegate m 2\ this app has some major differences between the previous app it features 2 sections, one for being logged into your app and one section when you are not logged in to your app the next big change is the appdelegate m file’s function ‘didregisterforremotenotificationswithdevicetoken’ we’ve added 1 line that store’s the user’s object id as part of the installation object that way we can know which user is associated with which installation object and can target them individually for pushes appdelegate m 3\ since we are now storing the user’s object id as part of the installation object we do not want to request a new push token until the user is logged in we do not want to request a token directly from appdelegate m file’s function ‘application didfinishlaunchingwithoptions’ instead we want to call it from the loggedinviewcontroller’s function ‘viewdidappear’ in ‘viewdidappear’ we call a function on the appdelegate to request access to a push notification token from apple since you can only view this section once you are logged in we can assume the user is logged in when we create the installation object and retrieve the object id loggedinviewcontroller appdelegate m 4\ ok, now to sign up or login on your physical device (iphone or ipad) start the app you should see the image below you should sign up to create a new user or sign in if you have already created a user on your app this is how it will look like you should now be able to see the loggedinviewcontroller it should look like this if you try to send pushes to yourself it won’t work yet because we haven’t added those methods to cloud code so that’s what we will do next 8 add the targeted push methods to cloud code open your main js file that you created previously and add the following functions to target installations by user id it is required to use the master key in this operation the following code executes these steps parse server 3 x // main js 1 parse cloud define('sendpushtoyourself', (request) => { 2 let userid = request user id; 3 4 let query = new parse query(parse installation); 5 query equalto("userid", userid); 6 query descending("updatedat"); 7 return parse push send({ 8 where query, 9 data { 10 title "hello from the cloud code", 11 alert "back4app rocks! single message!", 12 } 13 }, { usemasterkey true }); 14 }); 15 16 parse cloud define('sendpushtoallusers', (request) => { 17 let currentuser = request user; 18 let userids = \[currentuser id]; 19 20 let query = new parse query(parse installation); 21 query containedin('userid', userids); 22 return parse push send({ 23 where query, 24 data { 25 title "hello from the cloud code", 26 alert "back4app rocks! group message!", 27 } 28 }, { usemasterkey true }); 29 }); parse server 2 x //main js 1 parse cloud define('sendpushtoyourself', function (request, response) { 2 var currentuser = request user; 3 var userid = currentuser id; 4 5 var query = new parse query("installation"); 6 query equalto("userid", userid); 7 query descending("updatedat"); 8 parse push send({ 9 where query, 10 data { 11 title "hello from the cloud code", 12 alert "back4app rocks! single message!", 13 } 14 }, { 15 usemasterkey true, 16 success function () { 17 response success("success sending a single push!"); 18 }, 19 error function (error) { 20 response error(error code + " " + error description); 21 } 22 }); 23 }); 24 25 parse cloud define('sendpushtoallusers', function (request, response) { 26 var currentuser = request user; 27 var userids = \[currentuser id]; 28 29 var query = new parse query(parse installation); 30 query containedin('userid', userids); 31 parse push send({ 32 where query, 33 data { 34 title "hello from the cloud code", 35 alert "back4app rocks! group message!", 36 } 37 }, { 38 usemasterkey true, 39 success function () { 40 response success('success sending a group push!'); 41 }, 42 error function (message) { 43 response error(error code + " " + error description); 44 } 45 }); 46 }); 9 upload to cloud code go to your app at back4app website and click on dashboard dashboard find the cloud code cloud code and click on functions & web hosting functions & web hosting it looks like this 3\ upload or create a new file (you can also edit the current main js main js file directly on the browser) then, click at deploy deploy as shown here 10 test that you can send targeted push notifications to yourself open your app from the simulator while leaving your physical device closed with the lock screen on you can test that both push functions are working by pressing the ‘send push to a yourself’ button and the ‘send push to a group of people’ button you should see the pushes appear on your devices lock screen final thoughts this concludes the tutorial you should have a firm understanding of how to send pushes based on a user’s channel or a user’s object id or any other query that involves getting the user’s object id remember in order to store the user’s object id you must add it to the push installation and only request a push token when the user is logged in when sending pushes via query be aware that it is limited by default to 100 results and some users may have more than one instllation object also it is not reccomended to send pushes to array of installation objects that are larger than 100 results it could result in some pushes not getting sent if you are dealing with large groups of people it is better to use channels or to send the pushes out in repeated requests it’s done! at this stage, you can send push notifications using cloud code through back4app! congratulations!