You can now send push notifications with all of the new features in Google’s latest platform — Firebase Cloud Messaging (FCM) — using the Mobile Push Notifications API. The API supports all of the major messaging platforms: Apple Push Notification Service (APNs), Google Cloud Messaging (GCM), and Firebase Cloud Messaging (FCM).

FCM is a cross-platform messaging solution. Google announced it at I/O 2016, and plans to continue adding features to FCM instead of Google Cloud Messaging (GCM), which is their current service for sending push notifications to Android.

FCM Features

When you send push notifications over FCM using Pusher’s Mobile Push Notifications API, you can take advantage of the features released at Google I/O, including:

Analytics: Firebase Analytics is a free app measurement solution that provides insight on app usage and user engagement. Firebase Analytics automatically logs events that correspond to your FCM push notifications and supports reporting on the impact of each campaign.

Dynamic Links: Firebase Dynamic Links are smart URLs that dynamically change behaviour to provide the best experience across different platforms. In your push notification payload, you can link directly to your particular content in your app, or you can create links that initiate app-specific logic such as applying a coupon code to a user’s account.

Sending Push Notifications with Pusher is easy

Pusher’s Mobile Push Notifications API makes it easy to send push notifications to all of your iOS and Android users with one request.

Subscribe your registered devices to Pusher interests, which act like Pub/Sub channels. You can create as many interests as you need, and send unlimited push notifications to them for free.

How To Get Up And Running With FCM

Set Up Your Pusher App

To get started with FCM, you’ll have to create a new Firebase project in the Firebase Developers Console. You can do this by either starting a new fresh project, or importing an existing Google project.

Follow these steps to set up your project for Android. By the end of it, you should have an FCM server key and a google-services.json configuration file. Place the configuration file in the app/ directory of your Android project.

To upload your FCM server key to pusher, visit the Pusher dashboard. You can sign up for a free account if you do not have one already. Create an app and under the Push Notifications tab, head to Configure FCM. Enter your FCM server key and click Save.

Now your app should be provisioned to send and receive FCM notifications.

Set Up Your Devices

Let’s start by adding the Pusher Android library and FCM libraries to your project.

In your app-level build.gradle file, add the following to your repositories:

repositories {
  maven { url 'http://clojars.org/repo' }
}

Let’s add Pusher and Firebase to your dependencies:

dependencies {
  // Firebase
  compile 'com.google.firebase:firebase-messaging:9.4.0'
  compile 'com.google.firebase:firebase-core:9.4.0'

  // Pusher
  compile 'com.pusher:pusher-websocket-android:0.2.1'
}

At the bottom of this file add:

apply plugin: 'com.google.gms.google-services'

In your project-level build.gradle file, let’s add Google services to our buildscript:

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

To register Pusher’s background services for interacting with FCM on your behalf, add the following to your AndroidManifest.xml, within the application tags:

<application>
  <service
      android:name="com.pusher.android.notifications.fcm.FCMMessagingService">
      <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT"/>
      </intent-filter>
  </service>

  <service
      android:name="com.pusher.android.notifications.fcm.FCMInstanceIDService">
      <intent-filter>
          <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
      </intent-filter>
  </service>
</application>

Now you should be all set up to start registering your device with Pusher. All we need to do is instantiate a PusherAndroid object with our app key, register passing in a Context, and subscribe to an interest (in this example, "kittens"):

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      PusherAndroid pusher = new PusherAndroid(<pusher_api_key>);
      PushNotificationRegistration nativePusher = pusher.nativePusher();
      nativePusher.registerFCM(this);
      nativePusher.subscribe("kittens");
    }
    // ...
}

Both registerFCM and subscribe take optional listeners so that you can check the progress of each operation. These are documented on the Github README. Furthermore, you can also register a callback to execute on receiving a notification. These are documented on the Github README.(https://github.com/pusher/pusher-websocket-android#receiving-notifications).

Now that we’ve reigstered our device with Pusher and subscribed to "kittens", we can start pushing!

Start Sending Notifications Via FCM

Currently the server libraries available for sending push notifications are:

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

$ npm install pusher

In our JavaScript, all we need to do is initialize Pusher with our app credentials shown on our dashboard. Then we simply call .notify on an array of interests, passing in a payload. To send via FCM, the payload key will be fcm:

var Pusher = require('pusher');

var pusher = new Pusher({
  appId: 'APP_ID',
  key: 'KEY',
  secret: 'SECRET'
});

pusher.notify(['kittens'], {
    fcm: {
        notification: {
            'title': 'Kitten Alert!',
            'body': '?',
            'icon':  'xxx'
        }
    }
})

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

The Mobile Push Notifications API is out and we would love to hear your feedback so that we keep adding features that help you build more with less code. Get in touch!