Push Notification via Dashboard

Push notifications using Firebase Cloud Messaging is available on Back4App’s Parse Service. To use it on your app, just follow the steps that come.

Step 1 – Create a project at Firebase Console

First, it is necessary to go to Firebase Console. Then, click the button ‘Create New Project’. Add your project name and your country, as in the screen:

fb_proj

Wait for it to create, then click the ‘Add Firebase to your Android app’:

fb_android

Add the package name and press ‘Add App’:

fb_package

After pressing, a screen will tell you to put a file named ‘google-services.json’ into the root of your app and will start the download of this very same file:

google-services

Hit ‘Continue’ and follow the instructions on the last step, which consist of adding this line:

classpath 'com.google.gms:google-services:3.0.0'

To the ‘build.gradle’ file that is in the project folder, not the app folder. And adding this line:

apply plugin: 'com.google.gms.google-services'

To the ‘build.gradle’ file that is in the app folder this time. Now, it is necessary to setup some configurations at Back4App.

Step 2 – Add FCM credentials to Back4App Dashboard

First, it is necessary to get FCM credentials. Go to your Firebase Console and locate this cog:

fb_cog

Then click on Project Settings, which will get you to a screen that has some tabs, one of which is the ‘Cloud Messaging’ tab, as below:

fb_cloudmessaging

Click on this tab and locate your Server Key and your Sender ID, as below:

fb_serverkey

Write down both of these keys. Now, go to your Dashboard and find this option:Android Push notification

 

Click at “Server” and you’ll be taken to a page that has an ‘Edit’ button. Click on it and you’ll be taken to the following screen:

b4a_keys

Then, write on the field ‘GCM Sender ID’ your Firebase Sender ID and on the ‘API Key’, your Firebase Server Key. Hit ‘Next’ and you’re set with the Backend! Now you need to set up the libraries on the App.

Step 3 – Setup libraries and add permissions

Now, we have to add to the ‘build.gradle’ file that is in the app folder the following lines in the dependencies section:

dependencies {
    ...
    compile 'com.parse.bolts:bolts-android:1.+'
    compile 'com.parse:parse-android:1.+'
    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    ...
}

This setups the libraries. After that, go to your ‘AndroidManifest.xml’ file and add the following lines right after the ‘manifest’ tag:

<manifest xmlns:android= ...
    ... >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

So that your App has permissions required to use Push Notifications full capabilities. Right below these lines, add the following lines, but remember to complete the space with your package name:

<permission android:name="<Your package name here>.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="<Your package name here>.permission.C2D_MESSAGE" />

Now, we have to add the Sender Key to the ‘AndroidManifest.xml’ file. Right below the ‘</activity>’ section, add:

<activity ...
    ...>
    ...
</activity>
<meta-data android:name="com.parse.push.gcm_sender_id"
    android:value="id:<Your Firebase Sender Key here>" />

And change the fields to have your Firebase Sender Key in the right place. After this, we have to grant Parse API permission to receive push notifications. Immediately below the Sender Key you just added, add this piece of code:

<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="<Your Package name here>" />
  </intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false">
  <intent-filter>
    <action android:name="com.parse.push.intent.RECEIVE" />
    <action android:name="com.parse.push.intent.OPEN" />
    <action android:name="com.parse.push.intent.DELETE" />
  </intent-filter>
</receiver>

Note that you have to modify fields accordingly, in this case by adding your package name where it is asked to. Now, we have to create an installation, so that Parse API can receive the notifications.

Step 4 – Create Installation

Right after your Parse initialization, add the setup of the ‘Installation’:

// Back4App's Parse setup
Parse.initialize(new Parse.Configuration.Builder(this)
        .applicationId("<Your App ID here>")
        .clientKey("<Your client Key here>")
        .server("https://parseapi.back4app.com/").build()
);

// This is the installation part
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "<Your Firebase Sender Key here>");
installation.saveInBackground();

Just remember to fill the fields with your own informations, such as your App ID and Client Key, as well as your Firebase Sender Key. Now you are set to use the Push Notifications! To test it, just go to your Parse Dashboard and find ‘Push’. Locate ‘Send New Push’:
pushnotif_send_new_push

 

Click on it and there will be a screen to test your push notifications on your right. Select the ‘Audience’ you wish, type a message and click ‘Send push’:

pushnotif_sendpush

Here is a screenshot of the app working:

push_app

And that’s it for Push Notifications with Dashboard!