Skip to content

Android Push Notification Configuration

Ram edited this page Jun 9, 2016 · 35 revisions

Sending Android Push Notifications from AppVirality

This quick guide will take you through the steps required to configure your in-app referral push notifications.

STEP : 1

Enabling Google Cloud Messaging in your Google API Console

To start with, turn on the Google Cloud Messaging Services from Google's API Console Page

Alt text

Click on "Create Project" to create new project.Once you have created a new project (or if you already have an existing project), you will be taken to the console's dashboard. Take note of the project number (top pane of the page). This 12 digit number will be your Google Sender ID, which you will need to use in your code to register your application for push notifications.

Alt text

From the Google Developer Console page, select the "APIs" section under "APIs & auth" from the left-hand navigation.

Alt text

Find "Google Cloud Messaging for Android" in the list of services, and turn it on by clicking the switch at the top of the page. Then click "Credentials" under "APIs & auth" to generate an API key.

Alt text

Create an Google API key. Under "Public API access" click "Create new Key".

Alt text

You should see the following popup

Alt text

Click the "Server key" button. The next page will contain a field for IP address whitelisting - it is very important to leave this blank or else AppVirality will be unable to use the key.

Alt text

Click the "Create" button and your key will appear.

Alt text

STEP : 2

Receiving Push Notifications

If you are already using push notifications, you should be having user push registration ID which is required to send the push notification. Send the user push registration ID to AppVirality using the following code.

AppviralityAPI.UserDetails.setInstance(getApplicationContext())
.setPushRegID("User-Push-Registration-ID")
.Update(); 

If you are not already using the Android push notifications or do not have android push registration ID, please follow these steps to set up your App ready to receive the push notifications.

Get Push Registration ID

Use the class "GCMRegistration.java" available in "AppViralityUI" folder to register the user with GCM to acquire the user Push Registration ID. We have commented this class file, so, please un-comment this file and Change the following line in this file by replacing the text "YOUR-SENDER-ID" with your google project ID i.e sender ID.

static String SENDER_ID = "YOUR-SENDER-ID";  

After setting the sender ID, use the following method to get the user push registration ID

com.appvirality.GCMRegistration.registerGCM(getApplicationContext());

This method will fetch the user push registration ID from GCM and updates the same with AppVirality automatically. NOTE: If you are using the above method, you don't have to again call the following method to update the registration ID with AppVirality.

AppviralityAPI.UserDetails.setInstance(getApplicationContext())
.setPushRegID("User-Push-Registration-ID")
.Update(); 

Setting up your AndroidManifest.xml

Add the following permissions required to receive the push notifications.

To set up your permissions, Replace "YOUR_PACKAGE_NAME" with your own app's package name in the the following snippet of xml, and add them to your AndroidManifest.xml before the tag:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Now you'll need to inform the OS to allow a service to run that will handle inbound notifications. Replace "YOUR_PACKAGE_NAME"with your own app's package name in the the following snippet of xml, and add them to your AndroidManifest.xml inside of your tag:

<receiver android:name="com.appvirality.GCMReceiver"
          android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="YOUR_PACKAGE_NAME" />
    </intent-filter>
</receiver>

NOTE: Be sure to change the YOUR_PACKAGE_NAME to the name of your application package when you paste these tags into your manifest.

Receiving and Building the Push Notification

In order to build the Push notification we have provided a Class with the name "GCMReceiver.java" which is available in "AppviralityUI" folder.

 public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
            handleNotificationIntent(context, intent);
        }
    } 

STEP : 3

Configuring Push Notification at AppVirality

Set your Google API key

For AppVirality to send Google Cloud Messaging notifications on your behalf, you will need to submit the Google API key generated by following step 1 above.

Alt text

Login to AppVirality Dashboard and navigate to "Notifications" by clicking on left menu and then "Push Notifications" tab. Enter the Android GCM API Key in "Push Notification Setting(s)" section and "Save" it.

Configure Push Message Template

Alt text

To configure push messages, it is required to have a campaign created. If you have multiple campaigns, select the desired campaign and then continue to "Push Notification Template" section. Provide the details explained below and save.

  • Template Types — Choose from drop down, options provided are based on the events in referral cycle.
  • Status — On / Off, provides option to turn on or off push notification for the selected event.
  • Notification Service — GCM / Other, choose "GCM" if you are using Google Cloud Messaging(GCM). For third party services choose "Other".
  • Push URL — Required in case of third party push notification service. Provide the URL to send the post data configured.
  • Headers — Use in case of third party push notification service. Add the custom headers to be sent in the post request as per the specification provided by Third-Party service.
  • Message — Compose your message to be sent here. If you want the message to be personalized, we recommend to use "Replacement Tokens" provided on the right side pane. Ex:- %AppUserName% to address user with his/her name.

In case of GCM notification service, compose your desired message in the text-box. The push notification receiver will receive the message as part of data object as below.

    {
        "message":"YOUR DESIRED MESSAGE"
    }
Sending Custom/Complex JSON Message

Alt text

If complex JSON data need to be sent, then compose the message according to the your custom JSON format.

    {
        "message":"Custom Message to user",
        "sound":"default",
        "image":"refer1.jpg"
    }
Using Third Party Notification Services

Alt text

In case of Third party notification service, set the post data within the message text-box.

Testing Push Notifications

Alt text

In order to test push notifications, you have to register test devices on your AppVirality dashboard.

After registering test devices, click on "Test" button and choose the device to send test push notification and click on "Send". Now, AppVirality tries to send the post message configured and gives you an alert with either success or failure.

For any further support, feel free to email us at cs@appvirality.com

Need more Customization:

Please have a look at our Wiki page

  1. Getting Started With AppVirality Android SDK Integration
  2. AppVirality API for referral Growth Hack
  3. How to Add Test Devices & Test Referral Program
  4. Using Custom Domain
  5. Init Callback | hasReferrer | getUserKey
  6. Using Referral Code & Referral Link
  7. [Optional] Exclude Premium Users
  8. [Optional] Dynamic Share Message
  9. [Optional] Remind Later Settings
  10. [Optional] To Whom and When to Show Growth Hack
  11. [Optional] Reward Notifications or Web-hook Configuration
  12. [Optional] Android Push Notification Configuration
  13. [Optional] Personalized Welcome Screen
  14. Referral Program on Apps having Login and Logout