Android 8.0 Oreo上不会出现NOtification

时间:2017-11-07 03:53:12

标签: java android notifications android-notifications android-8.0-oreo

我读过有关通知的其他Stackoverflow文章没有出现在Android 8.0 Oreo上,但代码仍无法在Android Android api 26上运行。

代码如下:

    public void notification()
{

    //Create Notification Channel
    String channelId = "";
    String name = "volume";
    int id = 0;
    int importance = NotificationManager.IMPORTANCE_HIGH;

    NotificationChannel mChannel = new NotificationChannel(channelId, name, 
importance);
    mChannel.setShowBadge(true);

    //Create Notification Manager
    Intent resultIntent = new Intent(this, MainActivity.class);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT
            );

    NotificationCompat.Builder mBuilder = new 
NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setContentIntent(resultPendingIntent)
            ;

    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyMgr.createNotificationChannel(mChannel);

    // Builds the notification and issues it.
    mNotifyMgr.notify(id, mBuilder.build());

}

更新1:

我已经更改了String channelId =“name”,但它仍然不起作用。我甚至在下面尝试了另一组代码,但仍然不起作用:

        NotificationManager notificationManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new 
NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", 
NotificationManager.IMPORTANCE_DEFAULT);

        // Configure the notification channel.
        notificationChannel.setDescription("Channel description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 
1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    NotificationCompat.Builder builder = new 
NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setVibrate(new long[]{0, 100, 100, 100, 100, 100})

.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Content Title")
            .setContentText("Content Text");

    notificationManager.notify(NOTIFICATION_ID, builder.build());

更新2:

我的build.gradle(项目:测试)如下:

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.

buildscript {

repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    google()
    jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

我的build.gradle(模块:app)如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.a0064274652969.test"
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

2 个答案:

答案 0 :(得分:1)

所以我复制并粘贴了原始代码,它运行正常(在模拟器中),这意味着channelID的空字符串很好,因此通知ID为零。

您的错误是在其他地方,可能在您的设备Android设置中)。 试着发布你的build.gradle?

  

如果您定位Android O并发布通知而未指定   有效的通知渠道,通知无法发布和   系统记录错误。

对于你的新代码,你不再设置contentIntent了......另外,你需要设置一个“抬头通知”来将imporantce设置为HIGH或MAX。

答案 1 :(得分:0)

我发现了同样的问题,对我来说有用的是设置构建器优先级以及通知优先级。因此,添加此代码应该适合您。

    builder.setPriority(Notification.PRIORITY_HIGH);