如何在Android中更新前台服务的通知文本?

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对象的内容对其进行更新.如果先前的通知已被取消,则会创建新通知.

  • 这是正确的答案!上面的答案非常错误和误导.您无需重新启动服务,只需更新愚蠢的通知即可. (28认同)
  • 使用`NotificationManager`更新用`startForeground`显示的通知的问题是调用`stopForeground`将不再删除通知.用另一个调用`startForeground`来更新它可以避免这个问题. (9认同)
  • @Radu虽然我同意这是最佳答案(它避免了Commonsware的回答略长的代码路径)但你错误的是Commonsware的回答是什么 - start/stopForegound不启动/停止服务,它们只会影响它的前景性. (7认同)

Com*_*are 55

我认为startForeground()再次使用相同的唯一ID和Notification新信息进行调用会有效,尽管我没有尝试过这种情况.

更新:根据注释,您应该使用NotifcationManager更新通知,您的服务将继续保持在前台模式.看看下面的答案.

  • 进一步澄清:你不能`取消()一个由`startForeground()`设置的通知.你必须删除服务本身的前景状态(使用`stopForeground()`如果你想再次显示自动收报机文字.我失去了几个小时,因为这些答案让我相信它实际上是可能的. (9认同)
  • 我忽略了这个答案,因为它显然是错误的:https://developer.android.com/training/notify-user/managing.html请@CommonsWare考虑删除这个答案,因为你的高信誉评分使这个答案成为"圣洁的真相"对于休闲浏览器.谢谢. (4认同)
  • 不适用于我(尽管我记得在上一个项目中使用了相同的方法)。使用`NotificationManager`符合我的预期。 (2认同)

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)

  • 缺少关键字,应该有“newNotificationChannel” (2认同)

Nic*_*oso 6

似乎现有的答案都没有显示如何处理完整的情况 - 如果是第一次调用,则启动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)

有关完整的示例代码,您可以在这里检查:

https://github.com/plateaukao/AutoScreenOnOff/blob/master/src/com/danielkao/autoscreenonoff/SensorMonitorService.java

  • 如果我错了,请纠正我,但是人们可以否决这个答案,请对它有什么问题进行更多描述。问题不是问如何启动前台服务,而是问如何更新前台服务的通知。这实际上与人们同意的Luca相同,并且可以维持前台地位。 (4认同)