5

I'm trying to understand what I will need to build on my server for Push notifications to work successfully.

My thoughts were:

  1. The phone sends the notify URL to my server
  2. The server stores the information in a Database
  3. A separate process or PHP script will query the database and open continuous looping process for each device. (Each socket will be querying a 3rd party API)
  4. When there is a change detected in the API for that device a push notification will be sent to the device's notify url.

Is this the right method on what needs to be done. Isn't this going to eat up server resources or is it the expected outcome of Push a push notifications server?

I've produced a simple diagram on all this below:

enter image description here

Eran
  • 387,369
  • 54
  • 702
  • 768
Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126

3 Answers3

3

First of all, let's separate the process in the main stages needed for PUSH.

  1. Device subscription.
  2. Send the PUSH
  3. Process the notification on device.

Subscription

For the subscription, your device (more specifically, your App) must call the PUSH api,for enabling PUSH notifications. This call to the push API will give you a URL that uniquely identify the device where your application is installed and running. You should store this URL on your database, the same way you store a user's email, or a user's phone number. No special black magic here. You only use it when you need to send a communication to a user.

Send the PUSH

For the push stuff, the same approach as for email, or SMS messaging here: "One does not simply make an infinite loop and send a message if any change is detected". What you have to do is, just send the PUSH message when your application needs to. So you have the user to which you want to send a message, instead of opening a SMTP connection to send ane mail, just build the PUSH XML Message and call the URL associated with that user. Some things to consider here are:

  • Network reliability (you need to retry if you can't connect to the server).
  • Response error code-handling (you don't need to retry if the server tells you that the phone has uninstalled your application, for example).
  • Scalability. You don't want to send a PUSH message from your PHP code, because you don't know how long it will take for the task to be completed. You have to make this thing asynchronously. So just queue up all the push messages, you can create a separate process (windows service, nodeJS service, cron job, daemon, etc.) to send the PUSH, handle retries and errors and clean the queue.

Process the notification on Device

So now that you are this far, you need to handle the notification on the phone. It depends on the type of PUSH notification that you are sending:

  • Tile. You will update the image, text and counter of the application tile, if the user has put your application to the start screen. On client side you need nothing to so, as all these parameters are part of your PUSH request.
  • Toast. This one requires a title, text (limited to some 35 characters more or less) and a relative URL inside of your APP. Your application will be launched (like when you click on a Toast notification from Twitter, for example) using the URI that you specify in the payload. So a bit of data can be already injected here. You may/or may not make a request to your server for new data. It is up to you.
  • Raw. This one is pretty much silent. Is not seen by the user if your APP is not running. As you might guess, this kind of PUSH is useful to live update your running APP, instead of continuously polling your server, wasting user battery and bandwidth and wasting your server resources. You can send anything (raw bytes or strings) up to the max size of the payload allowed my Microsoft.

If yo have any more questions, don't hesitate to ask.

Bottom line: separate the PUSH sending, make it async, don't you ever forget that...

Adrian Salazar
  • 5,279
  • 34
  • 51
  • Thanks for that, I ended up building a Windows Console app on my win server to connect to web sockets under a thread. That way the load of all sockets can be stored in a ThreadPool and run in the background. Tests have shown it doesn't consume hardly any CPU Usage. – Sandeep Bansal Jun 17 '14 at 13:46
  • Is there anyway to handle Tile-type? My provider can only push tile-notifications, but I don't wanna show them to user. I'd like to make some processing before, only then show. – Vlad Sep 25 '15 at 19:10
1

Your PHP script that continually pings the database for changes...THAT is what will eat up your system resources. Push notifications go hand in hand with Event Driven Programming. This means that ideally, your code shouldn't continuously ping your DB. Rather, when something happens (ie, an "event"), THEN your code does something...like contact your phone via push notification.

Your steps for push notifications are more or less correct, but are incomplete. Step 4: the server contacts the client via the notify url (which you have). Step 5 is that the client then contacts the server to actually pull down the information it needs. That is: The new information is not provided to the client via the notify url. Once the client has its new information, then the program continues as normal (populates a list, downloads skynet, etc.)

Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • What if, rather than continuously polling the DB I create a dedicated process through some program that will be for that specific device. That will connect to the 3rd party API and when the socket has an update will notify the process and send the payload to the device? What I'm trying to gather is how is the whole architecture of all this done. – Sandeep Bansal Jun 04 '14 at 14:24
  • i *think* that will do what you want, if I understood you correctly. For example, Client A sends in a push URL. Later, Client B sends some information to the server that is relevant to Client A. The server then tells (via push) Client A that something has happened, and so Client A connects to the server and pulls down the new information. This way, Client A does not continuously ping the server, and the server is not constantly monitoring a database. The server simply responds to an event (in this case, Client B sending some information relevant to ClientA) – Russell Uhl Jun 04 '14 at 20:36
1

Your third step is very wasteful and not practical if your app is installed on more than a few devices.

Instead, each device should be subscribed to types of server updates it cares about. Your server's DB will have a mapping from each type of update you support to the list of notification channel URLs of devices that care about this update type.

When your server detects an update of type X, it would send a notification to all devices subscribed to that type of update.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • The problem for me is that the 3rd party api requires a key that is unique to each device. That information is only for the account that device is logged in to. So I can't possibly send a notification based off a category. – Sandeep Bansal Jun 04 '14 at 14:26
  • @SandeepBansal In that case, you should use a single socket to query the 3rd party API for multiple devices (or multiple sockets, but each one handles multiple devices). It makes no sense to run a process on your server for each device. It doesn't scale. – Eran Jun 04 '14 at 14:38
  • OK that makes sense. So I create a Socket and have each device open a connection that socket passing over the apiKey that will allow the 3rd party api to send over information relevant to that device. Is that what you mean? – Sandeep Bansal Jun 04 '14 at 14:45
  • @SandeepBansal No, I wasn't talking about how the devices would pass the apiKey to your server. That can be done when they pass the notification channel URI to your server. The socket[s] I was talking about would get the notification channels and apiKeys from the DB, and use them to query the 3rd party api and send push notification to the app if necessary. – Eran Jun 04 '14 at 14:49
  • OK let me add that this 3rd party api url is actually a web socket to allow me to listen in on it. All I need to do is be able to get information continuously from it for real time updates. The url looks like `wss://apiaddress/apikey`. So the socket server will loop through all devices stored in my db, won't that cause a looping mess in it all? I'm looking at a way I can send individual push notifications at any time to any device – Sandeep Bansal Jun 04 '14 at 14:54