New to Kinvey? Get started today with this tutorial or attend our getting started webinar.


Push helps you reach out to your users in a timely fashion with important messages. You can use it to inform users about updates to some piece of data, about new services available nearby or that they are winners in a game. Using Custom server code you can set up triggers which send push notifications to a targeted set of people based on changes in the data you specify.

Kinvey brings Android Push Notifications to you through an integration with Google Cloud Messaging (GCM) and iOS Push Notifications through an integration with AWS SNS. While push notifications can only be configured to be used by either iOS or Android devices, our APIs provide support for registering/unregistering devices and associating them with Kinvey users.


Console Setup

  1. Navigate to the Firebase Console and follow the guide to create a new project or select an existing project.
  2. Click the gear icon in the top left of the side menu and select Project Settings.
  3. Click on the Cloud Messaging tab and write down the Sender ID and Server Key (formerly known as Project Number).
  4. On Kinvey's console select your App.
  5. Navigate to Engagement and select Push.
  6. Click Configure Push.
  7. In the Android section, fill in the Sender ID and API Key fields with the respective Sender ID and Server Key values obtained in step 3.
  8. Click Save

Project Setup

If you don't have the Kinvey Android Client Library added to your project, first take a look at the getting started guide.

Next, obtain the Google Play Services Library by following Google's guide for installing Google Play Services, which will download google-play-services_lib to <myAndroidSdkDirectory>/extras/google/google_play_services/libproject/google-play-services_lib/. If you aren't using gradle, import this as a project into your IDE as an Android Library project and add it as a dependency to your application.

To use push features, you must use a property file for initializing the client. Take a look at the getting started guide's section on initializing from a property file first, and then you can add the following GCM specific properties:

# Required settings

To use GCM, gcm.enabled must be set to true. Replace mySenderID with your Sender Id (a.k.a. Project Number)obtained from your Google API Project.

Receiving push messages

You must implement two classes to receive and handle push notifications within your android application. First, create a new class which extends KinveyGCMService. This class provides 5 methods, one for each of the possible GCM actions. By overriding these methods, you can run any code when a message is received (for example, showing a notification or just writing to the logs).


public class GCMService extends KinveyGCMService {

    public void onMessage(String message) {

    public void onError(String error) {

    public void onDelete(String deleted) {

    public void onRegistered(String gcmID) {

    public void onUnregistered(String oldID) {

    //This method will return the WakefulBroadcastReceiver class you define in the next step
    public Class getReceiver() {
        return GCMReceiver.class;

    private void displayNotification(String message){
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

After defining the class which will handle receiving messages, you have to register this class with GCM. Create a new class in the same package as the above, and extend the Android class WakefulBroadcastReceiver.

public class GCMReceiver extends WakefulBroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(), GCMService.class.getName());
        GCMService.enqueueWork(context, (intent.setComponent(comp)), GCMService.class);

In the above code sample, replace com.myPackage.GCMService with the fully qualified name of the KinveyGCMService you created earlier. Also, visit your KinveyGCMService implementation and have the method getReceiver() return the WakefulBroadcastReceiver class you just created.

Manifest changes

To use GCM for push notifications, add the following permissions to your project's AndroidManifest.xml file within the <manifest> tags:

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="" />

Also add the following lines within the <manifest> tags, however replacing com.myPackage with your app's package name.

<permission android:name="com.myPackage.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.myPackage.permission.C2D_MESSAGE" />

Next, within your <application> tag, register the receiver and service you created earlier. Replace com.myPackage with your application's package, and ensure that the receiver name is set to match the custom WakefulBroadcastReceiver and the service name is set to match the KinveyGCMService.

        android:permission="" >
            <action android:name="" />
            <category android:name="com.myPackage" />
    <service android:name=".GCMService" />

Registering the Device

Now that you have configured your manifest and added the two necessary classes, it is necessary to register a device to receive Push notifications. To register the active user to receive push notifications on the current device, use the following:


After registration has completed, your application will be notified via the onRegistered(…) method of the KinveyGCMService.

Push registration occurs in the context of an initialized Kinvey client and a logged-in Kinvey user (either explicit or implicit).

Triggering Push Notifications

In addition to using the Console, you can trigger a push notification from Business Logic or a Flex service. This approach allows for greater flexibility as it makes it possible to schedule push notifications, to tie them down to various data events, and to set advanced options supported by Android and iOS.

You can send push notification from either collection hooks, effectively tying the notification to a data event, or custom endpoints which you can call manually or programmatically when needed.

The methods for sending push notifications are provided by the push module available in both Business Logic and the Flex SDK. They include send(), sendMessage(), sendPayload(), broadcastPayload(), and broadcastMessage(). Check the module's reference for detailed information.

The following code uses the onPreSave() before-processing collection hook to send a simple push notification to all users who fancy the winning team. Instead of saving any data to the data store, this request just causes the push notification to be sent.

function onPreSave(request,response,modules){
    var collectionAccess = modules.collectionAccess
   , userCollection = collectionAccess.collection('user')
   , utils = modules.utils
   , push = modules.push
   , template = '{{name}}, the {{team}} just won!'
   , pushedMessageCount = 0
   , userCount;

   // Find all users whose favoriteTeam matches the incoming request's winning team
   userCollection.find({"favoriteTeam": request.body.winningTeam}, function(err, userDocs){
     // Total number of messages to send
     userCount = userDocs.length;

     // Each message is customized
       var values = {
          name: doc.givenName,
          team: doc.favoriteTeam

       // Render the message to send
       var message = utils.renderTemplate(template, values);

       // Send the push
       push.send(doc, message);

       // Keep track of how many pushes are sent

       // Reduce the number of users left to push to
       if (userCount <= 0){
         // All pushes sent out, complete the request
         response.body = {"message": "Attempted to push " + pushedMessageCount + " messages."};

The body of the request must have a winningTeam property. For example:

  "winningTeam": "Boston Red Sox"

Users are expected to have a givenName property and a favoriteTeam property.

The Push notification will be similar to:

Joe, the Kansas City Royals just won!

Disabling Push Notifications

The unregister operation allows you to remove the current device from the user account's list of registrations, effectively stopping the user from receiving push notifications on the device. The operation is useful when the user has opted out of receiving this kind of notifications or for another reason. Unregister requires an active user. It will return an error if you call it after logging out the user.

Upon unregistering the active user, FCM replies with a confirmation which is caught by the onUnregistered handler inside the KinveyGCMService class.

Got a question?