如何在我的应用中隐藏持久通知?
目前我的通知也在锁屏中显示,但我不想。我只想在向下滑动通知菜单时显示通知。
我知道这是可能的,因为我有一个名为@Override
public int onStartCommand(Intent intent, int _flags, int _startId) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
noti = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Subtitle")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent)
.build();
startForeground(1234, noti);
mLocalBroadcastManager.sendBroadcast(new Intent(SERVICE_STARTED));
return Service.START_STICKY;
}
的应用程序,它正是这样做的。 (TouchWiz Android 5.0.1)
这是我的通知代码(它在服务中):
<div class="follow">
{% if followsId == null %}
<div id="followUser" data-userId="{{ profileUserData.id }}" data-currentUserId="{{ loggedUserData.id }}" data-action="follow">
Follow
</div>
{% else %}
<div id="unFollowUser" data-followsId="{{ followsId }}" data-action="unFollow">
Unfollow
</div>
{% endif %}
</div>
答案 0 :(得分:3)
使用setVisibility(NotificationCompat.VISIBILITY_SECRET)
&amp; setPriority(Notification.PRIORITY_MIN)
:
mNotification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Subtitle")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent)
.setOngoing(true)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setPriority(Notification.PRIORITY_MIN)
.build();
通知可见性:请勿在安全锁屏上显示此通知的任何部分。
最低优先级;除特殊情况外,这些项目可能不会向用户显示,例如详细的通知日志。
如果您仍想在状态栏中显示图标,则没有选项,但您可以通过订阅USER_PRESENT
&amp;来构建简单的解决方法。 SCREEN_OFF
事件:
SCREEN_OFF
次活动时取消通知USER_PRESENT
注册BroadcastReceiver
&amp;通知默认通知:
mNotificationmanager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.USER_PRESENT");
filter.addAction("android.intent.action.SCREEN_OFF");
registerReceiver(mBroadcastReceiver, filter);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
mNotification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Subtitle")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(contentIntent)
.setOngoing(true)
.build();
mNotificationmanager.notify(NOTIF_ID, mNotification);
在BroadcastReceiver
取消SCREEN_OFF
时,通知USER_PRESENT
:
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("test", intent.getAction());
if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
mNotificationmanager.cancel(NOTIF_ID);
} else if (intent.getAction().equals("android.intent.action.USER_PRESENT")) {
mNotificationmanager.notify(NOTIF_ID, mNotification);
}
}
};