Amazon Gift Cards

Saturday, March 28, 2009

Notification in Android

Notification in android is handy way to notify your user. Here is a very simple example of it. We would combine it with WebView with ZoomControls in Android.

First of all rename the Main class on the WebView tutorial to WebActivity making it
public class WebActivity extends Activity {
private WebView myWebView;


Then import the following to your manifest file
<application android:icon="@drawable/icon" android:label="@string/app_name">
  .....
  <activity android:name=".WebActivity"/>
</application>
<uses-permission android:name="android.permission.INTERNET">


public class Main extends Activity implements View.OnClickListener {
  private Button Button01;
  private NotificationManager mManager;
  private static final int APP_ID = 0; 

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.Button01 = (Button) this.findViewById( R.id.Button01);
    this.Button01.setOnClickListener(this);

    mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  }

  @Override
  public void onClick(View v) {
    Intent intent = new Intent(this,WebActivity.class);

    Notification notification = new Notification(R.drawable.icon,
"Notify", System.currentTimeMillis());
    notification.setLatestEventInfo(Main.this,
"App Name","Description of the notification",
PendingIntent.getActivity(this.getBaseContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
    mManager.notify(APP_ID, notification);
  }
}



Quick Explanation:
(Most of it is explained on the android dev site)

mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Call the notification service

Notification notification = new Notification(R.drawable.icon,"Notify", System.currentTimeMillis());
*** Create a new notification with an icon and label and do the notification now. See Notification Class

notification.setLatestEventInfo(Main.this,"App Name","Description of the notification",PendingIntent.getActivity(this.getBaseContext(), 0, intent,PendingIntent.FLAG_CANCEL_CURRENT));
*** Set the info of the notification, see Notification Class

mManager.notify(APP_ID, notification);
Notify the notification manager about our new notification and pass an id for it

Output:


Reference:
PhotoStream Source

Update History
   Jan 17, 2012 - Visual Update

9 comments:

Paul said...

How about having button in your notification???

Anonymous said...

Check here for more options in notificatoin button

http://www.firstdroid.com/2010/05/09/learn-by-example-using-notification-bar/

Adrian.

azelez said...

If you want to see more Learn by example tutorial, there is another nice tutorial about using the notification at:

http://www.firstdroid.com/2010/05/09/learn-by-example-using-notification-bar/

or the forum:

http://www.firstdroid.com/forum-2/?wpforumaction=viewtopic&t=10.0

BR,
Adrian.

saran said...

Your android notes is very useful for me...You have done a great job...Thank you so much for giving such a wonderful tutorial notes about Android...Plz just keep going...

Anonymous said...

How can I set a date and Time for the notification, i notice that it only uses System.currentTimeMillis(), is there any way that I can use a specific day and time for the notification?
Thanks in advance.

Be Pakistan said...

Yes, you can provide whatever time you want the notification to occur like this

Calendar calendar = Calendar.getInstance();
calander.set(2010, 12, 12);

now use calendar.getMilliSeconds() instead of System.getMilliseconds(). Its will send notification on 12 December 2010

Anonymous said...

Easy and fine, thanks!

NeoX said...

Use this user friendly library at http://code.google.com/p/android-easy-library/, the library provides easy to use method for creating notification, to create a notification just use

EasyNotification en = EasyNotification(this,0);
en.showNotification();

See it is really easy with this library, and you can also customise the notification to your will.

nieza said...

is there any code that i can connect from mysql data to android notification? when i want to add new news from mysql table, the notification about the news will show in android..