Caesar Apps Caesar Apps - 2 months ago 13
AngularJS Question

How to implement push notification in cordova with angularjs using php json array

I have a project were I am required to add push notification for Cordova AngularJS Push Notification using PHP on server side which gives JSON array as output. I can implement chat etc using this, but I want to know how to implement it in push notification or accessing native app API. Firstly I thought using Javascript SetInterval, but it slow downs the app, and checks the API everytime...

Answer

For Cordova push notification you can take a look here: http://phonegappro.com/tutorials/apache-cordova-phonegap-push-notification-tutorial-part-1/

Still, let's review some other methods of sending data to the client using Web APIs:

  1. Long polling - keep the connection open on the server side with no or long timeout, and return a result when you want to send a push notification.

  2. Use WebSockets - open a steady TCP connection to the server and transmit messages in both directions (unlike HTTP's request/response model).

  3. Use PushManager - this is an experimental technology that is not yet supported on mobile devices, so I guess this is a no-go for you.

  4. Use simple polling - every now and then poll the server for a new message.

Long polling and Web Sockets will overload the server if you'll have a lot of concurrently open connections, so I wouldn't go there. Also Web sockets are mostly used for communicating between clients (server passes the client info to both clients, then they can communicate on their own without the sever intermediating between them).

PushManager is too new a technology not yet supported in mobile.

For what you want (mobile I guess, as you want to use Cordova), and without knowing what you are actually trying to achieve, I would say go with #4. I understand you already tried it, but perhaps try to lower the polling rate to every 30 seconds or so.

I understand that while debugging it doesn't seem nice to send something from the server then wait the better part of a 30 seconds period until it appears on the client, but if you think of it from the user's view point you'll see that the user doesn't know when the server sends the data, so it does appear immediate.

However, if you're writing a chat client, then I would go with web-sockets, using the server to connect the two (or more) clients in the chat and letting them pass the messages directly. If you want the chat to be server backed, just periodically send the transcript to the server using simple AJAX.

Comments