又发现了一个原生Android Bug

最近在做推送,有一个接口可以给别的应用代发通知,可以不用拉起进程,这样做应该是比较省电,不用唤醒进程。

NotificationManager.notifyAsPackage()

但是有一个问题,创建一个振动和声音开启的NotificationChannel,在这个Channel上发送通知振动会失效,并且系统会报错误日志。单独振动和单独声音的Channel是正常的,看了下NotificationManagerService的代码,发现是在18年的时候,这个特性刚加不久,就被一个三星的哥们改出问题了,之后也没有去修复,很是烦恼…. 正常的应用使用通知是不会有问题的,就是代发通知会出现。

这个接口日常用的不多,基本没有人使用,在AOSP里面搜索都没有人使用,决定修改源码,再给Google的committer发个邮件看看,希望能够有回应吧。下面是邮件内容:


I found that the Android notification delegate has a bug. The vibration does not take effect when the vibration and sound exist at the same time:

NotificationManager.notifyAsPackage()

System will log a exception :

2020-11-27 16:22:27.341 1307-10892/system_process E/AppOps: checkOperation
  java.lang.SecurityException: Specified package com.xxxx.xxxx.push under uid 10225 but it is really 1000
    at com.android.server.appop.AppOpsService.verifyAndGetBypass(AppOpsService.java:4007)
    at com.android.server.appop.AppOpsService.checkOperationUnchecked(AppOpsService.java:2979)
    at com.android.server.appop.AppOpsService.checkOperationImpl(AppOpsService.java:2962)
    at com.android.server.appop.AppOpsService.checkOperationInternal(AppOpsService.java:2943)
    at com.android.server.appop.AppOpsService.checkOperation(AppOpsService.java:2932)
    at com.android.server.appop.AppOpsService.checkAudioOperationImpl(AppOpsService.java:3026)
    at com.android.server.appop.AppOpsService.checkAudioOperation(AppOpsService.java:3014)
    at android.app.AppOpsManager.checkAudioOpNoThrow(AppOpsManager.java:7788)
    at com.android.server.VibratorService.getAppOpMode(VibratorService.java:1094)
    at com.android.server.VibratorService.shouldVibrate(VibratorService.java:1126)
    at com.android.server.VibratorService.startVibrationLocked(VibratorService.java:919)
    at com.android.server.VibratorService.vibrate(VibratorService.java:814)
    at android.os.SystemVibrator.vibrate(SystemVibrator.java:235)
    at com.android.server.notification.NotificationManagerService.lambda$playVibration$5$NotificationManagerService(NotificationManagerService.java:7143)
    at com.android.server.notification.-$$Lambda$NotificationManagerService$BDavS_Sg4m_dKO3ZgtuCsFeqqms.run(Unknown Source:6)
    at java.lang.Thread.run(Thread.java:923)

This is the first commit, add notification delegate post feature:

feature commit

Then this submitted commit:

bug commit

change

  mVibrator.vibrate(record.sbn.getUid(), record.sbn.**getPackageName**(),    effect, "Notification (delayed)", record.getAudioAttributes());

to

  mVibrator.vibrate(record.sbn.getUid(), record.sbn.**getOpPkg**(),   effect, "Notification (delayed)", record.getAudioAttributes());

Cause AppOpsManager check that the package does not match the uid

Please check the bug, thanks!


提了Patch不给我合,算了,遇到这个问题的小伙伴自己修改下….

https://android-review.googlesource.com/c/platform/frameworks/base/+/1514786