chocolate cake
chocolate cake

Reputation: 2499

Notification is cancelled immediately

I need to create notification in my app. I am building it like this:

protected void onCreate(Bundle savedInstanceState) {
    [...]
    mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo_ico)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

    Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, mBuilder.build());
}

My problem is that my notification is cancelled immediately after is was shown for half a second. How can I fix it?

UPDATE:

Could it have to do with my other notification that comes from a framework? It is possible to have multiple notifications, isn't it?

UPDATE 2:

When I put notificationManager.notify(1, mBuilder.build()); in another method, the notification is shown until the end of that method.

Upvotes: 1

Views: 357

Answers (1)

krystian71115
krystian71115

Reputation: 1937

Try to set flags on mBuilder:

mBuilder.setFlags(Notification.FLAG_NO_CLEAR);

Yes it is possible to have multiple notifications. To show second notification use another id. e.g. notificationManager.notify(2, mBuilder.build()); http://developer.android.com/reference/android/app/Notification.html#FLAG_NO_CLEAR

Upvotes: 1

Related Questions