Skip to content

Push Notification Setup (Android)

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, your app must be configured to use Firebase Cloud Messaging the Backendless backend must be configured with a Google API Key. Detailed instructions for setting up FCM in your app can be found at:https://firebase.google.com/docs/android/setup, however, make sure to follow the instructions below as well.

  1. Login to Firebase Console and select or create a project.
  2. Add Firebase to your app. Start by clicking the Android icon and follow the instructions from the Google's documentation.
    add-firebase-shell
  3. Make sure the following dependencies are present in application's gradle.build file (usually app/build.gradle). Note that the version numbers will change, the numbers in the example below is the latest version at the time of this doc writing:

    implementation 'com.google.firebase:firebase-core:20.0.0'
    implementation 'com.google.firebase:firebase-messaging:23.0.0'
    

  4. Click the gear/settings icon located next to the Project Overview menu item and select Project Settings.
    firebase-settings

  5. Switch to the Service Accounts tab:
    firebase-service-accounts
  6. Click the Generate new private key button and confirm in the popup that follows. The result of these actions will be a .json file downloaded to your computer.
    generate-new-private-key
  7. Open Backendless Console and select your application.
  8. Click the Manage icon in the upper left corner and select the MOBILE tab.
  9. Click the ADD PRIVATE KEYS button in the Android Server Keys section.
    add-private-keys-fcm
  10. Browse to and select the private key file downloaded in Step 8. Make sure to map the key to a Backendless messaging channel. If you would like to use the key for all channels, use the All channels checkbox. Click the SAVE button to save the configuration.
    add-new-private-key-backendless
  11. Once the data is saved, the backend of your app is configured and is ready to publish push notifications to Android devices registered with your Backendless application.

App/Manifest Configuration

  1. Make sure to add Backendless SDK to your app per the documentation instructions.
  2. Backendless service for Android must be registered in the app's manifest as shown below:

    <service android:name="com.backendless.push.BackendlessFCMService">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
    </service>
    

  3. Use the device registration API. For example, the following API registers the device with both FCM and Backendless. It uses the Backendless default messaging channel:

    Future<dynamic> registerForPushNotifications() async {
        List<String> channels = [];
        channels.add("default");
    
        try {
          return await Backendless.messaging.registerDevice(channels, null, onMessage);
        } catch (ex) {
          return ex;
        }
      }
    
      void onMessage(Map<String, dynamic> message) async {
        PushNotificationMessage notification = PushNotificationMessage();
    
        if (io.Platform.isIOS) {
          Map pushData = message['aps']['alert'];
          notification.title = pushData['title'];
          notification.body = pushData['body'];
        } else if (io.Platform.isAndroid) {
          notification.title = message['android-content-title'];
          notification.body = message['message'];
        }
    
        showOverlayNotification((context) {
          return MessageNotification(
            title: notification.title,
            body: notification.body,
          );
        });
      }
    
    class PushNotificationMessage {
      String? title;
      String? body;
    }
    
    class MessageNotification extends StatelessWidget {
      const MessageNotification({Key? key, this.title, this.body})
          : super(key: key);
    
      final title;
      final body;
    
      @override
      Widget build(BuildContext context) {
        return Card(
            //construct notification
            );
      }
    }
    

  4. You can verify device registration in your Backendless backend. To do open Backendless console and navigate to the Data screen. The  device will appear in the the DeviceRegistration table:
    device-in-devregistration