using Cloud Code
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.
To complete this tutorial, we need:
- An app created on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial 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.
Every Parse application installed on a device registered for push notifications has an associated Installation object. The 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 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 are stored in the channels field of the 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 file and the GCMSenderId that you obtained at Firebase in the AndroidManifest.xml file.
- Import the following dependecies:
2. Initialize Parse with Parse.initialize(this).
3. Create a new array of channels and put the channels you would like to subscribe. In this example, the News channel is created.
4. Add to your installation your GCMSenderId, obtained from the Firebase Console, through the command 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 object to the installation through the command installation.put("channels", channels).
6. Save the installation to your database through installation.saveInBackground().
The following code executes these steps:
To know more about how to get started with Cloud Code look at Cloud Code for Android Tutorial.
- Create a .jsfile to put your Cloud Code into. In this example, a main.js file is created.
- Define a Cloud function, using Parse.Cloud.Define, to call the push notification. In this example, this function is called Parse.Push.Send.
It is required to use the master key in this operation.
The following code executes these steps:
- Find the Cloud Code and click on Functions & Web Hosting. It looks like this:
3. Upload or create a new file (you can also edit the current main.js file directly on the browser). Then, click at Deploy as shown here:
- Import the following dependencies:
2. Call the ParseCloud.callFunctionInBackground on the pushsample cloud function:
The 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.
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:
To test the push notifications, just use the REST code while the device is opened.
At this stage, you can send push notifications using Cloud Code through Back4App!