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.


我认为当活动被销毁时,您将取消应用中的所有通知。


完全删除autocancel()并试试这个,

    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());
链接地址: http://www.djcxy.com/p/84382.html

上一篇: Spring HandlerInterceptor:如何访问类注解?

下一篇: 活动结束后,通知消失