首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重新显示Ticker文本?

如何重新显示Ticker文本?
EN

Stack Overflow用户
提问于 2015-01-16 15:09:20
回答 1查看 2.5K关注 0票数 1

对于我的应用程序,我使用一个通知ID来避免干扰用户的通知菜单。我的每个通知都有Ticker文本。当没有来自我的应用程序的通知,并且用户得到通知时,代码显示文本。如果通知已经存在,并且仅被更新,则不会显示代码标记文本。

我做了一个非常烦人的工作,在通知之前我取消了通知,但这最终导致了一个非常明显的滞后与振动。

目前,我是如何发出通知的:

代码语言:javascript
复制
    mBuilder.setSmallIcon(R.drawable.actionbar_logo)
            .setContentTitle(extras.getString("title"))
            .setContentText(extras.getString("summary"))
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(extras.getString("extended_text"))
            )
            .setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("mainActivityNotification", true);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(contentIntent);

    final Notification notification = mBuilder.build();
    notification.tickerText = extras.getString("title") + "\n" +
                              extras.getString("summary") + "\n" +
                              extras.getString("post_body");

    mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-25 18:12:08

Android不会再次显示代码文本,如果它和以前一样的话。

您可以使用setTicker API of Notification.Builder类,而不是使用Notification对象构造代码标记文本,因为添加了Notification.Builder以便于构造通知。

如果您的意图是重新显示相同的代码文本(来自先前发布的通知),那么只需添加以下一行:

代码语言:javascript
复制
mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());

在你发布你的第一份通知之后。

因此,完整的代码如下所示:

代码语言:javascript
复制
mBuilder.setSmallIcon(R.drawable.actionbar_logo)
            .setContentTitle(extras.getString("title"))
            .setContentText(extras.getString("summary"))
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(extras.getString("extended_text"))
            )     //consider using setTicker here
            .setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("mainActivityNotification", true);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(contentIntent);

    final Notification notification = mBuilder.build();

    //consider using setTicker of Notification.Builder
    notification.tickerText = extras.getString("title") + "\n" +
                              extras.getString("summary") + "\n" +
                              extras.getString("post_body");

    mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);
    //second time onward, add your changed content like setContentText, setTicker etc.
    mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27986937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档