How to send Push Notifications to your Android app with Pusher’s Android SDK

email_not_tutorials.jpg

This tutorial is a step by step guide to set up push notifications in your android app using Pusher's Android SDK.

Introduction

Pusher’s Push Notifications 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 Push Notifications 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 Push Notifications instance

Head on over to the Beta Products Dashboard and create your first Push Notifications 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:

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:

Step 3 – Integrating the Push Notifications SDK into your Android project

Now you will integrate the Push Notifications SDK into your iOS project. This will send the Device Token to Pusher’s Push Notification 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:

1buildscript {
2        ...
3
4        dependencies {
5            classpath 'com.google.gms:google-services:3.1.0'
6        }
7    }

And your app build.gradle:

1dependencies {
2        ...
3
4        implementation 'com.google.firebase:firebase-messaging:11.6.2'
5        implementation 'com.pusher:push-notifications-android:0.9.9'
6    }
7
8    // Add this line to the end of the file
9    apply plugin: 'com.google.gms.google-services'

You can now register the Device Token with the Push Notifications 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:

1@Override
2    protected void onCreate(Bundle savedInstanceState) {
3        super.onCreate(savedInstanceState);
4        setContentView(R.layout.activity_main);
5        PushNotifications.start(getApplicationContext(), "YOUR_INSTANCE_ID");
6        PushNotifications.subscribe("hello");
7    }

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 Push Notifications 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 PushNotifications 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:

1const PushNotifications = require('pusher-push-notifications-node');
2
3    let pushNotifications = new PushNotifications({
4      instanceId: 'YOUR_INSTANCE_ID_HERE',
5      secretKey: 'YOUR_SECRET_KEY_HERE'
6    });
7
8    pushNotifications.publish(
9      ['hello'],
10      {
11        fcm: {
12          notification: {
13            title: 'Hello World',
14            body: 'Hello!'
15          }
16        }
17      }
18    }).then((publishResponse) => {
19      console.log('Just published:', publishResponse.publishId]);
20    }).catch((error) => {
21      console.log('Error:', error);
22    });

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.

Feel free to contact us on Twitter if we’re missing your language or with any other feedback about Pusher’s Push Notifications SDKs and API.