Android Push Notifications

Contents: [ Enabling Google Cloud Messaging in your Firebase Console , Uploading Your Google API Key to Mixpanel , Setting up Your AndroidManifest.xml , Importing Device Tokens , Send a Push Notification , Learning More ]
[Adding Firebase to your Android Application, Configuring Cloud Messaging in Firebase]

Visit the Dev Docs

A better version of this page exists at https://developer.mixpanel.com/docs/android-push-notifications.

This guide will show you how to configure an Android app to send and receive push notifications. You should first read the Android API reference.

In order to send Push Notifications, you must store Mixpanel People Profiles and successfully be distinguishing users using the distinct_id.

You should be comfortable getting access to the MixpanelAPI object using your Mixpanel API key and calling MixpanelAPI.getPeople().identify with a distinct_id to identify your users before sending push notifications.

Enabling Google Cloud Messaging in your Firebase Console

To use Mixpanel push notifications, you must have a Firebase project in your Firebase Console. If you do not already have one, you must create a new Firebase project.

Instructions on adding a new Firebase project can be found here.

Adding Firebase to your Android Application

Once you have created a new project, proceed to the project Overview page, and select the icon to add Firebase to your Android app.

When prompted, enter your application’s Package Name. This can be found in your AndroidManifest.xml file.

<!--The package name is the string equal to "package" below-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="mixpanel.example.com.sampleapp">
  
  ... 

Select ADD APP after entering the name.

Firebase will then generate a google-services.json file that you must add to your application’s “app” directory.

Next, add the Google Play Services dependencies to your application's build.gradle files.

There is one build.gradle file at the "app" level and one at the "project" level. Add the dependencies to both.

Configuring Cloud Messaging in Firebase

After you’ve added your app to Firebase, you must configure the "Cloud Messaging" settings. Select the app options gear icon from the Firebase Console.

Select the Cloud Messaging tab in your Settings. Here, you can find your API Key to upload to Mixpanel along with the Sender ID (you’ll need this later to generate tokens).

Uploading Your Google API Key to Mixpanel

In order for Mixpanel to send Firebase Cloud Messaging notifications on your behalf, you will need to enter the Google API key generated from the last step into Mixpanel. To upload it, log in to your Mixpanel project, click the settings gear in the upper righthand corner of your Mixpanel project, and select the Mixpanel Project from the "Project settings" from the dropdown.

Click Messages from the project settings overview.

Enter "Android FCM API Server Key" line, and paste in your Google API key into the text field that appears. Click the Save button to confirm.

Setting up Your AndroidManifest.xml

You will need to edit your Android application's AndroidManifest.xml to properly declare the FCM receiver.

You will need to include the following block in your AndroidManifest.xml file:

<application>
     ...
     <service
        android:name="com.mixpanel.android.mpmetrics.MixpanelFCMMessagingService"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                 <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
      </service>
     ...
</application>

Importing Device Tokens

If you already have your users' device tokens, you can import into Mixpanel by making API requests to the /engage endpoint and setting the special property $android_devices to a list of the user's Android device tokens in hex.

Your request should be formatted as a JSON dictionary, with $distinct_id and $token set as usual. $ip should be set to zero as to not update the users location incorrectly. $union should be set to a dictionary which has its $android_devices key pointing to an array of the users' device tokens in hex. A $union record operates on list properties and results in a list that contains only unique items (i.e., no duplicates). We use $union here, as opposed to $append so that the user's $android_devices list only contains one copy of each device ID.

Example:

{
    "$distinct_id": "123456",
    "$token": "36ada5b10da39a1347559321baf13063",
    "$ignore_time": true,
    "$ip": "0",
    "$union": {
        "$android_devices": ["2ffca4ad6599adc9b5202d15a5286d33c19547d472cd09de44219cda5ac30207"]
    }
}

After base64 encoding, the above would be sent as this request:
http://api.mixpanel.com/engage/?data=ew0KICAgICIkZGlzdGluY3RfaWQi OiAiMTIzNDU2IiwNCiAgICAiJHRva2VuIjogIjM2YWRhNWIxMGRhMzlhMTM0NzU1O TMyMWJhZjEzMDYzIiwNCiAgICAiJGlnbm9yZV90aW1lIjogdHJ1ZSwNCiAgICAiJH VuaW9uIjogew0KICAgICAgICAiJGlvc19kZXZpY2VzIjogWyIyZmZjYTRhZDY1OTl hZGM5YjUyMDJkMTVhNTI4NmQzM2MxOTU0N2Q0NzJjZDA5ZGU0NDIxOWNkYTVhYzMw MjA3Il0NCiAgICB9DQp9

Send a Push Notification

Once you have set up AndroidManifest.xml file you're ready to send a notification.

Install and run your application on an Android device (not the emulator, it can't receive messages). Make sure to run the app until the calls to MixpanelAPI.getPeople.identify has been run. For apps built according to our recommendations, these calls are in the onCreate method of your main application activity, so it is enough to simply open the app.

Press the back button to shut down your app. You should already be calling MixpanelAPI.flush() in the onDestroy method of your main application activity, so closing it should send all waiting messages to Mixpanel.

Now log in to your Mixpanel project and select Explore from the left-hand navigation. There should be a user in the list with an "Android Devices" property. Select the user and click Push Notification.

Compose your message, schedule it to send immediately, and click Send this message.

The message should show up on your device.

Learning More

Mixpanel has a short sample Android application on github that includes support for push notifications and demonstrates some of our recommended practices.

We also recommend you take a look at the complete Mixpanel Android API reference, which provides detailed information for all objects and methods available in the Mixpanel library.

Is this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.