and*_*per 12 notifications android foreground-service heads-up-notifications
我发现了一个应用程序以某种方式隐藏单挑通知,包括甚至正在进行的通知(由前台服务使用),称为NCleaner.
我想知道这样的事情是如何起作用的.
没有太多关于如何控制其他应用程序通知的信息.只有当前的应用程序,由于某种原因,我没有取消正在进行的通知,但我已成功取消正常的通知.
我发现此示例显示了如何监控通知.在搜索有关NotificationListenerService的文档后,我还发现了如何取消其他应用程序的特定通知.
但是,它不适用于正在进行的通知.
为了测试正常通知,我只是给自己发了一封电子邮件,或者通过我的电脑在PushBullet app上写了一些东西.
为了测试正在进行的通知,我已经安装并运行了我的业余时间应用程序(此处),该应用程序在应用程序开头显示正在进行的通知,从Android O开始.事实上,它甚至可以隐藏操作系统的通知USB连接和调试(只需通过USB将设备连接到PC)......
以下是我根据示例制作的代码,取消了它获得的所有通知:
NotificationListenerExampleService.kt
class NotificationListenerExampleService : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification) {
Log.d("AppLog", "sbn:" + sbn.toString())
cancelNotification(sbn.key)
}
override fun onListenerConnected() {
super.onListenerConnected()
Log.d("AppLog", "onListenerConnected")
}
override fun onNotificationRemoved(sbn: StatusBarNotification) {}
}
Run Code Online (Sandbox Code Playgroud)
表现:
<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".NotificationListenerExampleService" android:label="@string/service_label" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
</application>
Run Code Online (Sandbox Code Playgroud)
MainActivity.kt
class MainActivity : AppCompatActivity() {
private val isNotificationServiceEnabled: Boolean
get() {
val pkgName = packageName
val flat = Settings.Secure.getString(contentResolver, "enabled_notification_listeners"")
if (!TextUtils.isEmpty(flat)) {
val names = flat.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (i in names.indices) {
val cn = ComponentName.unflattenFromString(names[i])
if (cn != null) {
if (TextUtils.equals(pkgName, cn.packageName)) {
return true
}
}
}
}
return false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!isNotificationServiceEnabled) {
startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用此代码来构建通知。它有标题、文本、图标、contentAction作为广播消息。我希望这足以回答你的问题)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
notManager.createNotificationChannel(notChannel)
}
val builder = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
val contentIntent = Intent(NOT_ACTION)
contentIntent.putExtra(EXTRA_NOT_ID, notificationId)
val contentAction = PendingIntent.getBroadcast(this@MainActivity, NOT_REQ_CODE, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentTitle("Notification $notificationId")
.setContentText("Awesome text for $notificationId notification")
.setContentIntent(contentAction)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setDefaults(NotificationCompat.DEFAULT_ALL)
NotificationManagerCompat
.from(this@MainActivity)
.notify(notificationId++, builder.build())
Run Code Online (Sandbox Code Playgroud)
是的你可以。您可以Notification从获取StatusBarNotification,然后获取PendingIntents 来表示操作并使用它们。
比如我想触发一个通知的contentAction
override fun onNotificationPosted(sbn: StatusBarNotification) {
super.onNotificationPosted(sbn)
Log.d(TAG, "Gotcha notification from ${sbn.packageName}")
if (sbn.packageName == TARGET_PACKAGE) {
val contentIntent = sbn.notification.contentIntent
contentIntent.send()
}
}
Run Code Online (Sandbox Code Playgroud)
此代码将触发广播消息的发送。但它会忽略.setAutoCancel(true)通知,所以你需要像这样手动处理它
if (sbn.packageName == TARGET_PACKAGE) {
val contentIntent = sbn.notification.contentIntent
contentIntent.send()
if (sbn.notification.flags and Notification.FLAG_AUTO_CANCEL != 0) {
cancelNotification(sbn.key)
}
}
Run Code Online (Sandbox Code Playgroud)
我想到的第一个方法是NotificationListenerService.snoozeNotification像这样使用
if (sbn.packageName == TARGET_PACKAGE) {
snoozeNotification(sbn.key, Long.MAX_VALUE - System.currentTimeMillis())
}
Run Code Online (Sandbox Code Playgroud)
我 99% 确定 NCLeaner 使用此代码。因为它不支持 pre-O 设备的此功能。
笔记。您不能只使用Long.MAX_VALUE,通知会暂停几秒钟,然后再次出现。在 Pixel 2 8.1 上测试。
你不能。只有通知所有者应用程序可以修改它。
我还尝试通过NotificationManager和的隐藏方法和字段来克服反射的限制NotificationListenerService。所有尝试的结果是:
引起原因:java.lang.SecurityException:调用 uid 10210 给出了由 uid 10211 拥有的包 com.ns.notificationsender
所以你的应用程序需要是系统一号,或者 root 可能有助于成功。但我没有这样的设备。
| 归档时间: |
|
| 查看次数: |
583 次 |
| 最近记录: |