我试图在我的Android应用中设置通知,但是NotificationCompat将不接受频道ID作为参数。在我的gradle文件中

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:support-compat:27.1.1'


我还在下面的Starter代码中创建了频道。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
            CHANNEL_1, "Channel 1", NotificationManager.IMPORTANCE_LOW);

        channel.setDescription("This is channel 1");

        NotificationManager manager = getSystemService(NotificationManager.class);
        if (manager != null)
            manager.createNotificationChannel(channel);
    }


最后,我在活动代码中。

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1)


......这就是我找到问题的地方。 Android Studio告诉我签名块不正确。它不接受通道ID作为第二个参数。我已经为NotificationCompat导入了v4和v7,但都无法正常工作。我也尝试过

    NotificationCompat.Builder mBuilder = //continued code


我使用了下面的Android链接,但没有找到解决方案。

https://developer.android.com/training/notify-user/channels

https://developer.android.com/training/notify-user/build-notification#java

我还查看了下面的两个StackOverflow帖子,但对于我的情况仍然没有解决方案。

NotificationCompat.Builder() not accepting Channel Id as argument

NotificationCompat.Builder doesn't accept 2nd argument

我不知道为什么存在这个问题。

最佳答案

好吧,我在本文中找到了答案。

How to make it work NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.0'
            }
        }
    }

关于android - NotificationCompat.Builder不接受 channel ID作为参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53351784/

10-10 23:11