Android Push Notifications with GraphQL

Using GraphQL to enable Android GCM push notifications for mobile devices in 3 easy steps.

Vince Ning
Scaphold
5 min readFeb 14, 2017

--

Calling all Android mobile developers! Today, we’re going to walk you through how to set up Android push notifications for your GraphQL server.

It’s about time for mobile to get some GraphQL love. ❤️

We’re going to cut right to the chase with the 3 steps to get you set up right away with Android push notifications.

Prerequisite: You must have an actual Android device configured for development.

1. Create a Google API project to enable GCM.

We’re going to start off by creating an Android app. To make it easier for you, we’ve provided a starter kit that goes along with this guide.

Download the starter kit here.

Once we’ve created the app, we’ll want to connect it to a Google project. Please go to your Google Developer Console and create a new project.

Once you’ve created your Google project, you’ll have to also create a new Firebase project at the Firebase console.

Why do we have to go to Firebase? Google now only provides GCM push notification support through the Firebase console. 😤

Follow the steps to finish creating your app, then Add Firebase to your Android app.

We’ll need your Package Name (or Application ID). Find your package name here in your app:

The nickname is your choice. Your first step should look like this.

After adding the app, you’ll download a Google Service JSON file that you’ll need to import into your project. Place it in your app directory.

Great! Now you’ll need to update the following dependencies in your Android app.

  • App-level bundle.gradle:
  • Project-level bundle.gradle:

And finally, you’ll need to update your strings.xml constants file to reflect the App ID of the newly created Google project.

Nice work ✅ Your Android app and Google project are now connected!

2. Integrate your GraphQL server with GCM.

Now, it’s time to connect your GraphQL server with GCM so that you can actually send push notifications.

Let’s first grab your Server API Key that was auto-generated by Google after successful completion of the previous process of connecting your Android app with your Google project. You’ll need to find it in your Google Developer Console.

Open up the left side panel by clicking the top-left hamburger menu, and navigate to the API Manager.

Now click the Credentials tab on the left-hand menu to grab your Server API Key. Save this. You’ll need it in the next step to connect to your GraphQL server.

To make it easy, Scaphold.io provides an easy way to set up your very own GraphQL server for free in minutes. Sign up for an account and create an app. At this point, you instantly have a GraphQL server deployed in production, ready to make requests.

Now, go to Scaphold’s Integrations Portal to enable Android push notifications for development. Click Add to begin.

A form will then pop up, asking for your Server API Key. Fill out the appropriate fields and hit Create.

Good job! You’ve now connected your GraphQL server to be able to send push notifications to devices with your app installed.

3. Obtain a device token to send push notifications.

In order to send a push notification to a device, we need to generate a device token that is a unique identifier for a device and an app together. In that case, we’ll need to install your app onto an actual Android device.

But first, we need to create an event handler to receive the device token once a user permits the app to send push notifications to their phone. In your app, you’ll need to put the following snippet in your ExternalReceiver file (i.e. class that extends BroadcastReceiver):

We’re going to be using the boilerplate code from earlier to illustrate.

Now install your app on your Android device by pressing the big ▶️ button in the top bar of Android Studio.

Note: An actual device is needed since the simulator is transient and cannot have a device token associated to it.

Upon success, your device token will be issued by GCM and printed out in your Android Studio console.

Save this device token and send it to Scaphold to register it on your behalf with GCM. This take the form of sending a GraphQL mutation from your app, but for demonstration purposes, we’ll use GraphiQL to do so.

Congratulations! You’ve successfully configured your app and device to be able to send and receive Android push notifications. And the best part is that it was all done with GraphQL.

Now that your GraphQL server is set up for Android push notifications, the next part of this guide will walk you through how to…

  • Send push notifications
  • Associate users to device tokens
  • Manage push channels (i.e. groups)

Thanks for following along! We’ll walk you through sending your first push notification with GraphQL in the next part of this tutorial.

Looking for how to set up iOS push notifications with APNS? Follow along here!

Enjoy how easy that was to set up a GraphQL server? Join Scaphold today! And be sure to follow us on Twitter or join our Slack for more awesome ways to learn about how to quickly launch your next app with GraphQL!

--

--

Vince Ning
Scaphold

The service behind Scaphold’s GraphQL-as-a-service