我想创建一个调用另一个应用程序的通知通道设置的函数。我不知道其他应用的频道 ID。有没有办法做到这一点?
问问题
765 次
3 回答
3
您无法访问另一个应用程序的通知渠道,无论有哪些渠道,也无法访问它们的设置。
你唯一能做的就是打开另一个应用程序的通知设置概览,给定它的包名(例如 Facebook):
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, "com.facebook.katana");
startActivity(intent);
于 2018-06-15T18:19:57.630 回答
0
这可以通过反射来完成。请注意,每个 Android 版本都会阻止更多隐藏的 API,因此这不是一个长期的解决方案。
首先,将android.permission.STATUS_BAR_SERVICE
权限添加到AndroidManifest.xml
. 我的 IDE 警告说这是系统应用权限,但我的设备(运行小米的 MIUI 12.5,Android 11)允许它。
<manifest ...>
<uses-permission android:name="android.permission.STATUS_BAR_SERVICE"
tools:ignore="ProtectedPermissions"/>
...
</manifest>
现在,可以访问任何应用程序的频道:
// Retreive an instance of the INotificationManager service using the
// hidden NotificationManager.getService static method.
val sINM = NotificationManager::class.java.getMethod("getService").invoke(null)
// Find the INotificationManager.getNotificationChannelsForPackage method.
val getNotificationChannelsForPackageMethod =
sINM::class.java.getMethod(
"getNotificationChannelsForPackage",
java.lang.String::class.java,
Integer.TYPE,
java.lang.Boolean.TYPE,
)
// Retreive information about an app.
val applicationInfo = packageManager.getApplicationInfo("com.example", 0)
// Retreive a ParceledListSlice of the app's NotificationChannel objects.
// The boolean is the "includeDeleted" parameter.
val channelsSlice = getNotificationChannelsForPackageMethod.invoke(
sINM,
applicationInfo.packageName,
applicationInfo.uid,
false,
)
// Retreive the channels in the form of an ArrayList<NotificationChannel>.
val channels = channelsSlice::class.java.getMethod("getList")
.invoke(channelsSlice) as ArrayList<NotificationChannel>
AOSP 参考资料:
于 2021-07-07T14:00:16.067 回答
0
不,您不能处理其他应用通知通道,因为每个更改都有 id 并且没有 id 系统无法获取该通道。据我所知,我们无法获取其他应用程序的通知通道 ID。
于 2018-06-15T18:16:33.847 回答