更新通知文本,而不是整个通知 [英] Update text of notification, not entire notification

查看:35
本文介绍了更新通知文本,而不是整个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前奏

我正在尝试在通知中添加一个计时器.计时器是一项服务.每一秒都会调用这一行(继续 Thread 是正在运行的"布尔值,timeString 是显示时间的详细字符串):

NotificationChrono.updateNotification(getApplicationContext(), continueThread,NOTIF_ID, timeString, "Chronometer", notificationManager);

这是 NotificationChrono 类:

public class NotificationChrono {静态公共无效更新通知(上下文上下文,布尔运行,int id、字符串标题、字符串文本、NotificationManager 通知管理器) {Intent stopIntent = new Intent("com.corsalini.david.barcalc.STOP");PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context,0, 停止意图, 0);意图 startIntent = 新意图("com.corsalini.david.barcalc.STARTPAUSE");PendingIntent startPendingIntent = PendingIntent.getBroadcast(context,0, 开始意图, 0);NotificationCompat.Builder builder = new NotificationCompat.Builder(语境).setContentText(context.getString(R.string.notif_text)).setContentTitle(标题).setSmallIcon(R.drawable.ic_action_alarm_2).setAutoCancel(false).setOngoing(运行).setOnlyAlertOnce(真).setContentIntent(PendingIntent.getActivity(context, 10, new Intent(上下文,FrontActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0)).addAction(跑步 ?R.drawable.ic_action_pause: R.drawable.ic_action_play,跑步 ?context.getString(R.string.pause) : 上下文.getString(R.string.start), startPendingIntent).addAction(R.drawable.ic_action_stop,context.getString(R.string.stop), stopPendingIntent);NotificationManager.notify(id, builder.build());}}

问题

每秒钟通知被删除和重新创建,视觉上意味着每秒钟通知消失并重新出现在通知列表中.

我想要的只是更新 TITLE 文本,而不是每秒完全重新创建通知.可能吗?

解决方案

每次创建 Notification 时,请务必使用相同的 NotificationCompat.Builder 构建器!

尽管第一次您必须设置所有内容,但第二次使用 Builder 时,您只需设置要更新的值.之后它就像你一样调用 notificationManager.notify(id, builder.build()) .如果您使用相同的 ID,则通知会更新(很重要!).

示例:

//第一次NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentText(context.getString(R.string.notif_text)).setContentTitle(标题).setSmallIcon(R.drawable.ic_action_alarm_2).setAutoCancel(false).setOngoing(运行).setOnlyAlertOnce(真).setContentIntent(PendingIntent.getActivity(context, 10,新意图(上下文,FrontActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),0)).addAction(正在运行?R.drawable.ic_action_pause: R.drawable.ic_action_play,跑步 ?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());//第二次builder.setContentTitle(title);NotificationManager.notify(id, builder.build());

但您也可以使用 setUsesChronometer 方法.这将使用给定的时间戳自动显示计时器(可以使用 setWhen).

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 "running" boolean, timeString is the elaborated String showing the time):

NotificationChrono.updateNotification(getApplicationContext(), continueThread, 
NOTIF_ID, timeString, "Chronometer", notificationManager);

This is the NotificationChrono class:

public class NotificationChrono {

    static public void updateNotification(Context context, boolean running,
        int id, String title, String text,
        NotificationManager notificationManager) {

        Intent stopIntent = new Intent("com.corsalini.david.barcalc.STOP");
        PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context,
            0, stopIntent, 0);

    Intent startIntent = new Intent(
            "com.corsalini.david.barcalc.STARTPAUSE");
    PendingIntent startPendingIntent = PendingIntent.getBroadcast(context,
            0, startIntent, 0);

    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());
}
}

Problem

Every second the notification is deleted and recreated, visually it means that every second the notification disappears and reappears in the notification list.

What I would want is to just update the TITLE text, not recreating the notification entirely every second. Is it possible?

解决方案

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).

这篇关于更新通知文本,而不是整个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆