我正在尝试显示我的音乐应用的通知。它在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' }
}
答案 0 :(得分:0)
请尝试更改订单。 首先,
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);
答案 1 :(得分:0)
我不知道为什么,但android O
和compileSdkVersion "27"
在我的任何项目中都不起作用,并且当我更改它时,它会按预期正常工作,而不做任何更改在其他代码中(需要通知通道)
对build.gradle
使用此值,它会对您有效,我也将其更改为oreo通知。
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"
}
答案 2 :(得分:-1)
这样做就可以了,
通过此方法创建频道,在您的启动活动或首先出现的任何活动
中调用此频道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 = "Your_channel";
String description = "Your_channel_desc";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(YOUR_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);
}
}
这将在您的FCM服务类中创建频道,执行此操作,
private void showBigNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
// NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(mBuilder).bigText(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setWhen(Long.parseLong(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher_small)
.setContentText(message)
.setChannelId(YOUR_CHANNEL_ID)
.setPriority(Notification.PRIORITY_HIGH)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.build();
final int NOTIFICATION_ID_BIG_IMAGE = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_BIG_IMAGE, notification);
}
试试这个,我使用了这段代码,我收到了没有问题的通知。我希望它有所帮助。