Android
Push Notifications
using Cloud Code
16 min
send parse push notifications using cloud code 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 android project built with this tutorial at our github repository prerequisites to complete this tutorial, we need android studio an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria l to create an android studio project connected to back4app follow the steps 1 to 5 of back4app push notification via dashboard tutorial carefully to set up push notifications to your app a device (or virtual device ) running api level 27 or newer 1 set up android 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 to start working with push notifications, the following steps are required if you downloaded our project template , don’t forget to change your credentials in the app/src/main/res/values/string xml app/src/main/res/values/string xml file and the gcmsenderid gcmsenderid that you obtained at firebase in the androidmanifest xml androidmanifest xml file import the following dependecies 2\ initialize parse with parse initialize(this) parse initialize(this) 3\ create a new array of channels and put the channels you would like to subscribe in this example, the news news channel is created 4\ add to your installation your gcmsenderid gcmsenderid , obtained from the firebase console , through the command installation put("gcmsenderid", "your firebase gcm sender id here") installation put("gcmsenderid", "your firebase gcm sender id here") to know how you can obtain that key, look at step 1 of push notifications via dashboard tutorial 5\ add the channels channels object to the installation installation through the command installation put("channels", channels) installation put("channels", channels) 6\ save the installation to your database through installation saveinbackground() installation saveinbackground() the following code executes these steps 1 parse initialize(this); 2 arraylist\<string> channels = new arraylist<>(); 3 channels add("news"); 4 parseinstallation installation = parseinstallation getcurrentinstallation(); 5 // don't forget to change the line below with the sender id you obtained at firebase 6 installation put("gcmsenderid", "your firebase gcm sender id here"); 7 installation put("channels", channels); 8 installation saveinbackground(); 2 create your cloud code to know more about how to get started with cloud code look at cloud code for android tutorial create a js js file to put your cloud code into in this example, a main js main js file is created define a cloud function, using parse cloud define parse cloud define , to call the push notification in this example, this function is called parse push send parse push send 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({ 5 channels \["news"], 6 data { 7 title "hello from the cloud code", 8 alert "back4app rocks!", 9 } 10 }, { usemasterkey true }); 11 }); 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 }); 3 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 4 call cloud code from android app import the following dependencies 2\ call the parsecloud callfunctioninbackground parsecloud callfunctioninbackground on the pushsample pushsample cloud function 1 final hashmap\<string, string> params = new hashmap<>(); 2 // calling the cloud code function 3 parsecloud callfunctioninbackground("pushsample", params, new functioncallback\<object>() { 4 @override 5 public void done(object response, parseexception exc) { 6 if(exc == null) { 7 // the function was executed, but it's interesting to check its response 8 alertdisplayer("successful push","check on your phone the notifications to confirm!"); 9 } 10 else { 11 // something went wrong 12 toast maketext(mainactivity this, exc getmessage(), toast length long) show(); 13 } 14 } 15 }); the alertdisplayer alertdisplayer method used in the example above is the following 3\ test if the push notifications are being sent by calling the function above while the device is opened 5 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 opened it’s done! at this stage, you can send push notifications using cloud code through back4app!