Luk*_*uke 123 service notifications android foreground
我在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);
}
Run Code Online (Sandbox Code Playgroud)
我正在我的主要活动中创建服务,如下所示:
// Start Notification Service
Intent serviceIntent = new Intent(this, NotificationService.class);
startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)
Luc*_*nzo 201
如果要更新startForeground()设置的Notification,只需构建一个新的通知,然后使用NotificationManager通知它.
关键是使用相同的通知ID.
我没有测试反复调用startForeground()来更新Notification的场景,但我认为使用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);
}
Run Code Online (Sandbox Code Playgroud)
该文档的状态
要设置通知以便更新,请通过调用向通知ID发出通知
NotificationManager.notify().要在发出通知后更新此通知,请更新或创建NotificationCompat.Builder对象,Notification从中构建对象,并Notification使用您之前使用的相同ID 发出.如果先前的通知仍然可见,则系统会根据Notification对象的内容对其进行更新.如果先前的通知已被取消,则会创建新通知.
Com*_*are 55
我认为startForeground()再次使用相同的唯一ID和Notification新信息进行调用会有效,尽管我没有尝试过这种情况.
更新:根据注释,您应该使用NotifcationManager更新通知,您的服务将继续保持在前台模式.看看下面的答案.
hum*_*zed 16
在更新通知时改进Luca Manzo在Android 8.0+中的答案它将发出声音并显示为Heads-up.
防止你需要添加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);
}
Run Code Online (Sandbox Code Playgroud)
似乎现有的答案都没有显示如何处理完整的情况 - 如果是第一次调用,则启动Foreground,但更新后续调用的通知。
您可以使用以下模式来检测正确的情况:
private void notify(@NonNull String action) {
boolean isForegroundNotificationVisible = false;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
if (notification.getId() == FOREGROUND_NOTE_ID) {
isForegroundNotificationVisible = true;
break;
}
}
Log.v(getClass().getSimpleName(), "Is foreground visible: " + isForegroundNotificationVisible);
if (isForegroundNotificationVisible){
notificationManager.notify(FOREGROUND_NOTE_ID, buildForegroundNotification(action));
} else {
startForeground(FOREGROUND_NOTE_ID, buildForegroundNotification(action));
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您需要像其他答案一样构建通知和渠道:
private Notification buildForegroundNotification(@NonNull String action) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
//Do any customization you want here
String title;
if (ACTION_STOP.equals(action)) {
title = getString(R.string.fg_notitifcation_title_stopping);
} else {
title = getString(R.string.fg_notitifcation_title_starting);
}
//then build the notification
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setOngoing(true)
.build();
}
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel(){
NotificationChannel chan = new NotificationChannel(CHANNEL_ID, getString(R.string.fg_notification_channel), NotificationManager.IMPORTANCE_DEFAULT);
chan.setLightColor(Color.RED);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这是在您的服务中执行此操作的代码。创建一个新通知,但要求通知管理器通知您在startForeground中使用的相同通知ID。
Notification notify = createNotification();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(ONGOING_NOTIFICATION, notify);
Run Code Online (Sandbox Code Playgroud)
有关完整的示例代码,您可以在这里检查:
| 归档时间: |
|
| 查看次数: |
51881 次 |
| 最近记录: |