Update text of notification, not entire notification

后端 未结 3 358
不思量自难忘°
不思量自难忘° 2020-11-28 05:43

Prelude

I\'m trying to add a chronometer on the notification. The chronometer is a service. Every second this line is called (continue Thread is a the \"runni

相关标签:
3条回答
  • 2020-11-28 06:15

    Thanks for the idea PieterAelse!

    In my case I had to go with this (build a new notification) for the second and following times:

            builder.setContentText(getConvertedTime(millisUntilFinished));
            Notification notification = builder.getNotification();
            notification.flags = Notification.FLAG_ONGOING_EVENT;            
            nm.notify(R.string.app_name,notification);
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-28 06:15

    You need to mark the notification as "only Arert Once"

    Builder#setOnlyAlertOnce(true)
    
    0 讨论(0)
  • 2020-11-28 06:32

    Be sure to use the same NotificationCompat.Builder builder each time for creating the Notification!

    Although the first time you have to set everything, the second time using the Builder you only have to set the value(s) you want to update. After that it's calling notificationManager.notify(id, builder.build()) just like you did. If you use the same ID then the notification gets updated (important!).

    Example:

    //First time
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentText(context.getString(R.string.notif_text))
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_action_alarm_2)
                .setAutoCancel(false)
                .setOngoing(running)
                .setOnlyAlertOnce(true)
                .setContentIntent(
                        PendingIntent.getActivity(context, 10, 
                                new Intent(context, FrontActivity.class)                                 
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
                        0)
                )
                .addAction(running ? R.drawable.ic_action_pause 
                                   : R.drawable.ic_action_play,
                           running ? context.getString(R.string.pause)
                                   : context.getString(R.string.start),
                           startPendingIntent)
                .addAction(R.drawable.ic_action_stop, context.getString(R.string.stop),
                        stopPendingIntent);
    
    notificationManager.notify(id, builder.build());
    
    //Second time
    builder.setContentTitle(title);
    notificationManager.notify(id, builder.build());
    

    But you can also use the setUsesChronometer method of the NotificationCompat class. This automatically displays a chronometer using the given timestamp (able to set with setWhen).

    0 讨论(0)
提交回复
热议问题