【问题标题】:Hide persistent notification隐藏持久通知
【发布时间】:2016-08-01 08:00:49
【问题描述】:

如何在我的应用中隐藏持久通知?

目前我的通知也显示在锁屏中,但我不想这样做。我只希望在我向下滑动通知菜单时显示通知。

我知道这是可能的,因为我有一个名为 WiFi ADB 的应用程序,它就是这样做的。 (TouchWiz Android 5.0.1)

这是我的通知代码(在服务中):

@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;
}

【问题讨论】:

标签: android notifications


【解决方案1】:

使用setVisibility(NotificationCompat.VISIBILITY_SECRET) & 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();

对于VISIBILITY_SECRET

通知可见性:不要在安全的锁屏上显示此通知的任何部分。

对于PRIORITY_MIN

最低优先级;这些项目可能不会显示给用户,除非在特殊情况下,例如详细的通知日志。


如果您仍想在状态栏中显示图标,则没有此选项,但您可以通过订阅 USER_PRESENTSCREEN_OFF 事件来构建一个简单的解决方法:

  • 收到SCREEN_OFF 事件时取消通知
  • 收到USER_PRESENT时重新通知

注册BroadcastReceiver并通知默认通知:

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);
        }

    }
};

【讨论】:

  • 谢谢!这是工作!但是有没有办法在状态栏中仍然显示图标?
  • 如果您想在状态栏中显示通知图标、在通知菜单中显示通知记录而不在锁屏中显示通知,我已经更新了另一种解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多