2014년 11월 6일 목요일

Sending Android Push Notifications from PHP to PhoneGap Apps

For the last few days I’ve been working within a Phonegap project for Android devices using Push Notifications. The idea is simple. We need to use the Push Notification Plugin for Android. First we need to register the Google Cloud Messaging for Android service at Google’s console, and then we can send Push notifications to our Android device.
The Push Notification plugin provides a simple example to send notifications using Ruby. Normally my backend is built with PHP (and sometimes Python) so instead of using the ruby script we are going to build a simple PHP script to send Push Notifications.
The script is very simple:
01.<?php
02.$apiKey "myApiKey";
03.$regId "device reg ID";
04. 
05.$pusher new AndroidPusher\Pusher($apiKey);
06.$pusher->notify($regId"Hola");
07. 
08.print_r($pusher->getOutputAsArray());
09.$app->run();
And the whole library you can see here:
01.<?php
02.namespace AndroidPusher;
03. 
04.class Pusher
05.{
06.const GOOGLE_GCM_URL = 'https://android.googleapis.com/gcm/send';
07. 
08.private $apiKey;
09.private $proxy;
10.private $output;
11. 
12.public function __construct($apiKey$proxy = null)
13.{
14.$this->apiKey = $apiKey;
15.$this->proxy  = $proxy;
16.}
17. 
18./**
19.* @param string|array $regIds
20.* @param string $data
21.* @throws \Exception
22.*/
23.public function notify($regIds$data)
24.{
25.$ch = curl_init();
26.curl_setopt($ch, CURLOPT_URL, self::GOOGLE_GCM_URL);
27.if (!is_null($this->proxy)) {
28.curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
29.}
30.curl_setopt($ch, CURLOPT_POST, true);
31.curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeaders());
32.curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
33.curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
34.curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getPostFields($regIds$data));
35. 
36.$result = curl_exec($ch);
37.if ($result === false) {
38.throw new \Exception(curl_error($ch));
39.}
40. 
41.curl_close($ch);
42. 
43.$this->output = $result;
44.}
45. 
46./**
47.* @return array
48.*/
49.public function getOutputAsArray()
50.{
51.return json_decode($this->output, true);
52.}
53. 
54./**
55.* @return object
56.*/
57.public function getOutputAsObject()
58.{
59.return json_decode($this->output);
60.}
61. 
62.private function getHeaders()
63.{
64.return [
65.'Authorization: key=' $this->apiKey,
66.'Content-Type: application/json'
67.];
68.}
69. 
70.private function getPostFields($regIds$data)
71.{
72.$fields = [
73.'registration_ids' => is_string($regIds) ? [$regIds] : $regIds,
74.'data'             => is_string($data) ? ['message' => $data] : $data,
75.];
76. 
77.return json_encode($fields, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
78.}
79.}
Maybe we could improve the library with a parser of google’s ouptuput, basically because we need to handle this output to notice if the user has uninstalled the app (and we need the remove his reg-id from our database), but at least now it cover all my needs. You can see the code at github

댓글 없음:

댓글 쓰기