Push Notification Setup (Android)

Top  Previous  Next

Backendless can deliver published messages as push notifications to Android devices. Additionally, Backendless Console can be used to publish push notifications. In order to deliver a push notification to Android, the backend must be configured with Google API Key:

1.Login to Google Services Console.
2.Click the Get started. Pick a platform button.
server-key-step1
3.Click Enable services for my Android App:
server-key-step2
4.Select your Android app from the list. if the app is not in the list, enter the name of the app, it will be automatically registered in the Google Developer Console.
5.Enter Android package name of your app.
6.Click Continue to Choose and configure services.
server-key-step3
7.Click Cloud Messaging and then click Enable Google Cloud Messaging:
server-key-step4
8.Google generates API key and sender ID. Copy Server API Key:
generated-ids
9.Open Backendless Console and select your application.
10.Click Manage and scroll down to Mobile Settings.
11.Paste the Google API Key into corresponding field located under the Android Push Notifications label:
google-api-key-backendless-console
12.Click Save. At this point the backend is configured and is ready to publish push notifications to Android devices.

 

Google Project Number (Sender ID)

In your project you should register the device in order to receive or send push notifications. To accomplish this, do the following:

1.Return to Step 8 in the instructions above.
2.Copy Sender ID.
3.Use the Sender ID value in Backendless.Messaging.registerDevice(...) method as GCMSenderID argument. For example:
google-project-number-in-code

Manifest Configuration

Android applications must also include special configuration into the application manifest file to enable support for Backendless Push Notifications:

1.Add the following permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="[APP PACKAGE NAME].permission.C2D_MESSAGE"/>
<permission android:name="[APP PACKAGE NAME].permission.C2D_MESSAGE" 
               android:protectionLevel="signature"/>

where [APP PACKAGE NAME] is the top level package name, it must be the same value as the package attribute in the manifest root element.

2.Add receiver declaration into the <application> element in the manifest file:
<receiver android:name="com.backendless.push.BackendlessBroadcastReceiver" 
             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="[APP PACKAGE NAME]"/>
  </intent-filter>
</receiver>
<service android:name="com.backendless.push.BackendlessPushService" />

where [APP PACKAGE NAME] is the top level package name, it must be the same value as the package attribute in the manifest root element.

Receiver is responsible for processing incoming push notifications.

 

Notice the value for the android:name attribute in the <receiver> and <service> elements - The receiver class is com.backendless.push.BackendlessBroadcastReceiver. While the service is com.backendless.push.BackendlessPushService. These are the default implementations. They contain the logic for processing push notifications and displaying them in the device's Notification Center. The implementation can be extended in order to get access to the published messages and/or handle push notifications in a custom way.

 

Custom handling of push notifications requires an implementation of two classes. First create a class which extends com.backendless.push.BackendlessPushService. Your implementation can override each of the four methods which correspond to various GCM actions.

Notice the comment in the onMessage method. The implementation of the method in the BackendlessPushService class contains the logic of displaying push notifications in the Android Notification Center. To cancel the default handling of the push notification, make sure your code returns false.

package com.myPackage;

import com.backendless.push.BackendlessPushService;

public class MyPushService extends BackendlessPushService
{

  @Override
  public void onRegistered( Context context, String registrationId )
  {
    Toast.makeText( context, "device registered" + registrationId, Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onUnregistered( Context context, Boolean unregistered )
  {
    Toast.makeText( context, "device unregistered", Toast.LENGTH_SHORT).show();
  }

  @Override
  public boolean onMessage( Context context, Intent intent )
  {
    String message = intent.getStringExtra( "message" );
    Toast.makeText( context, "Push message received. Message: " + message, Toast.LENGTH_LONG ).show();
    // When returning 'true', default Backendless onMessage implementation will be executed.
    // The default implementation displays the notification in the Android Notification Center.
    // Returning false, cancels the execution of the default implementation.
    return false;
  }

  @Override
  public void onError( Context context, String message )
  {
    Toast.makeText( context, message, Toast.LENGTH_SHORT).show();
  }
}

Once a custom service class is defined, it has to be registered with GCM. Backendless handles it through a custom implementation of broadcast receiver. Create a class that extends com.backendless.push.BackendlessBroadcastReceiver. Your implementation must override the getServiceClass() method which returns the Class object of the service implementation:

package com.myPackage;

import com.backendless.push.BackendlessBroadcastReceiver;
import com.backendless.push.BackendlessPushService;

public class MyPushReceiver extends BackendlessBroadcastReceiver
{
  @Override
  public Class getServiceClass()
  {
    return MyPushService.class;
  }
}
. Make sure to modify the manifest file to reflect your class implementations:
<receiver android:name=".MyPushReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
    <category android:name="com.myPackage"/>
  </intent-filter>
</receiver>
<service android:name=".MyPushService" />

 


Please let us know how we can improve the documentation by leaving a comment. All technical questions should be posted to the Backendless Support forum. We do not respond to the technical questions on the documentation pages.: