oxenti/cake_gcm

CakeGCM is a plugin for CakePHP to send notification to an Android device through Google Cloud Messaging. This project is a fork from ker0x's repo.

Installs: 56

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 4

Forks: 3

Type:cakephp-plugin

v1.0 2015-12-01 21:31 UTC

This package is not auto-updated.

Last update: 2024-04-27 17:14:39 UTC


README

CakeGCM is a plugin for CakePHP to send downstream message to an Android or iOS device through Google Cloud Messaging

Scrutinizer Total Downloads License

Requirements

  • CakePHP >= 3.*
  • PHP >= 5.4

Installation

Run : composer require oxenti/cake_gcm:dev-cake3 Or add it in your composer.json:

"require": {
    "oxenti/cake_gcm": "dev-cake3"
},

Enable plugin

In config/bootstrap.php file add :

Plugin::load('CakeGcm', ['autoload' => true]);

or uncomment :

Plugin::loadAll();

Usage

In src/Controller/AppController.php file, add :

$this->loadComponent('CakeGcm.Gcm', [
    'api' => [
        'key' => '*****'
    ]
]);

in your Controller's initialize() method. Replace ***** by your API Key.

To get an API key, follow the instructions in this link: http://developer.android.com/google/gcm/gs.html#access-key

Then, in an action of your Controller, add the following code:

if ($this->Gcm->send($ids, $data, $parameters)) {
    // do some stuff
} else {
    // do other stuff
}

where:

  • $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 = $this->Gcm->response();

Examples

Send an empty notification to a device:

$this->Gcm->send('1');

Send a notification to a device:

$this->Gcm->send('1', [
    'notification' => [
        'title' => 'Hello World',
        'body' => 'My awesome Hellow World!'
    ]
]);

or

$this->Gcm->sendNotification('1', [
    'title' => 'Hello World',
    'body' => 'My awesome Hellow World!'
]);

Send a notification to multiple devices:

$this->Gcm->send(
    ['1', '2', '3', '4'],
    [
        'notification' => [
            'title' => 'Hello World',
            'body' => 'My awesome Hellow World!'
        ]
    ]
);

or

$this->Gcm->sendNotification(
    ['1', '2', '3', '4'],
    [
        'title' => 'Hello World',
        'body' => 'My awesome Hellow World!'
    ]
);

Send a notification with lots of data

$this->Gcm->send(
    ['1', '2', '3', '4'],
    [
        'data-1' => 'Lorem ipsum',
        'data-2' => 1234,
        'data-3' => true
    ]
);

Send datas to a device:

$this->Gcm->send(
    ['1', '2', '3', '4'],
    [
        'data' => [
            'data-1' => 'Lorem ipsum',
            'data-2' => '1234',
            'data-3' => 'true'
        ]
    ]
);

or

$this->Gcm->sendData(
    ['1', '2', '3', '4'],
    [
        'data-1' => 'Lorem ipsum',
        'data-2' => 1234,
        'data-3' => true
    ]
);

Send a notification and some datas to a device at the same time:

$this->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
        ]
    ]
);

Send a notification with extra parameters:

$this->Gcm->sendNotification(
    ['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'
    ]
);

License

The MIT License (MIT)

Copyright (c) 2015 Romain Monteil

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.