iOS - Chat application with push notification

2019-06-15 02:32发布

问题:

My question is simple, is it a good idea to develop chat application using push notifications? Hence push notifications are not reliable and there is no guaranty either they would arrive or not. If it is not reliable, which technique should be used for real time chat application?

回答1:

Why not?

It's definitely possible to build a chat using iOS push notifications. @Aaron points are interesting, but don't make sense in my opinion for the following reasons:

  1. If your app sends too many push notifications (and it will if you use that as the means of "chatting") no one will use your app because it will be annoying.

The only case here is when the app is not running and even then, apple will only send the last notification if there are too many in a row. When the app is in any other state (background, foreground active & inactive, suspended), the notification can be handled silently, using content-available : 1 in your push notification. If that's a worry for you, simply use badge notifications instead of alert notifications.

  1. The user can also disable push notifications for your app so you can't rely on that as the only means of communication.

That is wrong, that doesn't prevent notifications for being sent to the phone, they are just not being shown to the user. See here. You can therefore handle silent notifications even though the user has disabled push notifications. This is just a setting, the device will still have a device token, and the backend will still be in a position to send push notifications.

However..

It is true that some remote notifications might not be delivered and using their content is not a good idea. But that is not a big issue..

The best is to use your notification as a refresh me I got a new message kind of notification. Once you get a new notification, ask the server for new messages in this chat, and update the table. We have implemented this in one of our project and are very happy with the results so far. Some say Premature Optimization is the root of all evil, in that case, you could spend a lot of time using sockets, but you can be up and running in no time with push notifications. Optimize later.

This works great:

{
    "aps" : {
        "content-available" : 1,
        "alert" : "This is my new notification",
    }
    "conversationId": 23,
    "senderId":44
}


回答2:

No. It is not a good idea, for at least two reasons:

  1. If your app sends too many push notifications (and it will if you use that as the means of "chatting") no one will use your app because it will be annoying.
  2. The user can also disable push notifications for your app so you can't rely on that as the only means of communication.

You want a polling system of some sort with a client/channel relationship. There are lots of server systems out there that can do this for you. WebSync is one:

http://www.frozenmountain.com/websync/



回答3:

Disclaimer: Recently needed to develop a chat, and used push notifications for receiving the messages, it worked flawlessly. Sockets may be a good idea, but you will have a hard time transversing NAT if that is not done for you. So I don't agree with past me on this one.

Push notifications would be great if your app needed to be awoken from the background when the user got a new message, not for the chat itself.

There is a range of technologies that could be used, the simplest one, in my opinion would be using sockets (I think it simple due to the ammount of tutorials on the subject online)

one example: http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server

Long story short, push notifications would be a fine addition to a chat application, but just as an extra (a very good extra indeed, they are not so unreliable and you can always do some logic to detect in the client if the push arrived, like making the app resend some sort of code for every push received, and resend the push if no code was received in a given time).

Just be aware that you will need a server to make the pushes work, so if your chat app is a success, it might cost you some money...