NotificationCompat.Builder不接受频道ID作为参数

时间:2018-11-17 13:42:26

标签: android notifications

我正在尝试在我的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

我不知道为什么会出现此问题。

1 个答案:

答案 0 :(得分:1)

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

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'
            }
        }
    }