Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification disappear after activity finish

I have an activity named A which start from a broadcast receiver. Activity A triggered a notification but it disappear automatically when activity destroy(finish). But I want to keep this notification until the user click or manually clear the notification.

How activity start from broadcast receiver

Intent i = new Intent(context,A.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
i.putExtras(intent.getExtras());
context.startActivity(i);

Notification

Intent notificationIntent = new Intent();
notificationIntent.setClass(context,B.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.notification_small)
        .setContentTitle(status)
        .setTicker(status)
        .setAutoCancel(false)
        .setContentText(message)
        .setDefaults(Notification.DEFAULT_SOUND)
        .setLargeIcon(
                Bitmap.createScaledBitmap(icon, 128, 128, false))
        .setContentIntent(pendingIntent)
        .build();

manifest options

<activity android:name="com.example.activity.A"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>

Note: I also tried singleInstance but no luck.

Edit(Fixed)

I made a silly mistake. I called clearAll() instead of cancel a specific notification in onDestroy() function.

like image 805
shantanu Avatar asked Apr 01 '15 07:04

shantanu


People also ask

Why do some notifications disappear Android?

Do Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

How do I go back to start an activity from a notification?

When you start an activity from a notification, you must preserve the user's expected navigation experience. Tapping Back should take the user back through the app's normal work flow to the Home screen, and opening the Recents screen should show the activity as a separate task.

What is heads up notification?

Beginning with Android 5.0, notifications can briefly appear in a floating window called a heads-up notification. This behavior is normally for important notifications that the user should know about immediately, and it appears only if the device is unlocked.

What is status bar notification in Android?

Status bar (or notification bar) is an interface element at the top of the screen on Android devices that displays the notification icons, minimized notifications, battery information, device time, and other system status details.


2 Answers

I think you are cancelling all the notification in your app when the activity is destroyed.

like image 77
Fahim Avatar answered Oct 19 '22 11:10

Fahim


remove autocancel() totally and try this,

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;


    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);   
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MenuActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon).setContentTitle(" ").
                                            setStyle(new NotificationCompat.BigTextStyle().bigText(bundle.get("").toString())).
                                            setContentText(bundle.get("").toString());
    mBuilder.setContentIntent(contentIntent);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
like image 37
Heshan Sandeepa Avatar answered Oct 19 '22 13:10

Heshan Sandeepa