目前我有一个播放音乐的服务。我想在服务开始播放音乐时显示通知。这就是我的工作:
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();
}
问题在于未显示通知待处理意图,内容标题和内容信息。通知如下所示:
正如您所看到的,标题和内容信息无法显示。此外,当我点击通知时,它不会触发我的未决意图。
我在这里做错了什么?任何帮助是极大的赞赏。
马库斯
答案 0 :(得分:3)
根据notification guide list of required fields,您必须通过setSmallIcon()方法设置一个小图标。通常,此图标看起来像您的应用程序图标,但在notification iconography guide的透明背景下完全为白色。