GithubHelp home page GithubHelp logo

mryvlin / gcmmessage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from codemonkeysru/gcmmessage

0.0 2.0 0.0 40 KB

A PHP library to send messages to devices registered through Google Cloud Messaging.

License: MIT License

PHP 100.00%

gcmmessage's Introduction

Google Cloud Messaging (GCM) PHP Server Library

A PHP library for sending messages to devices registered through Google Cloud Messaging.

See: http://developer.android.com/guide/google/gcm/index.html

Example usage

use \CodeMonkeysRu\GCM;

$sender = new GCM\Sender("YOUR GOOGLE API KEY");

$message = new GCM\Message(
        array("device_registration_id1", "device_registration_id2"),
        array("data1" => "123", "data2" => "string")
);

$message
    ->notification(array("title" => "foo", "body" => "bar"))
    ->setCollapseKey("collapse_key")
    ->setDelayWhileIdle(true)
    ->setTtl(123)
    ->setRestrictedPackageName("com.example.trololo")
    ->setDryRun(true)
;

try {
    $response = $sender->send($message);

    if ($response->getNewRegistrationIdsCount() > 0) {
        $newRegistrationIds = $response->getNewRegistrationIds();
        foreach ($newRegistrationIds as $oldRegistrationId => $newRegistrationId){
            //Update $oldRegistrationId to $newRegistrationId in DB
            //TODO
        }
    }

    if ($response->getFailureCount() > 0) {
        $invalidRegistrationIds = $GCMresponse->getInvalidRegistrationIds();
        foreach($invalidRegistrationIds as $invalidRegistrationId) {
            //Remove $invalidRegistrationId from DB
            //TODO
        }

        //Schedule to resend messages to unavailable devices
        $unavailableIds = $response->getUnavailableRegistrationIds();
        //TODO
    }
} catch (GCM\Exception $e) {

    switch ($e->getCode()) {
        case GCM\Exception::ILLEGAL_API_KEY:
        case GCM\Exception::AUTHENTICATION_ERROR:
        case GCM\Exception::MALFORMED_REQUEST:
        case GCM\Exception::UNKNOWN_ERROR:
        case GCM\Exception::MALFORMED_RESPONSE:
            //Deal with it
            break;
    }
}

Also indirect message API available

use \CodeMonkeysRu\GCM;

$sender = new GCM\Sender("YOUR GOOGLE API KEY");

try {
    $response = $sender->sendMessage(
        array("device_registration_id1", "device_registration_id2"),
        array("data1" => "123", "data2" => "string"),
        "collapse_key"
    );

    if ($response->getNewRegistrationIdsCount() > 0) {
        $newRegistrationIds = $response->getNewRegistrationIds();
        foreach ($newRegistrationIds as $oldRegistrationId => $newRegistrationId){
            //Update $oldRegistrationId to $newRegistrationId in DB
            //TODO
        }
    }

    if ($response->getFailureCount() > 0) {
        $invalidRegistrationIds = $GCMresponse->getInvalidRegistrationIds();
        foreach($invalidRegistrationIds as $invalidRegistrationId) {
            //Remove $invalidRegistrationId from DB
            //TODO
        }

        //Schedule to resend messages to unavailable devices
        $unavailableIds = $response->getUnavailableRegistrationIds();
        //TODO
    }
} catch (GCM\Exception $e) {

    switch ($e->getCode()) {
        case GCM\Exception::ILLEGAL_API_KEY:
        case GCM\Exception::AUTHENTICATION_ERROR:
        case GCM\Exception::MALFORMED_REQUEST:
        case GCM\Exception::UNKNOWN_ERROR:
        case GCM\Exception::MALFORMED_RESPONSE:
            //Deal with it
            break;
    }
}

Note about cURL SSL verify peer option

Library has turned off CURLOPT_SSL_VERIFYPEER by default, but you can enable it by passing third parameter into constructor of Sender class.

You need to download root certificates and add them somewhere into your project directory. Then construct Sender object like this:

use \CodeMonkeysRu\GCM;

$sender = new GCM\Sender("YOUR GOOGLE API KEY", false, "/path/to/cacert.crt");

ChangeLog

  • v0.2 - Notifications added
  • v0.1 - Initial release

Licensed under MIT license.

gcmmessage's People

Contributors

ivariable avatar joeykrim avatar vasayxtx avatar mryvlin avatar stevetauber avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.