如何解决通知不在Android O中显示的问题/

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (148)

以下是我的代码:

Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    remoteView = new RemoteViews(getPackageName(), R.layout.notification_view);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        nBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                .setContentTitle("JukeTrilll")
                .setContentIntent(resultPendingIntent)
                .setSmallIcon(R.drawable.logo)
                .setCustomContentView(remoteView)
                .setAutoCancel(false);
    } else {
        nBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("JukeTrilll")
                .setContentIntent(resultPendingIntent)
                .setAutoCancel(false)
                .setContent(remoteView)
                .setSmallIcon(R.drawable.logo);
    }

    notification = nBuilder.build();
    notificationTarget = new NotificationTarget(
            this,
            R.id.img_cover_noti,
            remoteView,
            notification,
            2
    );

    Glide.with(this).asBitmap().load(Const.BASE_URL + url).into(notificationTarget);
    remoteView.setTextViewText(R.id.tv_song_name_noti, songName);
    remoteView.setTextViewText(R.id.tv_artist_name_noti, artist);

    //set the button listeners
    setListeners(remoteView);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Const.ACTION_PREV);
    intentFilter.addAction(Const.ACTION_PAUSE);
    intentFilter.addAction(Const.ACTION_NEXT);
    intentFilter.addAction(Const.ACTION_PLAY);
    registerReceiver(notificationService, intentFilter);

    nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription("desc");
        nBuilder.setChannelId(CHANNEL_ID);
        nManager.createNotificationChannel(channel);
    }


    assert nManager != null;
    nManager.notify(2, nBuilder.build());

myListener,用于处理通知中的操作

        //previous
    Intent volume = new Intent(Const.ACTION_PREV);
    PendingIntent btnPrev = PendingIntent.getBroadcast(this,
            0, volume,
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btn_prev_noti, btnPrev);

    //pause
    Intent stop = new Intent(Const.ACTION_PAUSE);
    PendingIntent btnStop = PendingIntent.getBroadcast(this, 0, stop, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btn_pause_noti, btnStop);

    //play
    Intent play = new Intent(Const.ACTION_PLAY);
    PendingIntent btnPlay = PendingIntent.getBroadcast(this, 0, play, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btn_play_noti, btnPlay);

    //next
    Intent next = new Intent(Const.ACTION_NEXT);
    PendingIntent btnNext = PendingIntent.getBroadcast(this, 0, next, PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btn_next_noti, btnNext);

广播接收器NotificationService.java

  @Override
public void onReceive(Context context, Intent intent) {
    if (intent != null) {
        String action = (String) intent.getAction();

        if (action.equals(Const.ACTION_PREV)) {
            PlayMusicService.prev();
            Song song = PlayMusicService.queueArrayList.get(PlayMusicService.position);
            MyNotificationService.updateNoti(true, song);
            EventBus.getDefault().postSticky(2);
        } else if (action.equals(Const.ACTION_PAUSE)) {
            MyNotificationService.updateNotiPlayPause(false);
            PlayMusicService.pauseAudio();
            EventBus.getDefault().postSticky(1);
        } else if (action.equals(Const.ACTION_NEXT)) {
            PlayMusicService.next();
            Song song = PlayMusicService.queueArrayList.get(PlayMusicService.position);
            MyNotificationService.updateNoti(true, song);
            EventBus.getDefault().postSticky(2);
        } else if (action.equals(Const.ACTION_PLAY)) {
            MyNotificationService.updateNotiPlayPause(true);
            PlayMusicService.unPauseAudio();
            EventBus.getDefault().postSticky(1);
        }
    }
}
buildscript {
repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
      }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'


android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
    applicationId "com.devhousemyanmar.juketrill"
    minSdkVersion 19
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    renderscriptTargetApi 17
    renderscriptSupportModeEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-         
rules.pro'
        }
    }
}
realm {
    syncEnabled = true;
}

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'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'it.sephiroth.android.library.bottomnavigation:bottom-    
    navigation:2.0.1-rc1'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.mikhaellopez:circularimageview:3.0.2'
    implementation 'com.jackandphantom.android:blurimage:1.0.1'
    implementation 'com.facebook.android:facebook-login:[4,5)'
    implementation 'com.github.bumptech.glide:glide:4.6.1'
    implementation 'com.mindorks.android:prdownloader:0.4.0'
    implementation 'com.amitshekhar.android:android-networking:1.0.1'
    implementation 'com.ryanjeffreybrooks:indefinitepagerindicator:1.0.7'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.github.mmin18:realtimeblurview:1.1.0'
    implementation 'org.greenrobot:eventbus:3.1.1'
    implementation 'com.google.firebase:firebase-crash:11.0.4'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3'
}

repositories {
    mavenCentral()
    google()
    maven { url 'https://maven.fabric.io/public' }
}
提问于
用户回答回答于

这样试试:

android {
compileSdkVersion "26"
buildToolsVersion "26.0.2"

defaultConfig {
    applicationId "your.package.name"
    minSdkVersion '15'
    targetSdkVersion '26'
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
用户回答回答于

先这样操作

CharSequence name = getString(R.string.channel_name);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription("desc");
        nBuilder.setChannelId(CHANNEL_ID);
        nManager.createNotificationChannel(channel);

然后

nBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                .setContentTitle("JukeTrilll")
                .setContentIntent(resultPendingIntent)
                .setSmallIcon(R.drawable.logo)
                .setCustomContentView(remoteView)
                .setAutoCancel(false);

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动