Introduction

In this article, I will tell you how to produce a notification by the notification manager in Android, but in this article, I will tell you how to generate a notification using a button in Android. To develop this application you can use the following procedure.
Step 1
As usual, create a new project file as in the following.
Newnotification.jpg
Step 2
Open the "activity_main.xml" file and update it as in the following code.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:paddingBottom="@dimen/activity_vertical_margin"
  4. android:paddingLeft="@dimen/activity_horizontal_margin"
  5. android:paddingRight="@dimen/activity_horizontal_margin"
  6. android:paddingTop="@dimen/activity_vertical_margin"
  7. tools:context=".MainActivity"
  8. android:orientation="vertical"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. >
  12. <Button android:id="@+id/notify"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:layout_weight="1"
  16. android:text=" Show Notification Button"
  17. android:onClick="triggerNotification"
  18. />
  19. <Button android:id="@+id/cancel"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:layout_weight="1"
  23. android:text="Cancel Notification Button"
  24. android:onClick="clearNotification"
  25. />
  26. </LinearLayout>
Step 3
Open the "MainActivity.java" file and update it as in the following code.
  1. package com.example.androidsecondapp;
  2. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.view.Menu;
  9. import android.view.View;
  10. public class MainActivity extends Activity {
  11. //Notification message ID
  12. private static final int NOTIFY_ME_ID=1337;
  13. //Counter
  14. private int count=0;
  15. //Create NotificationManager object
  16. private NotificationManager notifyMgr=null;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. notifyMgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  22. }
  23. public void triggerNotification(View v) {
  24. //Instantiate notification with icon and ticker message
  25. Notification notifyObj=new Notification(R.drawable.ic_launcher,"Notification message!",System.currentTimeMillis());
  26. //PendingIntent to launch our activity if the user selects it
  27. PendingIntent i=PendingIntent.getActivity(this, 0,
  28. new Intent(this, NotifyActivity.class),0);
  29. //Set the info that show in the notification panel
  30. notifyObj.setLatestEventInfo(this, "Notification Created","Click here to see the message", i);
  31. //Value indicates the current number of events represented by the notification
  32. notifyObj.number=++count;
  33. //Set default vibration
  34. notifyObj.defaults |= Notification.DEFAULT_VIBRATE;
  35. //Set default notification sound
  36. notifyObj.defaults |= Notification.DEFAULT_SOUND;
  37. //Clear the status notification when the user selects it
  38. notifyObj.flags|=Notification.FLAG_AUTO_CANCEL;
  39. //Send notification
  40. notifyMgr.notify(NOTIFY_ME_ID, notifyObj);
  41. }
  42. public void clearNotification(View v) {
  43. //Clear the notification
  44. notifyMgr.cancel(NOTIFY_ME_ID);
  45. }
  46. @Override
  47. public boolean onCreateOptionsMenu(Menu menu) {
  48. // Inflate the menu; this adds items to the action bar if it is present.
  49. getMenuInflater().inflate(R.menu.main, menu);
  50. return true;
  51. }
  52. }
Step 4
Now create a new Java file as "NotifyActivity.java" with the following code:
  1. package com.example.androidsecondapp;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;
  5. public class NotifyActivity extends Activity
  6. {
  7. @Override
  8. public void onCreate(Bundle savedInstanceState)
  9. {
  10. super.onCreate(savedInstanceState);
  11. TextView txt = new TextView(this);
  12. txt.setText("You have successfully opened the activity associated with the notification!");
  13. setContentView(txt);
  14. }
  15. }
Step 5
Open and update the manifest file as "AndroidManifest.xml" using the following code:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.androidsecondapp"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="17" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.example.androidsecondapp.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity android:label="@string/app_name"
  23. android:name=".NotifyActivity"></activity>
  24. </application>
  25. </manifest>
Step 6
See the output
Notification Button
NotificationButton.jpg
Notification Message
NotificationMessage.jpg