Welcome to CakeGCM’s documentation!¶
Contents:
Introduction¶
CakeGCM¶
CakeGCM is a plugin for CakePHP to send downstream message to an Android or iOS device through Google Cloud Messaging.
Requirements¶
- PHP >= 5.6
- CakePHP >= 3.4.*
Installation¶
Using Composer¶
To install this plugin, run composer require ker0x/cake-gcm
or add this snippet in your project’s composer.json
.
{
"require": {
"ker0x/cake-gcm": "~2.0"
}
}
Enable plugin¶
You need to enable the plugin in your config/bootstrap.php
file:
Plugin::load('ker0x/CakeGcm');
If you are already using Plugin::loadAll();
, then this is not necessary.
Get an API key¶
Go to https://console.developers.google.com/apis/library and enable Google Clous Messaging API
Use the Gcm class¶
First, you have to include it :
use ker0x\CakeGcm\Webservice\Gcm;
Then, in your code:
$Gcm = new Gcm(['api' => ['key' => '*****']]);
$Gcm->send($ids, $payload, $parameters);
where:
*****
is your API key (required)$ids
is a string or an array of device ids. (required)$payload
is an array containing the notification and/or some datas that will be passed to a device. (optional)$paramaters
is an array of parameters for the notification. (optional)
You could have the response of the request by using the function response()
:
$response = $Gcm->response();
Use the component¶
In src/Controller/AppController.php
, add :
$this->loadComponent('ker0x/CakeGcm.Gcm', [
'api' => [
'key' => '*****'
]
]);
in your Controller’s initialize() method.
Then, in an action of one of your Controller, add the following code:
if ($this->Gcm->send($ids, $payload, $parameters)) {
// do some stuff
} else {
// do other stuff
}
$response = $this->Gcm->response();
Examples¶
Send notifications¶
Send an empty notification to a device:
$Gcm->send('1');
Send a notification to a device:
$Gcm->send('1', [
'notification' => [
'title' => 'Hello World',
'body' => 'My awesome Hellow World!'
]
]);
or with the shortcut
$Gcm->sendNotification('1', [
'title' => 'Hello World',
'body' => 'My awesome Hellow World!'
]);
Send a notification to multiples devices:
$Gcm->send(
['1', '2', '3', '4'],
[
'notification' => [
'title' => 'Hello World',
'body' => 'My awesome Hellow World!'
]
]
);
or
$Gcm->sendNotification(
['1', '2', '3', '4'],
[
'title' => 'Hello World',
'body' => 'My awesome Hellow World!'
]
);
Send datas¶
Send datas to a device
$Gcm->send('1', [
'data' => [
'data-1' => 'Lorem ipsum',
'data-2' => 1234,
'data-3' => true
]
]);
or with the shortcut
$Gcm->sendData('1', [
'data-1' => 'Lorem ipsum',
'data-2' => 1234,
'data-3' => true
]);
Send notifications and datas¶
Send a notification and some datas to multiple devices at the same time:
$Gcm->send(
['1', '2', '3', '4'],
[
'notification' => [
'title' => 'Hello World',
'body' => 'My awesome Hellow World!'
],
'data' => [
'data-1' => 'Lorem ipsum',
'data-2' => 1234,
'data-3' => true
]
]
);
Extra parameters¶
Send a notification with extra parameters:
$Gcm->send(
['1', '2', '3', '4'],
[
'notification' => [
'title' => 'Hello World',
'body' => 'My awesome Hello World!'
]
],
[
'delay_while_idle' => true,
'dry_run' => false,
'time_to_live' => 86400,
'collapse_key' => 'Gcm',
'restricted_package_name' => 'my_awesome_package'
]
);