更新时间:2022-04-01 00:44:56
我一直在努力完成留言应用。我们希望用户在用户点击通知之前就能看到该消息。
我们面临的问题:
I've been struggling with the same task in my messaging app. We wanted users to see the message right before user taps the notification. What we faced with:
内容可用
没有警报正文的通知甚至可能无法发送到设备内容可用
- 在大多数情况下会提供,并且应用程序可以处理它content-available
notifications without alert body may even not be delivered to the devicecontent-available
- it will be delivered in most cases and an app is able to process it所以我们找到了唯一可接受的解决方案:我们决定仅在ios8 +中使用此功能。我们使用 content-available
密钥发送可见推送通知,如果进程正在运行/冻结,我们可以处理通知有效负载,并且如果应用程序是没跑。如果应用程序收到推送通知,它会将警报文本正文写入本地数据库,因此用户可以在对话中读取它。根据我们的统计数据,消息的平均大小不超过200个符号,因此大多数情况下不需要额外的请求。如果消息长于200个符号,我们使用额外参数扩展有效负载体,该参数用于在推送通知处理中请求文本体。用户将看到文本的裁剪版本,但在请求完成后,我们使用收到的值重写本地数据库中的消息。
So we came to the only acceptable solution: we decided to make this feature in ios8+ only. We send visible push notifications with content-available
key which allows us to process the notification payload if the process is running/frozen and both be able to present the notification if the app is not running. If an app receives push notification, it takes alert text body and writes it into local database, so the user is able to read it in conversation. According to our stats, the average size of the message is not more than 200 symbols, so most of the time no extra requests are required. If the message is longer than 200 symbols, we extend payload body with extra parameter which is used to request text body in push notification processing. The user will see a cropped version of text, but after a request is done, we rewrite a message in local database with the received value.
因此,该技术允许我们在大多数情况下立即向用户显示收到的消息+如果应用程序未运行,我们会在应用程序启动后立即向我们的服务器发出请求以获取丢失的消息。这是我们可以在iOS上获得的最快和最可接受的案例。希望我的经验能帮助你实现你想要的。
So, that technique allows us to show a received message to the user immediately in most of the cases + if the app was not running, we make a request to our server to fetch missing messages right after application start. This is the fastest and the most acceptable case we could get on iOS. Hope my experience will help you to implement what you want.