📜 ⬆️ ⬇️

Google Cloud Messaging: Library for Codeigniter and a couple of differences between Google Cloud Messaging and Apple Push Notifications

Good afternoon friends,

Questman just recently wrote an article about Google’s new old Google service called Google Cloud Messaging and gave an example of an application for android using it.
From my bell tower, I would like to continue the topic from the server-side point of view with an eye to the experience of using Apple Push Notifications.


')
The new service is called as already mentioned by Google Cloud Messaging (people faced with this before 06/27/12 better known as Cloud To Device Messaging (c2dm) Service).

One of the first noticeable differences is the sender script authentication method. GCM accepts API key authentication, while C2DM requires ClientLogin or OAuth.

You can get the API Key in the console - code.google.com/apis/console :
  1. Create a project
  2. Go to Services and select "Google Cloud Messaging for Android"
  3. Go to the access API and create a new Server Key


How to write itself android application described questman , and you can also search for information in the dock - developer.android.com/guide/google/gcm/gs.html

Further, as in the case of APNs, to send a notification to an android device you need to know its registration id (which plays about the same role as the device token in an iOS application). This should concern the application developer.

Unlike APNs, Google Cloud Messaging does not use certificates.
The address for sending notifications is only one for development and production: android.googleapis.com/gcm/send
Also, the key API does not have a separation between the development and production versions. But in the key settings, the allowed ip addresses for sending notifications are strictly specified.

After you receive the registration id and API Key on the server, you can safely start sending notifications to the device.

Again, remembering APNs, I want to say Google thanks for not having to bother with binary data. The data is transmitted by the POST method in plain text format, in JSON format or plain text (this is not exactly the same with simple and enhanced formats in APN; in the case of GCM, the service will return a detailed response in any case). I certainly recommend using JSON and I use it myself. Moreover, sending notifications to several recipients can be realized only with its help.

To send messages to several users, it is enough to list their registrations ids. The message is written only once.

As with APNs, additional data can be sent to GCM. Data size is limited only by the size of the entire notification, which can not be more than 4096 bytes.

Well, the last and the most pleasant - there are clear and complete answers to the service. GCM in this case is much more talkative colleagues from Apple. Error sending can be in the http-headers, and specifically applicable to each message / device individually.

All of them are described in detail in the developer.android.com/guide/google/gcm/gcm.html dock

And lastly I would like to demonstrate the work of a small library under Codeigniter (in principle, it is not particularly tied to CI itself, so migrating it to a separate php class is not difficult), based on the c2dm package code, © 2011 lytsing.org & 2012 thebub. net changed under GCM:

//  public function send_gcm() { //  // API key    $this->load->library('gcm'); // .        setData(), //        setMessage,   setData(), //    setMessage()     $this->gcm->setMessage('Test message '.date('dmY H:s:i')); //     //           4 $this->gcm->addRecepient('RegistrationId'); $this->gcm->addRecepient('New reg id'); //      $this->gcm->setData(array( 'some_key' => 'some_val' )); //  TTL   : http://developer.android.com/guide/google/gcm/adv.html#lifetime //  TTL $this->gcm->setTtl(500); //  TTL     $this->gcm->setTtl(false); //    $this->gcm->setGroup('Test'); //      $this->gcm->setGroup(false); //  if ($this->gcm->send()) echo '   '; else echo '      '; //       print_r($this->gcm->status); //     print_r($this->gcm->messagesStatuses); die(' .'); } 


The library itself can traditionally be found on github'e - codeigniter-gcm

Thank you all for your attention. Rotten tomatoes are welcome at least with some explanation)

Source: https://habr.com/ru/post/147592/


All Articles