简体   繁体   中英

Keep notification icon in the status bar when clicked

I want to display a simple notification when my app is running and doing certain things. I want my app to open when the user clicks my notification. So far, the behaviour works like I want it to. However, when the user clicks the notification, while the app is opened and the notification is still there, the notification icon in the notification bar (left to the time, before swiping down) disappears. Edit: I just observed that the notification icon reappears in the notification bar once any other notification (WhatsApp) arrives. How can I prevent this behaviour?

The notification looks as following:

   NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent intent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    builder.setContentTitle("Running")
            .setContentText("App is running.")
            .setSmallIcon(R.drawable.ic_lock_outline_black_24dp)
            .setOngoing(true)
            .setContentIntent(intent);
    notifyManager.notify(appRunningNotificationID, builder.build());

Look code below:

           TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Running")
                .setContentText("App is running")
                .setTicker("My app")
                .setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = mBuilder.build();
        long[] vibrate = { 500,1000 };
        notification.vibrate = vibrate;
        mNotificationManager.notify(appRunningNotificationID, notification);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM