Step-by-Step Tutorial: Implementing Android Push Notifications

 

Android push notification :

In this android development tutorial we will deal with android push notification if you are new to this scenario please go through this tutorial completely.

Push Notification in apps are used to pass a message when app is either in active state or in active state .Generally push notification are mostly used in social networking apps to show a new message, and also it has usages in other different apps like news, eCommerce, shopping apps, games and many more … lets start android push notification tutorial

In this push notification tutorial i will show you how to push notification example in a local style i.e., data will be created and passed within same activity itself.

You can make use of this notifications when you did a task or want to alert user regarding the task.

 

pushnotification1

 

The push notification contains

--> Ticker Text // androidcoding.in
--> Title Text  // Welcome to android coding.in
--> Message     // You have a message
--> Subtext     // sub text

 

Creating Project in Android Studio :

Provide android push notification example project details for creation.

creating proj

 

Select

File -> New..->New Project

you will see the above screen

 

Fill in the project details, and choose the layout depending upon your requirement if you are beginners and don’t know what to choose, choose blank activity.

 

In layout file i have added the required number of  edittext, and buttons to create push notifications, you may change your design accordingly.

activity_main.xml :

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tickertxt"
        android:hint="ticker text"
        android:layout_marginTop="50dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/titletxt"
        android:hint="title text" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/notifytexttxt"
        android:hint="notify message" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/notifySubtexttxt"
        android:hint="sub text" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:layout_marginTop="50dp">

    <Button
        android:id="@+id/notifyBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/clearBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:layout_weight="1"/>

    </LinearLayout>
</LinearLayout>

 

In this MainActivity.java we have Declared our Push Notification without firebase.

Notification.Builder builder = new Notification.Builder(MainActivity.this);

 

And made a notification screen such that user entered text will get displayed as a Push Notification.

 

MainActivity.java File

Providing the full code for android push notification implementation.

 

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static java.lang.System.currentTimeMillis;

public class MainActivity extends ActionBarActivity {

    Button Create, Clear;
    EditText ticker,title,message,subtext;
    NotificationManager manager;
    Notification myNotication;

    String strTicker,strTitle,strMessage,strSubtext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Create = (Button) findViewById(R.id.notifyBtn);
        Clear = (Button) findViewById(R.id.clearBtn);

        ticker = (EditText) findViewById(R.id.tickertxt);
        title = (EditText) findViewById(R.id.titletxt);
        message = (EditText) findViewById(R.id.notifytexttxt);
        subtext = (EditText) findViewById(R.id.notifySubtexttxt);


        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Create.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                strTicker = ticker.getText().toString();

                strTitle = title.getText().toString();

                strMessage = message.getText().toString();

                strSubtext = subtext.getText().toString();

                Notify();

            }
        });

        Clear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                manager.cancel(11);
            }
        });
    }

   public void Notify() {

       Intent intent = new Intent("com.androidcoding.pushnotification"); 
            //used to move to another activity but i haven't used in this tutorial

       PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1,
                                                                       intent, 0);

       Notification.Builder builder = new Notification.Builder(MainActivity.this);

       builder.setAutoCancel(false);
       builder.setTicker(strTicker);
       builder.setContentTitle(strTitle);
       builder.setContentText(strMessage);
       builder.setSmallIcon(R.drawable.ic_launcher);
       builder.setContentIntent(pendingIntent);
       builder.setOngoing(true);
       builder.setSubText(strSubtext);
       builder.setNumber(1);
       builder.build();

       myNotication = builder.getNotification();
       manager.notify(11, myNotication);


   }
}

 

No Additional Permissions required as this is local data based Push Notification but when you are doing with server based push notifications you will require Internet Permission.

AndroidManifest.xml :

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidcoding.pushnotification">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

Push notification output :

This screen depicts the usage of push notification implementation in your app,

android push notification   android push notification    android push notification

 

 

 

 

 

 

 

If you have any query in this tutorial on push notification specify in comment section below.

Like and share this tutorial for more interesting updates.

Show Buttons
Hide Buttons
Read previous post:
Building a Robust SQLite Database in Android: Detailed Tutorial

Android sqlite example: In this android database tutorial we will learn how to use sqlite database in android app.Inserting, deleting of values...

Close