Skip to content

Push Messaging

Roger Hu edited this page Mar 29, 2015 · 65 revisions

Overview

This guide will show you how to configure an Android app to send and receive push notifications. There are several approaches to sending push notifications. The two most common are:

Parse Push

First approach is to use the Parse Push service to send and receive push notifications.

Setup

Check out this official tutorial for a step-by-step guide to setting up push notifications with Parse. You can also review this Parse Setup Guide for full instructions.

Sending Push Notifications

Check out this official tutorial for a step-by-step guide to sending simple push notifications.

Make sure to go to the Settings -> Push section and toggle on Client push enabled. Otherwise, you may notice test push notifications work but sending between Android devices doesn't work.

ParsePush push = new ParsePush();
push.setChannel("mychannel");
push.setMessage("this is my message");
push.sendInBackground();

Full source code can be found on Github. For full details and options, check out the official parse Push guide.

Running into issues? Check out the push troubleshooting guide. Also compare your app with this sample reference app.

Receiving Push Notifications

Check out the Receiving Push Guide for a basic overview of receiving Push messages within an Android app. There are two basic ways to receive push notifications.

Push Invoking Activity

Normally, Parse will open the default activity that is designated as the main launcher in your AndroidManifest.xml file, but you can have Parse trigger a different activity to launch. You can change the behavior by subclassing ParshPushBroadcastReceiver and overriding the getActivity() method to returning the Activity you prefer to launch instead:

import android.app.Activity;

public class YourBroadcastReceiver extends ParsePushBroadcastReceiver {

  ....
  protected Class<? extends Activity> getActivity(Context context, Intent intent) {
    return YourActivity.class; // the activity that shows up 
  }
  ....

}

You will also need to modify the default Parse Broadcast Receiver normally used and replace with this custom class name.

<receiver
    android:name="your.package.name.YourBroadcastReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
</receiver>

This way is pretty straightforward to bringing up an activity when a push is received but you can use a more custom approach if you need more flexibility with how a notification is handled.

Custom Push Broadcast Receiver

Receiving push notifications sent with Parse can also be done using the BroadcastReceiver and implementing the onReceive() method to manage incoming push events. Any activity can register to handle these broadcast events. If you want to implement the push notification receiver, then check out this tutorial. Full source code for this tutorial can be found on github.

Communicating with an Activity

Often you might want your BroadcastReceiver to communicate with a host activity. There can be a bit of confusion around how to access an activity from the receiver. The easiest way to communicate between an activity and a receiver is to have the receiver be defined as an inner class within the activity as explained here in detail. Once you've defined the receiver that way, accessing the internal state of the Activity is easy.

Creating Dashboard Notifications

By default, Parse will create dashboard notifications based on certain details of the push message that are sent if you specify the "alert" and "title" properties in the notification. In the event that you want to manually manage the notifications, all you have to do is avoid sending the alert or title so parse doesn't create the notification for you.

Details of setting up custom push notifications can be found in this guide. Check out the Notifications guide for a detailed look at creating and managing these notices. See also how to group notifications by modifying existing notices. This is useful if you want to avoid stacking a bunch of notifications and instead group messages or only show the latest one.

Source Code

We have a full demo of Parse Push sending and receiving which can be found on Github. Check out MyCustomReceiver and MainActivity.

Gotchas

A few quick things to help make implementing push notifications easier:

  • Make sure to register for the broadcasts using the LocalBroadcastManager with the LocalBroadcastManager.getInstance(this).registerReceiver method.
  • If you need to communicate between a receiver and then a second activity that is not currently in the foreground, you may consider persisting the notification to disk in SQLite. This way we can easily access that from another Activity once that activity switches to the foreground.

Google Cloud Messaging

Second approach is the more manual one using GCM. Google Cloud Messaging for Android (GCM) is a service that allows you to send data from your server to your users' Android-powered device, and also to receive messages from devices on the same connection.

A GCM implementation includes a Google-provided connection server, a 3rd-party app server that interacts with the connection server, and a GCM-enabled client app running on an Android device:

GCM Arch

In order to use GCM, we need to go through the following steps:

  1. Register with Google Console and enable GCM
    • Obtain Sender ID (Project Number)
    • Obtain Server API Key
  2. Integrate GCM into our Android app
    • Register in app for a GCM Registration ID
    • Transmit the Registration ID (Device ID) to our web server
    • Register a GCM Receiver to handle incoming messages
  3. Develop HTTP Server with GCM endpoints
    • Endpoint for registering a user with a device ID
    • Endpoint for sending a push notification to a specified set of device IDs

Read our Google Cloud Messaging cliffnotes for specific implementation details. Check out these external links below for reference:

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally