Start An Application From Notification Bar In 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 notifica
Solution 1:
To create a status bar notification, do this in your onCreate Method:
Get a reference to the NotificationManager:
Stringns= Context.NOTIFICATION_SERVICE; NotificationManagermNotificationManager= (NotificationManager) getSystemService(ns);
Instantiate the Notification:
int icon = R.drawable.notification_icon; CharSequence tickerText = "Hello"; longwhen = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when);
Define the Notification's expanded message and Intent:
Contextcontext= getApplicationContext(); CharSequencecontentTitle="My notification"; CharSequencecontentText="Hello World!"; IntentnotificationIntent=newIntent(this, MyClass.class); PendingIntentcontentIntent= PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Pass the Notification to the NotificationManager:
privatestaticfinalint HELLO_ID = 1; mNotificationManager.notify(HELLO_ID, notification);
That's it. Your user has now been notified.
Solution 2:
The accepted answer is deprecated. Here is the way to show a dialog, from google's documentation.
NotificationCompat.BuildermBuilder=newNotificationCompat.Builder(this).setSmallIcon(R.drawable
.logo_listy).setContentTitle("My notification").setContentText("Hello World!");
IntentresultIntent=newIntent(this, ResultActivity.class);
TaskStackBuilderstackBuilder= TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntentresultPendingIntent=
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManagermNotificationManager=
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
Solution 3:
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.
Solution 4:
You need to post a notification with a pending intent which contains an intent to start your app. See http://developer.android.com/guide/topics/ui/notifiers/notifications.html for how to do it in general and http://javablogs.com/Jump.action?id=628173 for a trap you may fall into.
Post a Comment for "Start An Application From Notification Bar In Android"