Zend_Mobile_Push_Gcm - Zend_Mobile_Push


Zend_Mobile_Push_Gcm provides the ability to send push notifications to Android devices that contain Google Services. A message will always be constructed with Zend_Mobile_Push_Message_Gcm.

Pushing Messages

Note: Prior to pushing and receiving messages; you will need to create a Google API Project and setup your Android app to listen to GCM messages.. If you have not done this, follow the » GCM: Getting Started document.

When implementing GCM; you have a few components that you will utilize. Zend_Mobile_Push_Gcm which contains the server components, Zend_Mobile_Push_Message_Gcm which contains the message that you would like to send, and Zend_Mobile_Push_Response_Gcm which contains the response from GCM. Each message sent must do an HTTP request; so remember this when sending in large batches.

The actual implementation of the code is fairly minimal; however, considerations to error handling must be taken.

  1. $message = new Zend_Mobile_Push_Message_Gcm();
  2. $message->addToken('ABCDEF0123456789');
  3. $message->setData(array(
  4.     'foo' => 'bar',
  5.     'bar' => 'foo',
  6. ));
  8. $gcm = new Zend_Mobile_Push_Gcm();
  9. $gcm->setApiKey('YOUR_API_KEY');
  11. try {
  12.     $response = $gcm->send($message);
  13. } catch (Zend_Mobile_Push_Exception $e) {
  14.     // exceptions require action or implementation of exponential backoff.
  15.     die($e->getMessage());
  16. }
  18. // handle all errors and registration_id's
  19. foreach ($response->getResults() as $k => $v) {
  20.     if ($v['registration_id']) {
  21.         printf("%s has a new registration id of: %s\r\n", $k, $v['registration_id']);
  22.     }
  23.     if ($v['error']) {
  24.         printf("%s had an error of: %s\r\n", $k, $v['error']);
  25.     }
  26.     if ($v['message_id']) {
  27.         printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']);
  28.     }
  29. }
Exceptions and Remediation Techniques
Exception Meaning Handling
Zend_Mobile_Push_Exception These types of exceptions are more generic in nature and are thrown either from gcm when there was an unknown exception or internally on input validation. Read the message and determine remediation steps.
Zend_Mobile_Push_Exception_InvalidAuthToken Your API key was likely typed in wrong or does not have rights to the GCM service. Check your project on the » Google APIs Console page.
Zend_Mobile_Push_Exception_ServerUnavailable This exception means there was either an internal server error OR that the server denied your request and you should look at the Retry-After header. Read the exception message and utilize Exponential Backoff
Zend_Mobile_Push_Exception_InvalidPayload Generally the payload will not throw an exception unless the size of the payload is too large or the JSON is too large. Check the size of the payload is within the requirements of GCM, currently it is 4K.

Advanced Messages

GCM provides the ability for sending more advanced messages; for instance the examples above show the most basic implementation of a message. Zend_Mobile_Push_Message_Gcm allows you to do far more advanced messaging outlined below.

Delay While Idle

If included, indicates that the message should not be sent immediately if the device is idle. The server will wait for the device to become active, and then only the last message for each collapse_key value will be sent.

  1. $message = new Zend_Mobile_Push_Message_Gcm();
  2.     $message->setDelayWhileIdle(true);

Time to Live

You may set the time to live in seconds, by default GCM will save it for 4 weeks. Additionally if you specify a Time to Live, you must also set an ID (the collapse key). Generally it is best by using the message data so that you know it is a unique message.

  1. $message = new Zend_Mobile_Push_Message_Gcm();
  2.     $message->setTtl(86400);
  3.     $message->addData('key', 'value');
  4.     $message->setId(md5(json_encode($message->getData())));


GCM gives a response back that contains detail that needs to be understood. Most of the time you can just send a message but the server may come back telling you any the message id, any errors and potentially new registration ids.


The results are most commonly retrieved by calling the getResults() method. However, you may prefer to just get certain results by using the getResult() method. The getResult method utilizes the constants RESULT_* correlating to message id, error or registration id.

Several utility methods exist to give you a better idea of what happened during your send. Methods getSuccessCount(), getFailureCount() and getCanonicalCount() let you know how many where successful, how many failures and how many updates to registration ids you have.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.