gpt4 book ai didi

Android 显示通知不起作用

转载 作者:行者123 更新时间:2023-12-02 00:06:33 26 4
gpt4 key购买 nike

目前我有一个播放音乐的服务。我想在服务开始播放音乐时显示通知。这就是我所做的:

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

问题是未显示通知待处理 Intent 、内容标题和内容信息。该通知如下所示: screenshot

正如您所看到的,标题和内容信息不会显示。此外,当我单击通知时,它不会触发我的待处理 Intent 。

我在这里做错了什么?任何帮助是极大的赞赏。

马库斯

最佳答案

根据 notification guide list of required fields ,您必须通过setSmallIcon()设置一个小图标方法。一般来说,这个图标看起来像你的应用程序图标,尽管根据anatomy of a notification在透明背景上完全是白色的。 .

关于Android 显示通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com