java - 即使在设置频道后,通知也不会显示在 android O 中
问题描述
我正在尝试为我的音乐应用显示通知。它在低于 android 版本 0 时工作得非常完美,但对于高于 android 0 的版本没有显示通知。我已经为高于 0 版本设置了频道。但我仍然无法解决问题。这是我的通知服务。
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' }
}
解决方案
请尝试更改顺序。第一的,
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);
推荐阅读
- python - 如何在 Python 中使用 .iloc[] 进行一些计算?
- python - 带有keras的回归模型
- css - 如何在不引用任何标签的情况下引用 Angular 组件的完整模板设置 CSS?
- rust - 如何使用 PyO3 在嵌入在 Rust 代码中的 Python 代码中执行地板除法?
- junit5 - 在运行时访问 JUnit 版本
- java - Spring-Cloud-Stream-Kafka-Binder 功能样式忽略自定义 De/Serializer 和/或 useNativeEncoding?
- python - 在 requirements.txt 中打包所有依赖项
- typescript - 解决路径 tsconfig 与 nestjs 有问题
- python - 在 Google Cloud Composer Airflow DAG python 运算符中使用 GCP Connection 对象
- kubernetes - Windows 的持久卷在 kubernetes 上不起作用