【问题标题】:Add icon at the end of body text in Local notification在本地通知正文末尾添加图标
【发布时间】:2018-01-18 08:27:41
【问题描述】:

如何在通知(本地通知)正文的末尾添加小图像(图标)。 我正在使用 NotificationCompat.Builder 创建通知。我想在通知正文的末尾添加小图标。

【问题讨论】:

    标签: android android-notifications android-bitmap


    【解决方案1】:

    我认为你可以使用spannable String,因为setContentText(CharSequence message) 接受CharSequence

      String msg="Hello world";
        SpannableStringBuilder ssb = new SpannableStringBuilder(msg);
        ssb.setSpan(new ImageSpan(this, R.drawable.app_icon), msg.length()-1, msg.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    

    并将其添加到通知生成器中的消息中。

     NotificationCompat.Builder  notification
                    = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.app_icon)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(ssb))
                    .setContentTitle("Hello")
                    .setContentText(ssb);
    

    我没有测试过。但我认为它应该工作。注意索引。

    【讨论】:

    • 您的代码将图像添加到字符串,如果我将字符串设置为 textview 工作正常,但在通知中我没有得到图像。
    • 是的,我说过我没有测试过。虽然它应该与CharSequence 一起使用,但它很奇怪。谢谢澄清。您应该继续使用RemoteView。谢谢
    【解决方案2】:

    您可以使用NotificationCompat.Builder#setCustomContentView(RemoteViews)。在这个related question 你可以看到一个例子。

    考虑到通知模板在 Android N 中发生了变化,如果你走那条路,通常用户期望一个一致的 UI。见Android N: Introducing upgraded Notifications

    【讨论】:

      猜你喜欢
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      相关资源
      最近更新 更多