NotificationManager无法显示“notify:id corrupted:sent 1,got 0”警告的通知

时间:2012-11-30 23:46:13

标签: android

过去几天我在我的应用中遇到了NotificationManager的问题,而且我似乎没有接近解决它。

我有一个非常简单的服务,目前没有做任何事情。它只是显示通知:

public class UpdateService extends Service {
private static final String TAG = "UpdateService";
private static int NOTIFICATION_ID = 1;
private UpdateServiceBinder binder = new UpdateServiceBinder();

@Override
public void onCreate() {
        Log.i(TAG, "Service created");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Service started");

    sendNotification(100);

    return Service.START_NOT_STICKY;
}

private void sendNotification(int updatedItems) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Sync")
            .setContentText("Updated " + updatedItems);

    Intent resultIntent = new Intent(getBaseContext(), MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendindIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(resultPendindIntent);

    NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, builder.build());
}

@Override
public IBinder onBind(Intent intent) {
    return binder;
}

@Override
public boolean onUnbind(Intent intent) {
    return true;
}

public class UpdateServiceBinder extends Binder {
    public UpdateService getService() {
        return UpdateService.this;
    }
}

}

不幸的是,当服务启动并且应该显示通知时,没有任何反应。该服务肯定是根据日志消息启动的。同时NotificationManager发出警告:

  

11-30 23:24:34.620:I / UpdateService(28356):已创建服务

     

11-30 23:24:34.800:I / UpdateService(28356):服务已启动

     

11-30 23:24:34.808:W / NotificationManager(28356):notify:id   腐败:发送1,回到0

我尝试使用不同于1的数字,但它没有帮助。我不知道该怎么做。我使用的代码来自here中有关服务的Android文档和here中的通知。当我在单独的干净应用程序中隔离代码时,该应用程序的设置方式类似于我正在处理通知的工具。有没有人之前有这个问题或者有任何想法?

任何帮助表示赞赏。 谢谢!

7 个答案:

答案 0 :(得分:12)

我有这个问题然后我记得我已经从我的“应用信息”中禁用了“显示通知”。我启用了它,通知又回来了。

  1. 转到设置>应用程序(应用程序管理器)。
  2. 点按您要停止的应用。
  3. 点击以取消选中显示通知框。
  4. 仅供参考,它可能因Android设备而异,但它们或多或少都相同。

答案 1 :(得分:4)

从API 25(Nougat)开始,Android设置速率限制以阻止应用程序通过DDOS通知阴影。 AOSP引入此更改的提交可以是seen here

我看到了这些日志,因为我试图在循环中更新进度通知:

Observable.range(0, 100)
  .subscribe(progress -> updateProgressNotification(progress));

在每次发射之间添加200ms的延迟解决了我的问题:

Observable.range(0, 100)
  .zipWith(Observable.interval(200, TimeUnit.MILLISECONDS), (progress, delay) -> progress)
  .subscribe(progress -> updateProgressNotification(progress));

你可以read more about this problem on my blog

答案 2 :(得分:1)

我找到了解决这个问题的方法。我不得不将包名称从com.gttn.sample更改为com.gttn.sample2。不确定为什么我必须这样做,但通知显示正确,警告消失了。

答案 3 :(得分:1)

当你多次安装并重新安装应用程序时

取消选中“设置”中的“显示通知”> ApplicationManager> “你的应用程序”。

再次检查“显示通知”并感到高兴!

答案 4 :(得分:0)

我写了一些代码,可以下载歌曲并更新下载进度,作为通知进度。如果您在代码的第6行中看到,如果消除了该情况,则通知会非常快速地更新,因为while循环从输入流中读取字节,这现在导致对通知的重复更新,这被认为是从noughat开始的问题版本,以保持通知更新的速率限制。希望您对此有所了解。

 int previousPercentage = 0;
 int currentPercentage = 0;
 while ((bytesRead = inputStream.read(bytes)) != -1) {
       totalbytes = totalbytes + bytesRead;
       currentPercentage = (totalbytes * 100) / length_of_file;
       if (previousPercentage != currentPercentage) {// line : 6
        updateProgressNotification(builder, notificationId, currentPercentage);
       }
       previousPercentage = currentPercentage;
       fileOutputStream.write(bytes, 0, bytesRead);
 }

答案 5 :(得分:-1)

我不确定你为什么使用getBaseContext(),但我建议你改用getApplicationContext()。另外,我不确定你为什么要绑定服务。

答案 6 :(得分:-1)

我自己只是调查了这个问题。如果没有日志文件中的其他行(应该在5行之后),可能会出现类似于“E / NotificationService(287)”的消息:按用户请求拒绝来自包的通知。这是ICS(可能是JB)中的一个新设置,可以基于每个应用程序禁用通知。转到设置 - >应用 - >然后点击顶部附近的“显示通知”复选框。出于显而易见的原因,更改包名称将非常有效地绕过此设置。