Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start an application from notification bar in android

Tags:

android

I have an application, I want to show my app icon to the notification bar when my application is running and i also want when user will click on my app icon present in the notification bar my app will be open. How to do this? Please Help!

like image 416
Sudipta Som Avatar asked Jan 31 '11 10:01

Sudipta Som


3 Answers

To create a status bar notification, do this in your onCreate Method:

  1. Get a reference to the NotificationManager:

      String ns = Context.NOTIFICATION_SERVICE;
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    
  2. Instantiate the Notification:

      int icon = R.drawable.notification_icon;
      CharSequence tickerText = "Hello";
      long when = System.currentTimeMillis();
    
      Notification notification = new Notification(icon, tickerText, when);
    
  3. Define the Notification's expanded message and Intent:

      Context context = getApplicationContext();
      CharSequence contentTitle = "My notification";
      CharSequence contentText = "Hello World!";
      Intent notificationIntent = new Intent(this, MyClass.class);
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    
      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    
  4. Pass the Notification to the NotificationManager:

      private static final int HELLO_ID = 1;
    
      mNotificationManager.notify(HELLO_ID, notification);
    

    That's it. Your user has now been notified.

like image 119
Muhammad Shahab Avatar answered Nov 18 '22 00:11

Muhammad Shahab


The accepted answer is deprecated. Here is the way to show a dialog, from google's documentation.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable
            .logo_listy).setContentTitle("My notification").setContentText("Hello World!");

    Intent resultIntent = new Intent(this, ResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
like image 7
GLee Avatar answered Nov 17 '22 22:11

GLee


Few suggestions:

  • if you want icon in the notification bar, you must send some notification.
  • Application cannot be started by clicking on the notification icon. It may be started by clicking to the notification, that will be available if user pull-down notification bar. For that purpose you need to create PendingIntent.
like image 3
Zelimir Avatar answered Nov 18 '22 00:11

Zelimir