Android - Simple Notification

Submitted by: 
Operating System: 
Visitors have accessed this post 419 times.

In this tutorial we will create a Simple Notification using Android. This simple app can display a notification when there is something new that comes. Android is basically a piece of software which allows your hardware to function. It used in several gadget like smartphone, tablet, and even television. The android is an open source operating system it's free and user friendly to mobile developers. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplenotification.MainActivity">
  11.  
  12.  
  13. android:id="@+id/btn_notify"
  14. android:layout_height="wrap_content"
  15. android:layout_width="wrap_content"
  16. android:layout_centerInParent="true"
  17. android:text="Show Notification"/>
  18.  
  19.  
  20. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplenotification">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

The Main Function

This code contains the main function of the application. This code will prompt the user to receive the incoming notification. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.

  1. private Button btn_notify;

Then write these method to make to code work correctly.

  1. public void ShowNotification(View view){
  2. NotificationCompat.Builder builder;
  3.  
  4. builder = new NotificationCompat.Builder(this);
  5. builder.setAutoCancel(true);
  6.  
  7. builder.setSmallIcon(R.drawable.ic_stat_name);
  8. builder.setTicker("Notification Here");
  9. builder.setPriority(Notification.PRIORITY_HIGH);
  10. builder.setWhen(System.currentTimeMillis());
  11. builder.setContentTitle("Android");
  12. builder.setContentText("Waiting for your response");
  13.  
  14. Intent intent = new Intent(this, MainActivity.class);
  15. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  16. builder.setContentIntent(pendingIntent);
  17.  
  18. NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  19. notificationManager.notify(0, builder.build());
  20. }

Finally, initialize this method inside the onCreate method to run the application.

  1. btn_notify = (Button)findViewById(R.id.btn_notify);
  2.  
  3.  
  4. btn_notify.setOnClickListener(new View.OnClickListener() {
  5. @Override
  6. public void onClick(View v) {
  7. ShowNotification(v);
  8. }
  9. });

Try to run the app and see if it worked.

There you have it we have created a Simple Notification using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.