Android GCM e PHP

Primeiro de tudo você precisa ir para https://code.google.com/apis/console/ e obter o “Número do projeto” e a “chave API”. Em seguida, inclua “gcm.jar” em seu projeto, você o encontrará em “your-android-sdk-folder / extras / google / gcm / gcm-client / dist /”.

Permissões de manifesto:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.yourpackage.gcmtest.permission.C2D_MESSAGE" />

Destinatário do Manifesto:

<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.yourpackage.gcmtest" />
</intent-filter>
</receiver>

Serviço de manifesto:

<service android:name=".GCMIntentService" />

Código GCMIntentService:

/**
* {
@link IntentService} responsible for handling GCM messages.
*/

public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super("123ProjectNumber123");
Log.d("GCM", "GCMIntentService");
}
@Override
protected void onError(Context arg0, String arg1) {
Log.d("GCM", "onError: "+arg1);
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
// Here you get the notification data
Log.d("GCM", "onMessage: "+arg1.getStringExtra("message"));
}
@Override
protected void onRegistered(Context arg0, String arg1) {
Log.d("GCM", "onRegistered: "+arg1);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Log.d("GCM", "onUnregistered: "+arg1);
}
}

Este código registra o ID do dispositivo:

// GCMRegistrar
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.d("GCM", "GCMRegistrar isRegistered");
}
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "522646XXXXXX");
regId
= GCMRegistrar.getRegistrationId(this);
Log.d("GCM", "Register: " + regId);
} else {
Log.d("GCM", "Already registered as: " + regId);
}
txt1
.setText(regId);

CÓDIGO DE PHP DO LADO DO SERVIDOR:

<?php
$message
= "Hi, I am a GCM message! [".date("Y-m-d H:i:s")."]";
$pushCounter
=0;
$registatoin_ids
=array();
// The device Registration ID (regId)
$registatoin_ids
[]="65d4f6ds4f6ds4f6fa546s5df46asd5f46asdf54a6sdf54a6sdXXXXXXXXXX";
$pushCounter
++;
if($pushCounter>0){
$gcm
=new GCM();
$message
=array( "message"=> $message );
$result_android
=$gcm->send_notification($registatoin_ids,$message);
echo $result_android
;
}
class GCM{
function __construct(){}
public function send_notification($registatoin_ids,$message){
// GOOGLE API KEY
define
("GOOGLE_API_KEY","AISfldleuyvtv438v7tvXXXXXX");
$url
="https://android.googleapis.com/gcm/send";
$fields
=array(
"registration_ids"=>$registatoin_ids,
"data"=>$message
);
var_dump
($fields);
$headers
=array(
"Authorization: key=".GOOGLE_API_KEY,
"Content-Type: application/json"
);
$ch
=curl_init();
curl_setopt
($ch,CURLOPT_URL,$url);
curl_setopt
($ch,CURLOPT_POST,true);
curl_setopt
($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt
($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt
($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt
($ch,CURLOPT_POSTFIELDS,json_encode($fields));
$result
=curl_exec($ch);
if($result===FALSE){
die("Curl failed: ".curl_error($ch));
}
curl_close
($ch);
echo $result
;
}
}
?>