Как обновить текст уведомления для службы переднего плана в Android? - PullRequest
114 голосов
/ 03 апреля 2011

У меня есть настройка службы переднего плана в 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);

Ответы [ 4 ]

186 голосов
/ 22 ноября 2013

Если вы хотите обновить набор уведомлений с помощью startForeground (), просто создайте новое уведомление и затем используйте NotificationManager для его уведомления.

Ключевым моментом является использование того же идентификатора уведомления.

Я не тестировал сценарий многократного вызова 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);
}

Документация сообщает

Чтобы настроить уведомление, чтобы его можно было обновить, введите его с ID уведомления по телефону NotificationManager.notify(). Обновлять это уведомление после того, как вы его выпустили, обновите или создайте NotificationCompat.Builder объект, построить объект Notification из и введите Notification с тем же идентификатором, который вы использовали ранее. Если предыдущее уведомление все еще отображается, система обновляет его из содержимого объекта Notification. Если предыдущий уведомление было отклонено, создано новое уведомление вместо этого.

51 голосов
/ 03 апреля 2011

Я думаю, что повторный вызов startForeground() с тем же уникальным идентификатором и Notification с новой информацией будет работать, хотя я не пробовал этот сценарий.Вы должны использовать NotifcationManager для обновления уведомлений, и ваша служба продолжает оставаться в режиме переднего плана.Посмотрите на ответ ниже.

14 голосов
/ 12 марта 2018

Улучшение ответа 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);
}
4 голосов
/ 06 июня 2013

вот код для этого в вашем сервисе .Создайте новое уведомление, но попросите менеджера уведомлений уведомить тот же идентификатор уведомления, который вы использовали в startForeground.

Notification notify = createNotification();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext()
    .getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

notificationManager.notify(ONGOING_NOTIFICATION, notify);

для полных образцов кодов, вы можете проверить здесь:

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

...