Like Us on Facebook

How to create a push notification in Android studio

Hi friends, in this tutorial how to create push notification in Android for any android Apps. All of you know Push notification has become very important features for many latest Android Application. As you know parse is going to close soon we need to  move to another backend website for sending push notification. One such alternatives are Onesignal.com. Using this we can send image notification and also give you the clear statistics about push notification.

How to create a push notification in Android studio
 

Prerequisites:

 
  • JDK 7.0 or Above
  • Android Studio 2.0
  • GCM Project ID and Server Api Key
  • OneSignal.com Account (Signup Here Its free)


 

Steps to follow


Step 1: Create a New Android Studio project name Androidpush.

Step 2: Select minimum API level15  so that it can support maximum of Android Device available in the google play.

Step 3: select blank Activity  andGive your layout xml name is activity_main And click finish .

Step 4: 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'



Step 6: Then add following  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"]
 


step 7: Now replace OneSignal App ID  in above code with One Signal App ID
      Also replace GCM Project ID with you ID .
 


Step 8:Then Open Your Main Activity.java  and add below lines of  under onCreate method below super.onCreate();
 
OneSignal.startInit(this)
        .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
        .init();



Step 9: Add the  following  method to  MainActivity
 
// 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, JSONObjectadditionalData, booleanisActive) {
        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();
        }
 


Step 10:  rebuild the Project and make sure it rebuilds the project without any errors. Now setup backend in your OneSignal.com dashboard for sending push notifications to android app

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 1 )

moha hashemi:
TNX :)
Admin:
its ok

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also