How To Create Notifications That Don't Go Away When Clicked In Android?
Solution 1:
You should read the whole things not just a part, buddy. Please re-read carefully step-by-step.
// thisStringns= Context.NOTIFICATION_SERVICE;
NotificationManagermNotificationManager= (NotificationManager) getSystemService(ns);
inticon= R.drawable.icon4;
CharSequencetickerText="Hello"; // ticker-textlongwhen= System.currentTimeMillis();
Contextcontext= getApplicationContext();
CharSequencecontentTitle="Hello";
CharSequencecontentText="Hello";
IntentnotificationIntent=newIntent(this, Example.class);
PendingIntentcontentIntent= PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notificationnotification=newNotification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
// and thisprivatestaticfinalintHELLO_ID=1;
mNotificationManager.notify(HELLO_ID, notification);
Solution 2:
int icon = R.drawable.ic_launcher;
longwhen = System.currentTimeMillis();
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(context, 0, intent, 0);
Notification notification;
if (Build.VERSION.SDK_INT < 11) {
notification = new Notification(icon, "Title", when);
notification.setLatestEventInfo(
context,
"Title",
"Text",
pending);
} else {
notification = new Notification.Builder(context)
.setContentTitle("Title")
.setContentText(
"Text").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pending).setWhen(when).setAutoCancel(true)
.build();
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
nm.notify(0, notification);
Or you can Download direct tutorial from here : http://www.demoadda.com/demo/android/how-to-create-local-notification-notification-manager-demo-with-example-android-source-code_26
Solution 3:
If you are using Android 5.0 > it became a lot easier, functionallity changed, but you can use the same code.
//Some VarspublicstaticfinalintNOTIFICATION_ID=1; //this can be any int//Building the Notification
NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setContentTitle("BasicNotifications Sample");
builder.setContentText("Time to learn about notifications!");
NotificationManagernotificationManager= (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Make sure you are in an application context, if not you may need to pass the context and change your sourcecode as follows
NotificationCompat.Builderbuilder=newNotificationCompat.Builder(context);
...
..
.
NotificationManagernotificationManager= (NotificationManager) context.getSystemService(
context.NOTIFICATION_SERVICE);
You can see the full-source-code at:https://github.com/googlesamples/android-BasicNotifications/blob/master/Application/src/main/java/com/example/android/basicnotifications/MainActivity.java#L73
Solution 4:
Here is code
Intentintent=newIntent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntentpendingIntent= PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.BuildernotificationBuilder=newNotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManagernotificationManager=
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
Here if you want notifications that don't go away when clicked in Android? so set
setAutoCancel(false);
Solution 5:
if (android.os.Build.VERSION.SDK_INT>16)
{
notificationManager.notify(5, notification.build());
}else
{
notificationManager.notify(5, notification.getNotification());
}
To work in android.os.Build.VERSION.SDK_INT<16
keep in mind to do some changes
Post a Comment for "How To Create Notifications That Don't Go Away When Clicked In Android?"