iOS - Chat application with push notification

10,137

Solution 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
}

Solution 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/

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

Share:
10,137
Adnan
Author by

Adnan

Welcome to my corner on the web. I’m Associate Architect specializing in full stack mobile and web development. Experienced with all stages of the development cycle for dynamic and enterprise level projects and products. Well-versed in numerous programming languages and development platforms including Android (Java & Kotlin), iOS (Objective-C & Swift), backend (Java-EE & Nodejs), cross-platform (Ionic & React Native), frontend (Angular) and many more. Having strong background in project management and customer relations.

Updated on June 03, 2022

Comments

  • Adnan
    Adnan about 2 years

    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?

  • drowa
    drowa over 9 years
    Sockets would require the antenna to be on as long the connection is open. This would consume too much energy.
  • divergio
    divergio about 9 years
    "You can therefore handle silent notifications even though the user has disabled push notifications." Are you certain about this? In my testing, disabling push notifications seems to prevent silent notifications from reaching the app.
  • Mick
    Mick about 9 years
    @divergio absolutely certain! We do it for our apps. You still have a device token even though the user chooses to disable push notifications. When the user chooses not to receive push notification, that only means that he doesn't want to physically be alerted of a push notification. You can do whatever you wish silently!