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.
Step 2
Open the "activity_main.xml" file and update it as in the following code.
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button android:id="@+id/notify"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:text=" Show Notification Button"
- android:onClick="triggerNotification"
- />
- <Button android:id="@+id/cancel"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- android:text="Cancel Notification Button"
- android:onClick="clearNotification"
- />
- </LinearLayout>
Step 3
Open the "MainActivity.java" file and update it as in the following code.
- package com.example.androidsecondapp;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- public class MainActivity extends Activity {
-
- private static final int NOTIFY_ME_ID=1337;
-
- private int count=0;
-
- private NotificationManager notifyMgr=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- notifyMgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- }
- public void triggerNotification(View v) {
-
- Notification notifyObj=new Notification(R.drawable.ic_launcher,"Notification message!",System.currentTimeMillis());
-
- PendingIntent i=PendingIntent.getActivity(this, 0,
- new Intent(this, NotifyActivity.class),0);
-
- notifyObj.setLatestEventInfo(this, "Notification Created","Click here to see the message", i);
-
- notifyObj.number=++count;
-
- notifyObj.defaults |= Notification.DEFAULT_VIBRATE;
-
- notifyObj.defaults |= Notification.DEFAULT_SOUND;
-
- notifyObj.flags|=Notification.FLAG_AUTO_CANCEL;
-
- notifyMgr.notify(NOTIFY_ME_ID, notifyObj);
- }
- public void clearNotification(View v) {
-
- notifyMgr.cancel(NOTIFY_ME_ID);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 4
Now create a new Java file as "NotifyActivity.java" with the following code:
- package com.example.androidsecondapp;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class NotifyActivity extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- TextView txt = new TextView(this);
- txt.setText("You have successfully opened the activity associated with the notification!");
- setContentView(txt);
- }
- }
Step 5
Open and update the manifest file as "AndroidManifest.xml" using the following code:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.androidsecondapp"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.androidsecondapp.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:label="@string/app_name"
- android:name=".NotifyActivity"></activity>
- </application>
- </manifest>
Step 6
See the output
Notification Button