Upload
morettoni
View
1.769
Download
0
Embed Size (px)
DESCRIPTION
Introduzione alle notifiche push del sistema Android (Google Cloud Messaging). GDG Perugia
Citation preview
Google Cloud Messaging for Android
Luca Morettoni <[email protected]>
Aggiornare i dati delle APP:
● Polling;● Notifiche PUSH.
Vantaggi delle notifiche PUSH:
● Minor consumo di risorse (batteria/banda);● Aggiornamenti (quasi) real-time;● L'applicazione non deve essere attiva.
Google Cloud Messaging
Limiti/richieste di GCM:
● Android 2.2 o superiore;● Google Play Store installato;● L'utente deve avere almeno un profilo
Google configurato (per versioni < 4.0.4);● Payload dal server alle app limitato (~4K).
Google Cloud Messaging
Registrazione dei device
Google Cloud Messaging
1
2
3
Invio di notifiche
Google Cloud Messaging
1
2
3
Google Cloud Messaging
Prima di cominciare:https://code.google.com/apis/console
1. Prendete nota dell'ID del progetto (nella URL);2. Attivate il servizio Google Cloud Messaging;3. Create una nuova chiave per il server.
Google Cloud Messaging
Android side:
1. Registrare il device sui server GCM;2. Ottenere il device ID;3. Inviare il device ID al proprio server;4. Attendere le notifiche!
Google Cloud Messaging
AndroidManifest.XML
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
<permission android:name="net.morettoni.terremoto.permission.C2D_MESSAGE" android:protectionLevel="signature" /><uses-permission android:name="net.morettoni.terremoto.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /><uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /><uses-permission android:name="android.permission.WAKE_LOCK" />
<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="net.morettoni.terremoto" /> </intent-filter></receiver>
<service android:name=".GCMIntentService" />
Google Cloud Messaging
Registrazione
GCMRegistrar.checkDevice(this);GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);if (regId.equals("")) { GCMRegistrar.register(this, ID_PROGETTO);} else {
// controlliamo se siamo registrati sul nostro server
if (!GCMRegistrar.isRegisteredOnServer(this))
...
}
}
Google Cloud Messaging
GCMIntentService
public class GCMIntentService extends GCMBaseIntentService {
protected void onError(Context context, String errorId) {}
protected void onMessage(Context context, Intent intent) {}
protected void onRegistered(Context context, String registrationId) {}
protected void onUnregistered(Context context, String registrationId) {}
}
Google Cloud Messaging
Server side
1. Effettuare un POST al server GCM;2. Eliminare/aggiornare i device ID.
Google Cloud Messaging
Server side (POST)
https://android.googleapis.com/gcm/send
header:Authorization: key= CHIAVE_SERVERContent-Type: application/json
body:{
registration_ids: ['device_1', 'device2', ..., 'device1000'],
collapse_key: 'key',
data: { 'payload_data': 'ABC123...' }
}
Google Cloud Messaging
Altri campi "interessanti":
"delay_while_idle": true| false
"time_to_live": seconds (default 4 settimane)
"dry_run": true|false
Google Cloud Messaging
Server side (RESPOSE){
"multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}
Google Cloud Messaging
alcune risorse:
● http://developer.android.com/google/gcm/index.html● https://groups.google.com/forum/?fromgroups#!
forum/android-gcm● <SDK_ROOT>/extras/google/gcm/samples● https://plus.google.com/u/0/100229757360577322667/
http://bit.ly/morettoni_plus