如何访问另一个Android应用程序的通知渠道?

4

我想创建一个函数,调用另一个应用程序的通知频道设置。但我不知道另一个应用程序的频道ID。有办法做到吗?

4个回答

3

你不能访问另一个应用的通知渠道,无论是哪些渠道还是其设置。

唯一能做的就是通过其包名(例如Facebook)打开另一个应用的通知设置概览:

Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
        .putExtra(Settings.EXTRA_APP_PACKAGE, "com.facebook.katana");
startActivity(intent);

1
这可以通过反射来实现。请注意,每次Android发布都会阻止更多的隐藏API,因此这不是长期解决方案。
首先,在AndroidManifest.xml中添加android.permission.STATUS_BAR_SERVICE权限。我的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参考资料:

NotificationManager.java

INotificationManager.aidl

BaseParceledListSlice.java


0

不可以处理其他应用程序的通知渠道,因为每个更改都有一个ID,没有ID系统无法获取该渠道。 据我所知,我们无法获取其他应用程序的通知渠道ID。


0

您可以使用SDK中内置的NotificationListenerService访问其他应用程序的通知渠道。在注册服务并授予通知访问权限(使用Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))后,只需调用以下方法之类的内容即可:

private List<NotificationChannel> getChannels() {
    final var ranking = getCurrentRanking();
    final var channelsList = new ArrayList<NotificationChannel>();

    for (var notification : getActiveNotifications()) {
        final var currentRanking = new Ranking();
        ranking.getRanking(notification.getKey(), currentRanking);
        channelsList.add(currentRanking.getChannel());
    }
    return channelsList;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接