android 在通知中嵌入换行符

发布于 2025-01-08 15:05:13 字数 82 浏览 2 评论 0原文

我需要在通知中嵌入换行符。我有两行不同的数据,我需要嵌入换行符来区分这两行。 我修改了反斜杠-n,但这不起作用。

有办法做到这一点吗?

I need to embed a newline in a notification. I have two lines of data that are different and I need to embed a newline to distinguish between the two.
I trued a backslash-n, but that does not work.

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2025-01-15 15:05:13

尽管其他人在这里说过,您不需要使用 RemoteViews 或自定义布局来显示多行!
多行文本是可能的,但仅限于展开通知时。

为此,您可以使用 NotificationCompat.BigTextStyle。如果您阅读此处的开发指南,那么您会注意到他们提到了这一点提示:

提示:要在文本中添加格式(粗体、斜体、换行符等),您可以 使用 HTML 标记添加样式

因此,换句话说:

val title = "My Title"
val body = "Line 1<br>Line 2<br><i>Italic Line 3</i>"
val formattedBody = SpannableString(
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) Html.fromHtml(body)
   else Html.fromHtml(body, Html.FROM_HTML_MODE_LEGACY)
)

NotificationCompat.Builder(context, channelId)
   .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
   .setContentTitle(title)
   .setContentText(formattedBody)
   .setStyle(NotificationCompat.BigTextStyle().bigText(formattedBody).setBigContentTitle(title))
   .setSmallIcon(R.drawable.ic_small_notification_icon)
   .build()

关于其工作原理需要注意的事项:

  1. 标题可以有格式(即不要尝试在此处使用 HTML)
  2. 正文可以具有我们想要的尽可能多的格式,但是我们必须注意,当通知折叠时,换行符不会显示。所有其他 HTML 格式在折叠/展开状态下都可以正常工作。您可以根据需要使用粗体、斜体等(我认为)

如果您不想在推送通知中发送 HTML 格式,那么另一种选择是使用某种 Markdown 格式并在通知之前手动处理它使用 SpannableStringBuilder 进行构建

You do not need to use RemoteViews or a custom layout to show multiple lines, despite what others have said here!
Multiline texts are possible, but only when the notification is expanded.

To do this, you can use NotificationCompat.BigTextStyle. And if you read the dev guides here then you'll notice that they mention this tip:

Tip: To add formatting in your text (bold, italic, line breaks, and so on), you can add styling with HTML markup.

So, in other words:

val title = "My Title"
val body = "Line 1<br>Line 2<br><i>Italic Line 3</i>"
val formattedBody = SpannableString(
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) Html.fromHtml(body)
   else Html.fromHtml(body, Html.FROM_HTML_MODE_LEGACY)
)

NotificationCompat.Builder(context, channelId)
   .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
   .setContentTitle(title)
   .setContentText(formattedBody)
   .setStyle(NotificationCompat.BigTextStyle().bigText(formattedBody).setBigContentTitle(title))
   .setSmallIcon(R.drawable.ic_small_notification_icon)
   .build()

Things to note about how this works:

  1. The title can have no formatting (i.e. don't try to use HTML here)
  2. The body can have as much formatting as we want, but we must note that the line breaks won’t display when the notification is collapsed. All other HTML formatting works fine in both collapsed/expanded states. You can bold, italicize, etc. as much as you want (I think)

If you don't want to send HTML formatting in your push notification, then another option is to use markdown formatting of some sort and manually handle this before your notification building by using SpannableStringBuilder

但可醉心 2025-01-15 15:05:13

据我所知,除了创建自定义 Notification 布局之外,没有其他方法可以做到这一点。 Notification 文档提供了执行此操作的分步过程,并且非常全面。 这是指向该内容的链接。

创建这两行正如您所需要的,仅意味着在布局 XML 中添加额外的 TextView 。然后,您只需将每个 TextView 的文本设置为两行数据,如示例所示。

As far as I know there is no way to do this other than creating a custom Notification layout. The Notification documentation has a step-by-step process on doing this and is quite thorough. Here is the link to that.

Creating the two lines as you require would just mean putting in an extra TextView in the layout XML. Then you would just set each TextView's text to your two lines of data, as the example shows.

就像说晚安 2025-01-15 15:05:13

您需要在您的应用程序中创建自定义通知生成器。更多详细信息请参见:https://developer.android.com/guide /topics/ui/notifiers/notifications.html#CustomExpandedView,我花了很长时间才找到让 firebase 在所有情况下都调用 onMessageReceived 的方法。

  1. 中的应用程序
  2. 前台应用程序
  3. 后台应用程序中的应用程序被杀死。

基本上,您通过 api 发送给 firebase 的消息在发送给 firebase 服务器的 json 请求中不得包含“notification”键,而只能使用“data”键,如下所示,firebase 服务将调用在上面列出的所有情况下您的 onMessageReceived 。

{
  "to": "/topics/your_topic",
   "data": {
       "key1":"data_key1",
       "title":"",
       "line1":"data1",
       "line2":"data2"
   }
} 

You need to make custom notification builder in your app. more detail here : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView, It takes me so long to find out the way to make firebase to call onMessageReceived in all cases.

  1. app in foreground
  2. app in background
  3. app was killed.

basically your message to firebase via api must not have "notification" key in your json request to firebase server and only use "data" key as show below, firebase service will call your onMessageReceived in all cases listed above.

{
  "to": "/topics/your_topic",
   "data": {
       "key1":"data_key1",
       "title":"",
       "line1":"data1",
       "line2":"data2"
   }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文