友盟 推送自定义通知_推送通知中自定义布局的最佳做法

友盟 推送自定义通知

This article assumes that readers are familiar with building Custom Layouts for Push Notification and does not touch upon what is custom layouts and how to build them. You can refer to the documentation to learn how to build custom notification layouts.

本文假定读者熟悉构建推送通知的自定义布局,并且不涉及什么是自定义布局以及如何构建它们。 您可以参考文档以了解如何构建自定义通知布局。

什么是推送通知? (What is a Push Notification?)

A notification is a message that Android displays outside your app’s UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.

通知是指Android显示在应用程序用户界面之外的消息,旨在向用户提供提醒,其他人的交流或应用程序中的其他及时信息。 用户可以点击通知以打开您的应用或直接从通知中执行操作。

Today more and more companies are running their marketing campaigns via push notifications. This leaves every user's notification drawer very cluttered.

如今,越来越多的公司通过推送通知进行营销活动。 这使每个用户的通知抽屉非常混乱。

Image for post
Notification Clutter
通知杂物

In order to stand out from the crowd, many applications are using different notification layouts to grab users' attention.

为了在人群中脱颖而出,许多应用程序都在使用不同的通知布局来吸引用户的注意力。

Image for post
Notification Custom View
通知自定义视图

While this helps in grabbing the user’s attention but if not done right it can go terribly wrong, example text might not be visible or there could be an additional tint in the notification content as shown below.

虽然这有助于引起用户的注意,但是如果操作不正确,可能会导致严重错误,但是示例文本可能不可见,或者通知内容中可能还会显示其他色彩,如下所示。

Image for post
Gone Wrong
出错了

为什么会发生这些问题? (Why do these issues happen?)

These issues come up when both Light and Dark Themes are not handled in the styling applied to the custom layouts or text color is hardcoded to a specific color(black in most cases). The system then tries to handle the situation for you trying to apply an inverse and resulting in something like shown above.

当在应用于自定义布局的样式中未同时处理浅色主题和深色主题时,或者将文本颜色硬编码为特定颜色(大多数情况下为黑色)时,就会出现这些问题。 然后,系统会尝试为您处理这种情况,以尝试应用反函数并导致出现如上所示的情况。

如何获得正确的自定义布局? (How to get the custom layout right?)

To get a custom layout right we need to take care of 2 important things:

为了获得正确的自定义布局,我们需要注意以下两点:

  • Layout Height

    布局高度
  • Text Color of the Content

    内容的文字颜色

布局高度 (Layout Height)

The right notification height is important so that the content is not clipped and the complete and the right message is shown to the user.

正确的通知高度非常重要,这样就不会剪切内容,并且不会向用户显示完整且正确的消息。

For default notifications, the maximum layout height varies based on the OS version or API Level.

对于默认通知,最大布局高度取决于操作系统版本或API级别。

  • Pre-Android N - The height of the notification is 64dp when collapsed and 256dp when expanded.

    Android之前的N-折叠时通知的高度为64dp,展开时通知的高度为256dp。
  • Android N and above — The height of the notification is 100dp when collapsed and 300dp when expanded(approx).

    Android N及更高版本-折叠时通知的高度为100dp,展开(大约)时为300dp。

While the maximum height of the notification layout on Android N and above devices is greater though some OEMs in their custom launchers and skins still have the height fixed as 64dp and 256dp respectively for collapsed and expanded. Hence it is best to stick to 64dp and 256dp respectively for collapsed and expanded, even the official documentation suggests the same

尽管某些OEM在其自定义启动器和外观上的OEM仍将折叠和展开的高度分别固定为64dp和256dp,但在Android N及更高版本的设备上,通知布局的最大高度更大。 因此,最好是分别使用64dp和256dp进行折叠和展开,即使官方文档中建议也是如此

When you use a custom notification layout, take special care to ensure that your custom layout works with different device orientations and resolutions. While this advice applies to all UI layouts, it’s especially important for notifications because the space in the notification drawer is very restricted. The height available for a custom notification layout depends on the notification view. Usually, collapsed view layouts are limited to 64 dp, and expanded view layouts are limited to 256 dp.

使用自定义通知布局时,请格外小心,以确保自定义布局可在不同的设备方向和分辨率下使用。 尽管此建议适用于所有UI布局,但对于通知尤为重要,因为通知抽屉中的空间非常有限。 自定义通知布局的可用高度取决于通知视图。 通常,折叠视图的布局限制为64 dp,展开视图的布局限制为256 dp。

内容的文字颜色 (Text Color of the Content)

The text color of the content in the notification drawer should always be inverse of the applied or used System Theme, i.e. black text when Light Theme is enabled and white text when Dark Theme is enabled.

通知抽屉中内容的文本颜色应始终与应用或使用的系统主题相反,即,启用浅色主题时为黑色,启用深色主题时为白色。

While there could be different ways to handle this text color like programmatically checking whether Dark Theme is enabled or not and then set the color programmatically at runtime, but the best way to handle this would be using Notification Style.

虽然可能有不同的方法来处理此文本颜色,例如以编程方式检查是否启用了“深色主题”,然后在运行时以编程方式设置颜色,但是处理此文本颜色的最佳方法是使用“通知样式”。

You can extend your text style with the Notification Styles defined in the androidx.core:core library and customize the content as required. Below are the themes you should be using.

您可以使用androidx.core:core库中定义的通知样式扩展文本样式,并根据需要自定义内容。 以下是您应该使用的主题。

Make sure you do not set the text color explicitly in your style.

确保您没有在样式中显式设置文本颜色。

The TextAppearance.Compat.Notification.* style always ensures that the text color is always an inverse of the System which ensures text readability. When using these styles the system knows that the color is handled and it does not try to inverse the color to handle readability etc.

TextAppearance.Compat.Notification.*样式始终确保文本颜色始终是System的反色,从而确保文本的可读性。 使用这些样式时,系统会知道已处理颜色,并且不会尝试将颜色反转以处理可读性等。

翻译自: https://medium.com/@umangchamaria/best-practices-for-custom-layouts-in-push-notifications-a1104c4f9098

友盟 推送自定义通知

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值