Notification Alert Android App Using Android Studio

In this article, I will show you how to create a Notification Alert Android App using Android Studio.

Introduction

In this article, I will show you how to create a Notification Alert Android app using Android Studio.

Requirements

Steps to be followed.

Follow these steps to create a Notification Alert app using Android Studio. I have attached the source code too.

Step 1

Open Android Studio and start a new Android project.

Notification Alert Android App

Step 2

You can choose your application name and choose where your project is stored on the system. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Notification Alert Android App

Step 3

Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role to run the application.

Notification Alert Android App

Step 4

Now, add an Activity and click "Next" button.

Notification Alert Android App

Step 5

Add the Activity name and click "Finish".

Notification Alert Android App

Step 6

Go to activity_main.xml. This XML file contains the designing code for our Android app.

Notification Alert Android App

The XML code is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:id="@+id/rl"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:padding="16dp"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#a3aee1"  
  11.     >  
  12.     <Button  
  13.         android:id="@+id/btn"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Notification"  
  17.         android:layout_margin="50dp"  
  18.         />  
  19. </RelativeLayout>  

Step 7

Go to  MainActivity.java. This Java program is the backend language for Android.

Notification Alert Android App

The java code is given below.

  1. package abu.notification;  
  2. import android.app.NotificationManager;  
  3. import android.content.Context;  
  4. import android.content.res.Resources;  
  5. import android.graphics.BitmapFactory;  
  6. import android.support.v4.app.NotificationCompat;  
  7. import android.support.v7.app.AppCompatActivity;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.RelativeLayout;  
  12. import android.graphics.Bitmap;  
  13. public class MainActivity extends AppCompatActivity {  
  14.     private Context mContext;  
  15.     private Resources mResources;  
  16.     private RelativeLayout mRelativeLayout;  
  17.     private Button mButton;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         mContext = getApplicationContext();  
  23.         mResources = getResources();  
  24.         mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);  
  25.         mButton = (Button) findViewById(R.id.btn);  
  26.         mButton.setOnClickListener(new View.OnClickListener() {  
  27.             @Override  
  28.             public void onClick(View view) {  
  29.                 NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);  
  30.                 builder.setSmallIcon(R.drawable.notification_small_icon);  
  31.                 Bitmap bitmap = BitmapFactory.decodeResource(mResources, R.drawable.notification_large_icon);  
  32.                 builder.setLargeIcon(bitmap);  
  33.                 builder.setContentTitle("Notification Title");  
  34.                 builder.setContentText("Hello! Notification service.");  
  35.                 int notificationId = 001;  
  36.                 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  37.                 manager.notify(notificationId, builder.build());  
  38.             }  
  39.         });  
  40.     }  
  41. }  

Step 8

Now, go to menu bar and click "Make Project" or press ctrl+f9 to debug the error.

Notification Alert Android App

Step 9

Then, click the Run button or press shift+f10 to run the project. And, choose the virtual machine option and click OK.

Notification Alert Android App

Conclusion

We have successfully created the Notification Alert app using Android Studio.

Notification Alert Android App

Notification Alert Android App