В настоящее время у меня есть служба, которая играет музыку. Я хочу показать уведомление, когда служба начнет воспроизводить музыку. Это то, что я делаю:Android показать уведомление не работает
public void setUpNotification() {
/*
* Set up the notification for the started service
*/
Notification.Builder notifyBuilder = new Notification.Builder(this);
notifyBuilder.setTicker("ticker");
notifyBuilder.setOngoing(true);
notifyBuilder.setContentTitle("title");
notifyBuilder.setContentInfo("content info");
// set up an Intent if the user clicks the notification
int NOTIFICATION_ID = 1;
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, notifyIntent,
NOTIFICATION_ID);
notifyBuilder.setContentIntent(pi);
Notification notification = notifyBuilder.build();
// start ongoing
startForeground(NOTIFICATION_ID, notification);
}
метод вызывается в службе playMusic(), как это:
public void playMusic(String songPath) {
if(mPlayer == null || !mPlayer.isPlaying()){
try {
mPlayer = MediaPlayer.create(getBaseContext(), Uri.parse(songPath));
mPlayer.start();
this.songPath = songPath;
} catch (IllegalStateException e) {
e.printStackTrace();
}
}else{
mPlayer.stop();
mPlayer.release();
mPlayer = MediaPlayer
.create(getBaseContext(), Uri.parse(songPath));
mPlayer.start();
this.songPath = songPath;
}
setUpNotification();
}
Проблема заключается в том, что уведомления в ожидании намерения, название контента и информации контента не получают отображается. Уведомление выглядит так:
И как вы можете видеть, информация о названии и содержании не отображается. Кроме того, когда я нажимаю уведомление, оно не отменяет мои ожидающие намерения.
Что я здесь делаю неправильно? Любая помощь приветствуется.
Маркус
Спасибо, связка, это решило это. Я понятия не имел, что значок нужен. Оценил! – Marcus