DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android Push Notification Using Parse.Com

Android Push Notification Using Parse.Com

Master push notifications the easy way for your Android apps

Francesco Azzola user avatar by
Francesco Azzola
CORE ·
Sep. 11, 15 · Tutorial
Like (2)
Save
Tweet
Share
7.90K Views

Join the DZone community and get the full member experience.

Join For Free

Android push notification is a service used to send messages directly to the Android smart phones. Using this service, developers can send data to Android apps as soon as it is available, in this way Android app doesn't have to make requests to the server to know if new information are available.

Using Android push service, apps can save smartphone battery and reduce the network traffic: the user experience is also improved. 

There are several different methods that can be used to implement Android push notification, the standard way is using GCM (Google Cloud Messaging) but there are some alternatives very interesting like Parse.com, that is easier to use.


Parse.Com Set Up The Project

The first thing is creating a Parse account and configure a new app. This is very easy. Once you have all things done, it is time to create a new project in Android Studio and modify build.grade to include the parse library: 

dependencies {
    ...
    compile 'com.parse.bolts:bolts-android:1.2.1'
    compile 'com.parse:parse-android:1.10.1'
}


Now you can follow, the tutorial provided by Parse.com. In our case, we have created a ParseTutorialApplication that extends Application and it is used to configure the Parse connection:

public class ParseTutorialApplication extends Application {
 
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Application");
        Parse.initialize(this, "your key", "your key");
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }
}


By now you use the default receiver provided by the library, in the next paragraph you will see how to customise it.

If the project is configured correctly, you can try to send a push notification using the web interface:



Schermata%2B2015-09-08%2Balle%2B22.20.20





In your Android emulator, you should get the notification:


device-2015-09-01-225504



Please be sure that the emulator includes Google API.


Parse.Com Custom Receiver

Now it is time to customise the receiver so that we can support custom message and not only text messages. Customising the receiver, it is possible to implement the app logic like parsing the incoming message and show custom messages and so on. Looking back in the Manifest.xml, as broadcast receiver it was used the standard receiver com.parse.ParsePushBroadcastReceiver, now to customise its behaviour we can subclass it:

public class JSONCustomReceiver extends ParsePushBroadcastReceiver {
    @Override
    protected void onPushReceive(Context context, Intent intent) {
        .....
    }
}

and override the onPushReceiver so that it is possible to implement the app logic when the message is available. Let us suppose that the message is in JSON format like that:

{"message":"hello phone"}

In the onPushReceiver, the app parses the message: 

private String getData(String jsonData) {
        // Parse JSON Data
        try {
            System.out.println("JSON Data ["+jsonData+"]");
            JSONObject obj = new JSONObject(jsonData);
 
            return obj.getString("message");
        }
        catch(JSONException jse) {
            jse.printStackTrace();
        }
 
        return "";
}

Once the message content is available and extracted from JSON message, the app notifies it to the user using NotificationCompat and NotificationManager. 

// Create custom notification
NotificationCompat.Builder  builder = new NotificationCompat.Builder(context)
  .setSmallIcon(R.drawable.ic_not_alert)
  .setContentText(data)
  .setContentTitle("Notification from Parse")
  .setContentIntent(pendingIntent);
 
Notification notification = builder.build();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

where pendingIntent is the instance of the Intent that starts the Activity when user touches the push notification: 

// Add custom intent
Intent cIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
                              cIntent, PendingIntent.FLAG_UPDATE_CURRENT);


and finally the app notifies the message:

nm.notify(1410, notification);


The final result is shown in the picture below:

device-2015-09-06-222013

Notice we have customised the notification icon too.
At the end of this post you know how to send android push message using Parse.com, in the next posts you will learn how to use push notification to send messages generated by smart controllers like Arduino, stay tuned! 


Android (robot) push app

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes vs Docker: Differences Explained
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • A Brief Overview of the Spring Cloud Framework

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: