As all we know that Parse is shutdown we need move to another backend website for Android Push Notifications. One such alternative is www.OneSignal.com OneSignal.com also allow you to send Images as notifications and also give you clear statistics about your notifications. And Implementing OneSignal.com Push notifications in Android App is a cake Job.

Prerequisites for Implementing OneSignal.com Push Notifications in Your Android App.

  1. Android Studio Project
  2. GCM Project ID and Server Api Key ( Watch This Video to know how get GCM Credentials )
  3. OneSignal.com Account (Signup Here Its free)

Watch below video for good understanding.

 

Procedure:

Procedure Include 2 Parts 1. Android Side 2. OneSignal.com(Watch video)

  1. Android Side
  • Open your Android Studio project in which you want to add OneSignal.com push Notifications.
  • In project structure go to app and open build.gradle and add below lines in your dependencies
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.android.gms:play-services-ads:8.1.0'
compile 'com.google.android.gms:play-services-identity:8.1.0'
compile 'com.google.android.gms:play-services-gcm:8.1.0'
compile 'com.onesignal:OneSignal:2.+@aar'
compile 'com.google.android.gms:play-services-analytics:8.1.0'
compile 'com.google.android.gms:play-services-location:8.1.0'
  • Then add below lines to you defaultconfig in build.gradle below the versionName.
manifestPlaceholders = [manifestApplicationId: "${applicationId}",
                        onesignal_app_id: "OneSignal App ID",
                        onesignal_google_project_number: "GCM Project ID"]
  • Now replace OneSignal App ID  in above code with One Signal App ID (Watch video above to know how to get ID)
  • Also replace GCM Project ID with you ID (Watch This Video to know how get GCM Project ID)
  • Then Open Your Main Activity.java  and add below lines of  under onCreate method below super.onCreate();
OneSignal.startInit(this)
        .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
        .init();
  • Then add below method to your MainActivity.java file
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
    @Override
    public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
        try {
            if (additionalData != null) {
                if (additionalData.has("actionSelected"))
                    Log.d("OneSignalExample", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");

                Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
  • Then rebuild the Project and make sure it rebuilds the project without any errors.