android - 在 android 8.0+ 上不显示通知抽屉

标签 android android-notifications

安卓工作室 3.2.1

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.myproject"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 423
        versionName "2.1.423"

def AAVersion = '4.5.2'

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    annotationProcessor "org.androidannotations:ormlite:$AAVersion"

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:animated-vector-drawable:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:customtabs:28.0.0'
    implementation 'com.android.support:exifinterface:28.0.0'
    implementation 'com.android.support:support-media-compat:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'

    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.7'
    implementation 'com.google.android.gms:play-services-gcm:16.0.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.j256.ormlite:ormlite-android:5.1'
    implementation 'commons-codec:commons-codec:1.11'
    implementation 'commons-io:commons-io:2.6'
    implementation 'org.apache.commons:commons-lang3:3.8.1'
    implementation 'org.apache.httpcomponents:httpclient:4.3.6'
    implementation "org.androidannotations:androidannotations-api:$AAVersion"
    implementation "org.androidannotations:ormlite-api:$AAVersion"  

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

此处显示通知抽屉的方法:

  NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = context.getString(R.string.channel_name);
            String description = context.getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            notificationManager.createNotificationChannel(channel);
        }
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (mBuilder == null) {
            mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                    // icon as on notification bar and on notification layout
                    .setSmallIcon(R.drawable.ic_stat_bulb).setContentTitle(context.getString(R.string.app_name))
                    // .setLargeIcon(largeIcon).setTicker(message).setWhen(when)
                    .setSound(soundUri)
                    // notification LED
                    .setLights(0xFF0000FF, 100, 3000)
                    // hide icon on notification bar when clicked
                    .setAutoCancel(true);
        }

它在 android 8.0- 上成功运行。推送通知成功显示。

但在 android 8.0+ 上通知抽屉不显示。 如您所见,我使用了 NOTIFICATION_CHANNEL_ID 但它没有帮助。

最佳答案

您需要创建通知 channel 并在创建通知构建器时使用相同的 channel ID。

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
       CharSequence name = getString(R.string.channel_name);
       String description = getString(R.string.channel_description);
       int importance = NotificationManager.IMPORTANCE_DEFAULT;
       NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
       channel.setDescription(description);
       // Register the channel with the system; you can't change the importance
       // or other notification behaviors after this

      NotificationManager notificationManager = getSystemService(NotificationManager.class);
      notificationManager.createNotificationChannel(channel);
  }
} 

关于android - 在 android 8.0+ 上不显示通知抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53856235/

相关文章:

java - 我的代码有什么问题吗?我似乎遇到了局部/全局变量问题。请帮我

android - 使用Android SDK生成类图Eclipse

android - 收到通知时出现异常

android - gridlayout 中的 Nativescript 标签没有得到链式 react

java - 如何为 ImageButton 添加阴影?

java - 大图标位图在通知中显示为白色方 block ?

未触发 Android 通知操作 (PendingIntent)

android - 如何在不使用 TYPE_KEYGUARD 的情况下禁用主页按钮?

Android TextView以编程方式在LinearLayout中右对齐

android - RemoteServiceException 在 MIUI 11 上使我的应用程序崩溃