首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >即使在设置了通道之后,android O中也不会显示通知

即使在设置了通道之后,android O中也不会显示通知
EN

Stack Overflow用户
提问于 2018-06-15 12:01:54
回答 3查看 1.4K关注 0票数 2

我正在尝试显示我的音乐应用程序的通知。它在android版本0以下运行得非常完美,但android 0以上的版本不会显示通知。我已经设置了0以上版本的频道。但是我还是不能解决这个问题。这是我的NotificationService。

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);
        }
    }
}

有人能帮我解决这个问题吗?

我已经在我的活动中开始了这项服务。所有这些在android O版本下都工作得很好。

这是我的build.gradle。

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' }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50868926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档