【问题标题】:Android API7 clearing ongoing notificationAndroid API7 清除正在进行的通知
【发布时间】:2011-07-19 22:01:34
【问题描述】:

从我的服务中,我发送(“持续”ID = 0)持续通知,似乎没有办法从代码中清除它。如果我将其他通知发送到相同的 ID (0)、图标和意图 将更新:/

我不知道这是否是 API 7 中的错误,但这对我来说毫无意义。

当我使用具有不同 ID(例如 1)的 Notification.FLAG_ONLY_ALERT_ONCE 发送通知时,它被归类为简单通知,我可以轻松地从扩展菜单或代码中清除它。但是只需向之前的 ID (0) 发送相同的通知,它就会被归类为正在进行,我无论如何都无法取消它!

我还尝试保存对“正在进行的”通知的引用,然后使用其他 PendingIntent 和/或标志重新通知它,但它没有效果!

我当前的服务代码

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.d(TAG, "start notification");
    startStatusBarNotification();

    //do other stuff
    // We want this service to continue running until it is explicitly stopped, so return sticky.
    return START_STICKY;
}

private void startStatusBarNotification()
{
    Log.d(TAG, "startStatusBarNotification.");

    final int icon = R.drawable.stbar_icon_fail;
    final long when = System.currentTimeMillis();
    final Notification notification = new Notification(icon, getString(R.string.notify_title), when);

    final Intent notificationIntent = new Intent(this, MyActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    notification.setLatestEventInfo(this, getString(R.string.notify_title), expandText, contentIntent);
    notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE;//| Notification.FLAG_NO_CLEAR

    Log.d(TAG, "notify send.");
    final NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(SERVICE_ONGOING_NOTIFICATION_ID, notification);//ID = 0
}

@Override
public void onDestroy()
{
    Log.d(TAG, "clear status bar notification.");
    cancelNotification();

    super.onDestroy();
}

private void cancelNotification()
{
    final int icon = R.drawable.stbar_icon_exit;
    final long when = System.currentTimeMillis();
    final Notification notification = new Notification(icon, getString(R.string.notify_title), when);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, null, PendingIntent.FLAG_CANCEL_CURRENT);
    notification.setLatestEventInfo(this, getString(R.string.notify_stopping_title), getString(R.string.notify_stopping_text), contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;

    final NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel(SERVICE_ONGOING_NOTIFICATION_ID);  //NOTHING HAPPENS
    mNotificationManager.notify(SERVICE_ONGOING_NOTIFICATION_ID, notification);   //ID = 0 but if this was changed to 1 cancel notification can be cleared :/
}

我可以看到“退出”图标,因此调用了 onDestroy 和 cancelNotification,我不知道我做错了什么

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    您似乎无法成功取消 ID 为 0 的正在进行的通知:您可能必须使用非零通知 ID。

    核心通知管理器服务代码是here,它似乎对通知 ID 的选择没有具体限制,但使用 ONGOING 通知的其他所有人(例如,电话应用程序)都使用非零 ID .

    【讨论】:

    • 好吧,我试过了,不幸的是,情况并非如此,实际上我认为这与线程有关,因为状态栏运行在与我的服务不同的线程/进程中。
    • 所以当您使用非零 ID 进行 ONGOING 通知时,您仍然无法取消?
    • 我已将其更改为 20;] 我真的不知道我做错了什么,有人可以确认您可以取消正在进行的服务通知吗?
    • 您意识到在您的cancelNotification 函数结束时您正在使用mNotificationManager.notify(SERVICE_ONGOING_NOTIFICATION_ID, notification); //ID = 0 but if this was changed to 1 cancel notification can be cleared :/ 重新创建通知?如果您删除最后一行,它现在可以工作吗?
    • 不是什么都没有发生,“旧”通知仍然存在,奇怪的是,当我在从 cancelNotification 返回之前将这个取消循环 100 次时,最终我收到了 2 个通知 - 一个“旧”用于运行服务和一个来自 mNotificationManager.notify(SERVICE_ONGOING_NOTIFICATION_ID, notification);这是正常的通知
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多