且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在显示推送通知 VS 静默推送之前触发后台刷新的推送通知

更新时间: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:

  • 有效载荷大小限制.iOS 7 的有效载荷只能有 256 个字节
  • 单个静默通知不会启动应用程序(如果应用程序未运行)
  • content-available 没有警报正文的通知甚至可能不会发送到设备
  • 后台提取不受您的应用程序控制,因此您可能永远不会收到所需的信号,因此我们不能依赖此功能.但这可能有助于实现我们想要的目标
  • iOS 8 有很大的负载空间 - 2KB
  • 如果您发送警报正文 content-available - 在大多数情况下它会被发送并且应用能够处理它
  • The payload size limitation. iOS 7 can have only 256 bytes for payload
  • single silent notifications will not start an app, if it is not running
  • content-available notifications without alert body may even not be delivered to the device
  • Background fetch is not controlled by your app, so you may never receive the desired signal, so we cannot rely on this feature. But this may be helpful as an additional way to achieve what we want
  • iOS 8 has a lot of space for payload - 2KB
  • if you send alert body and content-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.