Push Notifications: Pushing to your Android FCM client

Our Push Notifications system is currently in beta. This means you may experience errors, and the APIs may change. We would love to hear your feedback, so please get in touch!

Push notifications are triggered by your servers. After one of your Android applications subscribes to an interest, your server can then send a push notification to the Android application by publishing to that interest. Your server can publish using one of our many official server libraries. This page guides you through setting up your server to send push notifications.

Note: Can’t see your server language here? We’re busy adding push notification features to our server libraries. If yours isn’t done yet, you can use our HTTP API directly.

Hello world example

To get started, let’s send the time-honored “hello world” message to your Android application. We’ll assume your Android application has subscribed to the interest "donuts". You can use any of the below examples - they will all trigger the same push notification.

// Install the server library with:
//   composer require pusher/pusher-php-server

require __DIR__ . '/vendor/autoload.php';

$pusher = new Pusher\Pusher('APP_KEY', 'APP_SECRET', 'APP_ID', array('cluster' => 'APP_CLUSTER'));

$pusher->notify(
  array("donuts"),
  array(
    'fcm' => array(
      'notification' => array(
        'title' => 'hello world',
        'icon' => 'androidlogo'
      ),
    ),
  )
);
// Install the server library with:
//   npm install 'pusher@>=1.4.0'

var Pusher = require("pusher");

var pusher = new Pusher({
  appId: "APP_ID",
  key: "APP_KEY",
  secret: "APP_SECRET",
  cluster: "APP_CLUSTER",
});

pusher.notify(['donuts'], {
  fcm: {
    notification: {
      title: "hello world",
      icon: "androidlogo"
    }
  }
});
# Install the server library with:
#   gem install pusher -v '>= 1.2.0'

require 'pusher'

pusher_client = Pusher::Client.new(
  app_id: 'APP_ID',
  key: 'APP_KEY',
  secret: 'APP_SECRET',
  cluster: 'APP_CLUSTER'
)

class HelloWorldController < ApplicationController
  def hello_world
    pusher_client.notify(["donuts"], {
      fcm: {
        notification: {
          title: 'hello world',
          icon: 'androidlogo'
        }
      }
    })
  end
end
# Install the server library with:
#   gem install pusher -v '>= 1.2.0'

require 'pusher'

pusher_client = Pusher::Client.new(
  app_id: 'APP_ID',
  key: 'APP_KEY',
  secret: 'APP_SECRET',
  cluster: 'APP_CLUSTER'
)

pusher_client.notify(["donuts"], {
  fcm: {
    notification: {
      title: 'hello world',
      icon: 'androidlogo'
    }
  }
})
# Install the server library with:
#   pip install 'pusher>=1.4' --upgrade

from pusher import Pusher

pusher = Pusher(app_id=u'APP_ID', key=u'APP_KEY', secret=u'APP_SECRET', cluster=u'APP_CLUSTER')

pusher.notify(['donuts'], {
  'fcm': {
    'notification': {
      'title': 'hello world',
      'icon': 'androidlogo'
    }
  }
})

If you set up your app to subscribe to the donuts interest, you should have just received your first push notification!

Didn’t receive a push notification? Check the debug console!

If you published something to the donuts interest, but your running app did not receive it, you can find out more by using the push notifications debug console. In the Pusher dashboard, visit your app, go to “Debug Console”, then choose the “Push Notifications” tab. This console will log any errors that happen when sending your push notifications.

If you still did not receive a push notification, you can contact Pusher support.

Advanced options

Each of the notify methods accepts an object which specifies the push notification to send. This object is the language’s native representation of JSON. In the “hello world” examples above, the JSON description of our message was:

{
  "fcm": {
    "notification": {
      "title": "hello world",
      "icon": "androidlogo"
    }
  }
}

This is the simplest push notification you can send using FCM. There are many more advanced details you can specify. The full set of options for the FCM section of the notify call is described by Google in their documentation of FCM downstream HTTP messages.

Custom data

Any custom data fields you wish to include in your push notification must be provided under a data key in the fcm data structure. Any custom data fields specified outside of the data key won’t be pushed to the client.

{
  "fcm": {
    "notification": {...},
    "data": {
      "foo": "bar"
    }
  }
}

Congratulations, you’ve completed the Android push notifications guide! You can also set up your iOS application by following our iOS push notifications guide. After that, you can send to all your iOS and Android devices with a single call to notify!

Further reading

Have you tried using the search to find what you’re after? If you still have a question then get in touch with us and let us help you out.