Push notifications from server to user with PHP/JavaScript
In modern browsers, Websockets or in older browsers, "long polling". There are also libraries that can abstract some of this (like comet).
A very common library in use now is socket.io
which automatically connects with the best technology available, using webSockets if available and falling back to other technologies like long polling if not available.
Some other references:
How do I implement basic "Long Polling"?
COMET (server push to client) on iPhone
More on Long Polling
Is there a difference between long-polling and using Comet
Pushing the web with websockets
Oh HTML5, Push Data to my Mobile
How to send notification to the browser of the selected user
Yes, you can target specific users as described in the docs.
1. Keep track of the user ID on subscription
When you subscribe a user to push notifications, attach some metadata to its subscription:
pushpad('subscribe', null, uid: 'User1', uidSignature: 'YOUR_SIGNATURE');
- You can use any string as the user ID (
uid
): in this example I use'User1'
, but you can simply call it'1'
- The
uidSignature
can be generated using the PHP library:Pushpad\Pushpad::signature_for($uid);
- You can read more about the
pushpad('subscribe')
method and the similarpushpad('uid')
method in the Javascript SDK reference
2. Send notifications to specific users
When you send push notifications from your server (with the PHP library) you can target specific users:
$notification = new Pushpad\Notification(array(
'body' => "Hello world!"
));
# deliver to a user
$notification->deliver_to('User1');
# deliver to a group of users
$notification->deliver_to(['User1', 'User2', 'User3']);
Push notification to the client browser
As you want to implement this in CakePHP (so I assume it's a web-based application), the user will have to have an 'active' page open in order to receive the push messages.
It's worth looking at the first two answers to this, but also just think about how other sites might achieve this. Sites like Facebook, BBC, Stackoverflow all use techniques to keep pages up to date.
I suspect Facebook just uses some AJAX that runs in a loop/timer to periodically pull updates in a way that would make it look like push. If the update request is often enough (short time period), it'll almost look realtime. If it's a long time period it'll look like a pull. Finding the right balance between up-to-dateness and browser/processor/network thrashing is the key.
The actual request shouldn't thrash the system, but the reply in some applications may be much bigger. In your case, the data in each direction is tiny, so you could make the request loop quite short.
Experiment!
How to send push notifications to multiple devices using php script using FCM?
Try to send device ID of multiple devices as an array. In your case,
$registration_ids must be an array of device IDs.
E.g
$registration_ids = array('Device ID 1', 'Device ID 2');
Related Topics
How to Pass Parameters into a PHP Script Through a Webpage
How to Get Current PHP Page Name
Which Coding Style You Use for Ternary Operator
How to Access Service Container in Symfony2 Global Helper Function (Service)
When to Use Index.PHP Instead of Index.Html
PHP - Csrf - How to Make It Works in All Tabs
How to Get Email and Their Attachments from PHP
How to Generate Random Password with PHP
Redirecting from Http to Https with PHP
Can PHP Detect If Its Run from a Cron Job or from the Command Line
Symfony 2.0 Getting Service Inside Entity
How to Execute PHP with Extension File.Php.Jpg
While ($Row = MySQL_Fetch_Array($Result)) - How Many Loops Are Being Performed