Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create notifications that don't go away when clicked in Android?

int icon = R.drawable.icon4;        
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis();         
Context context = getApplicationContext();     
CharSequence contentTitle = "Hello";  
CharSequence contentText = "Hello";      
Intent notificationIntent = new Intent(this, Example.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

This won't work for me.

How could I create a notification that is clickable and goes to my app, but does not go away when clicked?

like image 399
Ryan Ciehanski Avatar asked Nov 25 '11 04:11

Ryan Ciehanski


People also ask

What is permanent notification?

A permanent notification is a (usually silent) alert displayed by certain apps on your Android smartphone or tablet at all times to let you know they're running in the background.

How do I save notifications on Android?

Open the Settings on Your Android devices, scroll down and select Accessibility. Now in previous versions of Android, you might find the Notification Reminder settings right inside Accessibility. But in the Android 8 and 9, you will see Advanced Settings, select it. And then inside it select Notification reminders.

What is sensitive notification?

Android allows you to hide what it calls “sensitive content” from notifications on your lock screen. The notification will still appear, but the content of it will be hidden. What exactly qualifies as a “sensitive” notification is up to the app developers, so it can vary.


2 Answers

You should read the whole things not just a part, buddy. Please re-read carefully step-by-step.

// this
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.icon4;        
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis();         
Context context = getApplicationContext();     
CharSequence contentTitle = "Hello";  
CharSequence contentText = "Hello";      
Intent notificationIntent = new Intent(this, Example.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

// and this
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
like image 175
Pete Houston Avatar answered Sep 18 '22 14:09

Pete Houston


int icon = R.drawable.ic_launcher;
long when = 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

like image 31
Kishan Dhamat Avatar answered Sep 18 '22 14:09

Kishan Dhamat