Android 4: can't dismiss notification by swiping

前端 未结 3 1451
庸人自扰
庸人自扰 2020-12-10 14:19

I have some code that creates some notifications, it\'s really basic.

int icon = R.drawable.notification;
CharSequence tickerText = \"Text\";
long when = Sys         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 14:58

    You can't swipe away your notification, because it is in an "ONGOING"-State.

    First the solution:

    Replace setting flags with following code:

    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    

    Defaults are for the defaults-section, flags for the flags-section.

    And now the reason why it was ongoing?

    As you might already know flags (and defaults) for notifications are set by a bitwise operation. Means each flag has a constant value which is a power of 2. Adding them results in an unique number for a set of flags which makes it real fast to calculate which flags are actually set.

    Notification.DEFAULT_VIBRATE and Notification.FLAG_ONGOING_EVENT have the same contant value of 2.

提交回复
热议问题