如何设置Android通知频道?

时间:2018-11-21 00:20:43

标签: android service notifications channel

我查看了示例代码Android "Create a Notification" example,并创建了自己的版本:

import android.support.v4.app.NotificationCompat;

...

    Notification notification = new NotificationCompat.Builder(this, (String) CHANNEL_ID)
            .setContentTitle("Eumag")
            .setContentText("foreground service running")
            .setSmallIcon(R.drawable.ic_android)
            .setContentIntent(pendingIntent)
            .build();

但是,当我尝试构建项目时,会产生此错误:

  

错误:类Builder中的构造方法Builder无法应用于给定   类型;必需:找到上下文:ForegroundService,字符串原因:   实际和正式论证清单的长度不同

我做了一些研究,发现这意味着没有构造函数接受给定的参数。因此,我调查了NotificationCompat.Builder documents。根据文档,以下两种方法都应存在,但是在源代码中,只有其中一种可以使用:

NotificationCompat.Builder(上下文上下文) [存在]
NotificationCompat.Builder(Context context,String channelId) [不存在]

我猜很好,所以我去尝试另一个替代方法,即“ setChannelId(String channelId)”,它也不在其中!我很困惑。

build.gradle

android {
    compileSdkVersion 28

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.eumag"
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation project(':spotify-app-remote')
    implementation "com.google.code.gson:gson:2.6.1"
    implementation 'com.fasterxml.jackson.core:jackson-core:2.7.3'
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
    implementation 'com.github.spotify:android-auth:9425c6a140'
    implementation 'com.github.nkzawa:socket.io-client:0.3.0'
}

0 个答案:

没有答案