且构网

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

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

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


  • 有效负载大小限制。 iOS 7只能有256个字节用于有效负载

  • 单个静默通知无法启动应用程序(如果未运行)

  • 内容可用没有警报正文的通知甚至可能无法发送到设备

  • 后台提取不受您的应用程序控制,因此您可能永远不会收到所需的信号,所以我们不能依赖这个功能。但这可能有助于实现我们想要的另一种方式

  • iOS 8有足够的空间用于有效载荷 - 2KB

  • 如果您发送警报正文 内容可用 - 在大多数情况下会提供,并且应用程序可以处理它

  • 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.