$Command->appendParameter(new Option('data', 'd', 'payload data', Option::TYPE_STRING), function ($name, $value) use(&$data) { $data = json_decode($value, true); }); $Command->appendParameter(new Option('id', 'i', 'device registration id', Option::TYPE_STRING, true), function ($name, $value) use(&$id) { $id = $value; }); $Command->appendParameter(new Option('key', 'k', 'authorization key', Option::TYPE_STRING, true), function ($name, $value) use(&$key) { $key = $value; }); try { $Command->parse(true); // Create payload instance $Data = new CustomPayloadData($data); // Create and initialize message instance $Message = new PayloadMessage(); $Message->setRegistrationIds($id)->setType(PayloadMessage::TYPE_JSON)->setData($Data); // Create GCM client $Client = new Client(); $Client->getRequest()->setTimeout(5); $Client->getRequest()->setConnectTimeout(60)->setSslVersion(6); $Client->setAuthorizationKey($key); // ...and send the message $Response = $Client->send($Message); printf(<<<'EOD' success: %s failure: %s EOD , $Response->getSuccessCount(), $Response->getFailureCount()); } catch (RequiredOptionException $Ex) { $Command->displayHelp();
* See the License for the specific language governing permissions and * limitations under the License. * * GCM message sending example * @author alxmsl * @date 5/28/13 */ include '../vendor/autoload.php'; use alxmsl\Google\GCM\Client; use alxmsl\Google\GCM\Message\PayloadData; use alxmsl\Google\GCM\Message\PayloadMessage; // Create new payload class final class NewPayloadData extends PayloadData { protected function getDataFields() { return array('test' => 'test_01'); } } // Create payload instance $Data = new NewPayloadData(); // Create and initialize message instance $Message = new PayloadMessage(); $Message->setRegistrationIds('DeV1CeT0kEN')->setType(PayloadMessage::TYPE_JSON)->setData($Data); // Create GCM client $Client = new Client(); $Client->getRequest()->setConnectTimeout(60); $Client->setAuthorizationKey('aUTH0R1Z4t1oNKEy'); // ...and send the message $Response = $Client->send($Message); var_dump($Response);
public function testPlainTypeForManyRegistrationIds() { $Message1 = new PayloadMessage(); $Message1->setRegistrationIds('Reg15TRaTi0N_1d1'); $Message1->setType(PayloadMessage::TYPE_PLAIN); $this->assertEquals('Reg15TRaTi0N_1d1', $Message1->getRegistrationIds()); $Message2 = new PayloadMessage(); $Message2->setType(PayloadMessage::TYPE_JSON); $Message2->setRegistrationIds(['Reg15TRaTi0N_1d1']); $this->assertEquals(['Reg15TRaTi0N_1d1'], $Message2->getRegistrationIds()); $Message2->setType(PayloadMessage::TYPE_PLAIN); $this->assertEquals('Reg15TRaTi0N_1d1', $Message2->getRegistrationIds()); $Message3 = new PayloadMessage(); $Message3->setType(PayloadMessage::TYPE_JSON); $Message3->setRegistrationIds(['Reg15TRaTi0N_1d1', 'Reg15TRaTi0N_1d2']); $this->assertEquals(['Reg15TRaTi0N_1d1', 'Reg15TRaTi0N_1d2'], $Message3->getRegistrationIds()); try { $Message3->setType(PayloadMessage::TYPE_PLAIN); $this->fail(); } catch (GCMRegistrationIdsIncorrectForMessageType $Ex) { } }