更新时间:2023-02-26 21:05:55
我一直在为我的 消息应用.我们希望用户在用户点击通知之前立即看到消息.我们面临的问题:
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
没有警报正文的通知甚至可能不会发送到设备content-available
- 在大多数情况下它会被发送并且应用能够处理它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.