如何更新 Android 中前台服务的通知文本?
我在 Android 中有一个前台服务设置。我想更新通知文本。我正在创建如下所示的服务。
如何更新此前台服务中设置的通知文本?更新通知的最佳做法是什么?任何示例代码将不胜感激。
public class NotificationService extends Service {
private static final int ONGOING_NOTIFICATION = 1;
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
this.notification = new Notification(R.drawable.statusbar, getText(R.string.app_name), System.currentTimeMillis());
Intent notificationIntent = new Intent(this, AbList.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
this.notification.setLatestEventInfo(this, getText(R.string.app_name), "Update This Text", pendingIntent);
startForeground(ONGOING_NOTIFICATION, this.notification);
}
我正在我的主要活动中创建服务,如下所示:
// Start Notification Service
Intent serviceIntent = new Intent(this, NotificationService.class);
startService(serviceIntent);
I have a foreground service setup in Android. I would like to update the notification text. I am creating the service as shown below.
How can I update the notification text that is setup within this foreground service? What is the best practise for updating the notification? Any sample code would be appreciated.
public class NotificationService extends Service {
private static final int ONGOING_NOTIFICATION = 1;
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
this.notification = new Notification(R.drawable.statusbar, getText(R.string.app_name), System.currentTimeMillis());
Intent notificationIntent = new Intent(this, AbList.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
this.notification.setLatestEventInfo(this, getText(R.string.app_name), "Update This Text", pendingIntent);
startForeground(ONGOING_NOTIFICATION, this.notification);
}
I am creating the service in my main activity as shown below:
// Start Notification Service
Intent serviceIntent = new Intent(this, NotificationService.class);
startService(serviceIntent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当你想更新startForeground()设置的Notification时,只需构建一个新的notification,然后使用NotificationManager来通知它。
关键点是使用相同的通知ID。
我没有测试重复调用startForeground()来更新Notification的场景,但我认为使用NotificationManager.notify会更好。
更新通知不会将服务从前台状态中删除(这只能通过调用 stopForground 来完成);
示例:
文档指出
When you want to update a Notification set by startForeground(), simply build a new notication and then use NotificationManager to notify it.
The key point is to use the same notification id.
I didn't test the scenario of repeatedly calling startForeground() to update the Notification, but I think that using NotificationManager.notify would be better.
Updating the Notification will NOT remove the Service from the foreground status (this can be done only by calling stopForground );
Example:
The documentation states
我认为使用相同的唯一 ID 和带有新信息的
Notification
再次调用startForeground()
会起作用,尽管我还没有尝试过这种情况。更新:根据评论,您应该使用 NotifcationManager 更新通知,并且您的服务继续保持在前台模式。看看下面的答案。
I would think that calling
startForeground()
again with the same unique ID and aNotification
with the new information would work, though I have not tried this scenario.Update: Based on the comments, you should use NotifcationManager to update the notification and your service continues to stay in the foreground mode. Take a look at the answer below.
改进了 Android 8.0+ 中 Luca Manzo 的答案,更新通知时它将发出声音并显示为“平视”。
为了防止您需要添加
setOnlyAlertOnce(true)
所以代码是:
Improving on Luca Manzo answer in android 8.0+ when updating the notification it will make sound and show as Heads-up.
to prevent that you need to add
setOnlyAlertOnce(true)
so the code is:
这是在您的服务中执行此操作的代码。创建一个新通知,但要求通知管理器通知您在 startForeground 中使用的相同通知 ID。
有关完整的示例代码,您可以在此处查看:
https ://github.com/plateaukao/AutoScreenOnOff/blob/master/src/com/danielkao/autoscreenonoff/SensorMonitorService.java
here's the code to do so in your service. Create a new notification, but ask notification manager to notify the same notification id you used in startForeground.
for full sample codes, you can check here:
https://github.com/plateaukao/AutoScreenOnOff/blob/master/src/com/danielkao/autoscreenonoff/SensorMonitorService.java
似乎现有的答案都没有显示如何处理完整的情况 - 如果是第一次调用,则启动Foreground,但更新后续调用的通知。
您可以使用以下模式来检测正确的情况:
此外,您需要像其他答案中那样构建通知和通道:
It seems none of the existing answers show how to handle the full case - to startForeground if it's the first call but update the notification for subsequent calls.
You can use the following pattern to detect the right case:
Additionally you need to build the notification and channel as in other answers: