7

I want to make my Android notification stay even if user clicks it or clicks clear all...

Right now it stays sometimes, and gets removed sometimes, and I'm not sure what causes it.

Here's my code for the notification:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void createNotification()
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                            .setContentTitle("Wolftech Field Agent")
                            .setContentText("Getting Status")
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setOngoing(true)
                            .setAutoCancel(false);

    Intent intent = new Intent(context, FieldAgent.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FieldAgent.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

public static void updateNotificationText(String inString)
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                                            .setContentText(inString)
                                            .setContentTitle("Wolftech Field Agent")
                                            .setSmallIcon(R.drawable.ic_launcher)
                                            .setOngoing(true)
                                            .setAutoCancel(false);

    Intent intent = new Intent(context, FieldAgent.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FieldAgent.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(pendingIntent);

    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

public static void cancelNotification()
{
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.cancel(NOTIFICATION_ID);
}
CeeRo
  • 1,142
  • 3
  • 12
  • 21

5 Answers5

11

I think you are looking for ongoing notifications, in that case if you are using NotificationCompat.Builder , you can use :

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
mNotifyBuilder.setOngoing(true);
madx
  • 6,723
  • 4
  • 55
  • 59
4

I believe that you are looking for this flag:

Notification.FLAG_NO_CLEAR

Add that flag to your notification.

Knossos
  • 15,802
  • 10
  • 54
  • 91
  • It seems to stay when I click clear all, but it still disappears sometimes when I click it. Weird – CeeRo Jun 03 '13 at 11:04
  • Right now I'm leaning towards recreating/updating the notification on onResume() so it gets remade after each click, there doesn't seem to be a way to make sure that it really stays otherwise... – CeeRo Jun 03 '13 at 11:09
  • I figured out what's causing it, check my answer – CeeRo Jun 03 '13 at 13:39
  • 2
    What is the difference between this and **setOngoing(true)**? – IgorGanapolsky Aug 29 '17 at 15:44
1

you try

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);
 notificationManager.cancel(pendingIntent);
AnilPatel
  • 2,356
  • 1
  • 24
  • 40
  • I think you misunderstood the question (or i'm misunderstanding your response...) I don't want to cancel the notification (well, I do, when I decide), I want to make the notification stay every timer the user clicks it or clicks "clear all". – CeeRo May 31 '13 at 13:31
1

Okey, I've realized the code I posted is actually good.

What is happening is that when I'm clicking the notification, it's calling onCreate() again, and then after a random interval it's calling onDestroy().

In onDestroy() I had my cancelNotification() method, so if onDestroy() got called after onCreate(), it removed my notification.

Which brings me to a new question: Why is it destroying and recreating my activity after I've followed every answer I could find on here on how to stop that from happening?

You can find that question here How to make notification resume and not recreate activity? if you want to help me solve it...

Community
  • 1
  • 1
CeeRo
  • 1,142
  • 3
  • 12
  • 21
-1

I had this issue too. My Solution is the add extra on the function that creates the intent

        private void addNotification(String headLine, String info, Context context) {
        mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.icon)  //R.drawable.icon
                        .setContentTitle(headLine)
                        .setContentText(info)
                        .setOngoing(true)
                        .setAutoCancel(false);
// Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, MainActivity.class);
        resultIntent.putExtra("FROM_NOTIF", true);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
        //      stackBuilder.addParentStack(MainActivity.class);
        //Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
        //      mBuilder.setf |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        mNotificationManager.notify(mNotification_id, mBuilder.build());
    }

and In MainActivity:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getBooleanExtra("FROM_NOTIF", false)) {
        addNotification(...)
            ... call the function that adds the notification again ..}

So thing is that the notification turns off after the user press on it, but than you can be notify on the activity that started using onNewIntent(), check the intent over there and if you find out that it came from the notification, set it again.

Itai.S.
  • 144
  • 13
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) for providing quality answer. – thewaywewere Jun 18 '17 at 13:05
  • Edited my answer – Itai.S. Jun 18 '17 at 13:41