我有一个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);
发布于 2011-04-03 11:52:49
我认为使用相同的惟一ID和使用新信息的Notification
再次调用startForeground()
是可行的,尽管我还没有尝试过这种情况。
更新:根据评论,您应该使用NotifcationManager更新通知,您的服务将继续停留在前台模式。看看下面的答案。
发布于 2013-11-22 10:31:11
当您想要通过startForeground()更新通知集时,只需构建一个新的通知,然后使用NotificationManager来通知它。
关键点是使用相同的通知id。
我没有测试重复调用startForeground()来更新通知的场景,但我认为使用NotificationManager.notify会更好。
更新通知不会将服务从前台状态移除(这只能通过调用stopForground来完成);
示例:
private static final int NOTIF_ID=1;
@Override
public void onCreate (){
this.startForeground();
}
private void startForeground() {
startForeground(NOTIF_ID, getMyActivityNotification(""));
}
private Notification getMyActivityNotification(String text){
// The PendingIntent to launch our activity if the user selects
// this notification
CharSequence title = getText(R.string.title_activity);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, new Intent(this, MyActivity.class), 0);
return new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher_b3)
.setContentIntent(contentIntent).getNotification();
}
/**
* This is the method that can be called to update the Notification
*/
private void updateNotification() {
String text = "Some text that will update the notification";
Notification notification = getMyActivityNotification(text);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIF_ID, notification);
}
要设置通知以便可以更新,请通过调用
NotificationManager.notify()
使用通知ID发出通知。要在发出通知后更新该通知,请更新或创建一个NotificationCompat.Builder
对象,从该对象构建一个Notification
对象,并使用您之前使用的相同ID发出Notification
。如果先前的通知仍然可见,系统将根据Notification
对象的内容对其进行更新。如果先前的通知已被驳回,则会创建一个新的通知。
发布于 2018-03-12 14:48:35
改进了安卓8.0+中的Luca Manzo answer,当更新通知时,它将发出声音并显示为平视。
为了防止出现这种情况,您需要添加setOnlyAlertOnce(true)
所以代码是:
private static final int NOTIF_ID=1;
@Override
public void onCreate(){
this.startForeground();
}
private void startForeground(){
startForeground(NOTIF_ID,getMyActivityNotification(""));
}
private Notification getMyActivityNotification(String text){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(
NotificationChannel("timer_notification","Timer Notification",NotificationManager.IMPORTANCE_HIGH))
}
// The PendingIntent to launch our activity if the user selects
// this notification
PendingIntent contentIntent=PendingIntent.getActivity(this,
0,new Intent(this,MyActivity.class),0);
return new NotificationCompat.Builder(this,"my_channel_01")
.setContentTitle("some title")
.setContentText(text)
.setOnlyAlertOnce(true) // so when data is updated don't make sound and alert in android 8.0+
.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_b3)
.setContentIntent(contentIntent)
.build();
}
/**
* This is the method that can be called to update the Notification
*/
private void updateNotification(){
String text="Some text that will update the notification";
Notification notification=getMyActivityNotification(text);
NotificationManager mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIF_ID,notification);
}
https://stackoverflow.com/questions/5528288
复制相似问题