Skip to content

Push Messaging

Nathan Esquenazi edited this page Apr 19, 2014 · 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.

Parse Push

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

Sending Push Notifications

Check out this official tutorial for a step-by-step guide to sending simple push notifications. 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

If you are using advanced targeting, you can have Parse trigger a particular activity to launch. This is pretty straightforward approach 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.

Push Broadcast Receiver

Receiving push notifications sent with Parse can be done using the BroadcastReceiver to manage the 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 excellent tutorial. Full source code for that 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

(Needs Attention)

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