Back to search

Send Push Notifications to your Android app

  • Jonathan Lloyd
January 18th, 2018
A basic understanding of Java and Node.js is needed to follow this tutorial.

Pusher’s Beams SDKs and API make it easy to send push notifications to all of your iOS and Android users with one request.

The service manages manages your app credentials and the lifecycle of device tokens which are required for every request to Google’s Firebase Cloud Messaging (FCM) service and APNs for your iOS devices.

You can subscribe your app user’s registered device to Interests, which act like persistent Pub/Sub topics, so that you can update many devices with the same request. Create as many interests as you need, and send unlimited push notifications.

Today we will walk you through the steps to start sending push notifications to your Android app users with Pusher’s Beams SDKs and API.

Step 1 - Sign up for a Pusher account

Before we can start building you will have to sign up for a Pusher account (or log in with your existing Pusher credentials).

Step 2 - Set up your free Beams instance

Head on over to the Beta Products Dashboard and create your first Beams instance. With a free plan, you can send unlimited push notifications to up to 10,000 devices during a billing month.

This will take you to our Quickstart wizard:

push-notifications-android-choose-platform

Select Android and follow the instructions. The Quickstart will guide you through the following steps:

Configuring FCM

The first step of the Quickstart involves configuring your credentials with FCM.

All push notifications requests for Android devices go through Firebase Cloud Messaging (FCM), Google’s service which delivers them to the device.

Pusher’s SDKs and API are abstracted above FCM as well as Apple’s APNs service so that you can deliver push notifications to both platforms with one request. Pusher manages the Server Key and Device Token for each request.

This page guides you through the process of getting an FCM Server Key.

Uploading your FCM Server Key

After you have configured FCM the next step is to upload your FCM Server Key through the Quickstart wizard:

push-notifications-android-fcm-key

Step 3 - Integrating the Beams SDK into your Android project

Now you will integrate the Beams SDK into your Android project. This will send the Device Token to Pusher’s Beams service, which manages its lifecycle if it changes or if the user uninstalls the app.

You will start by adding the necessary dependencies to your project build.gradle:

    buildscript {
        ...

        dependencies {
           classpath 'com.google.gms:google-services:4.0.1'
        }
    }

And your app build.gradle:

    dependencies {
        ...

        implementation 'com.google.firebase:firebase-core:16.0.1'
        implementation 'com.google.firebase:firebase-messaging:17.1.0'
        implementation 'com.pusher:push-notifications-android:1.0.0'
    }

    // Add this line to the end of the file
    apply plugin: 'com.google.gms.google-services'

You can now register the Device Token with the Beams service. All you have to do is import the SDK:

    import com.pusher.pushnotifications.PushNotifications;

Then register the Device Token at the appropriate point in your application lifecycle and subscribe your device to an interest hello:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PushNotifications.start(getApplicationContext(), "YOUR_INSTANCE_ID");
        PushNotifications.subscribe("hello");
    }

When your server publishes a push notification to the interest hello, it will get passed to your app.

You can find more information on this step here.

Step 4 - Start sending notifications

Push notifications are triggered by your servers to your Beams service. After a device using your Android application subscribes to an interest on our service, your server can then send a push notification to that device by publishing to that interest.

In this example we’ll use Node.js. Firstly, let’s install the dependency:

    $ npm install pusher-push-notifications-node --save

In our JavaScript file, all we need to do is create a Beams instance with our app credentials shown on our dashboard. Then we simply call publish with an array of interests and a payload. Every device subscribed to the interests specified in the array will receive a push notification.

To send via FCM, the payload key will be fcm:

    const PushNotifications = require('pusher-push-notifications-node');

    let pushNotifications = new PushNotifications({
      instanceId: 'YOUR_INSTANCE_ID_HERE',
      secretKey: 'YOUR_SECRET_KEY_HERE'
    });

    pushNotifications.publish(
      ['hello'],
      {
        fcm: {
          notification: {
            title: 'Hello World',
            body: 'Hello!'
          }
        }
      }
    }).then((publishResponse) => {
      console.log('Just published:', publishResponse.publishId]);
    }).catch((error) => {
      console.log('Error:', error);
    });

If you run your mobile app and put it into background mode, then run this script – you should see your notification. Neat, huh?

Didn’t see the push notification? We recommend that you minimise your app by pressing the home button so that you see the push notification when it arrives.

You can send push notifications from your server by using one of our server SDKs. We currently support many languages including PHP, Python, Node.js to Java.

  • Beams

© 2018 Pusher Ltd. All rights reserved.

Pusher Limited is a company registered in England and Wales (No. 07489873) whose registered office is at 28 Scrutton Street, London EC2A 4RP.