基于iOS套接字的聊天,如何接收应用程序在后台时发送的消息?

标签 ios iphone objective-c

为了好玩,我构建了一个基于套接字的小聊天应用程序。它通过 nodejs 服务器通过 tcp 套接字进行通信。它成功地来回发送消息。但是,当我按下主页按钮并将我的应用程序置于后台时出现问题 - 此时套接字似乎“暂停”并且在此期间发送的所有消息都丢失了。

我已经设置了应用程序在后台时的推送通知设置,但由于套接字在应用程序中未激活,因此消息从未真正到达应用程序。

我该如何处理?我有哪些选择? 我有一些想法,但我喜欢这里的一些关于人们如何处理这个问题的意见。

我的想法:

  1. Add a "loadHistory" method on my nodejs server that sends all the history of a channel. So that my app can call a certain URL and get a JSON formated response with all the messages. Maybe this should be done each time a table item is touched?

  2. Make use of the new iOS7 background running features? (Im not to sure its possible to keep the socket alive with that)

  3. Implement a way for the server to know if a message has been delivered, if it could not be delivered it stores the message on the database. As soon as the client goes online again it sends the messages over tcp. This would require a lot of added functionality to the server so id like to avoid this if possible.

最佳答案

如果你想离线消息,就需要第三种方式,每收到一条消息,你需要向服务器发送一个通知,告诉它消息已发送,当你连接上时,你应该取回丢失的消息.

您可以使用 voip,但如果您的应用被杀死,您的应用将无法接收消息。 (如果你添加了voip模式,但你的应用不是关于语音的,苹果会屏蔽你的应用)

关于基于iOS套接字的聊天,如何接收应用程序在后台时发送的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19630871/

相关文章:

ios - Nest-API 恒温器设备未显示在 REST 请求中

ios - ios中的ssh协商过程

ios - 在单行 UILabel 旁边居中 NSTextAttachment 图像

ios - 应用程序窗口预计会有 Root View Controller 抛出警告

iPhone/xCode - 第四次运行动画代码时的 EXC_BAD_ACCESS

ios - iOS 应用程序沙箱中的默认文件

ios - 在 UIWebView 中加载 HTTPS url

iphone - 如何在 iOS 应用程序中显示各种文件类型的图标?

iphone - 在 iOS 上运行无限循环动画

iphone - 在 iPhone 旋转时缩放图像以适合屏幕