GCM aperitivo Android

1,512 views

Published on

Introduzione alle notifiche push del sistema Android (Google Cloud Messaging).
GDG Perugia

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,512
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

GCM aperitivo Android

  1. 1. Google Cloud Messaging for Android Luca Morettoni <luca@morettoni.net>
  2. 2. Google Cloud MessagingAggiornare i dati delle APP:● Polling;● Notifiche PUSH.Vantaggi delle notifiche PUSH:● Minor consumo di risorse (batteria/banda);● Aggiornamenti (quasi) real-time;● Lapplicazione non deve essere attiva.
  3. 3. Google Cloud MessagingLimiti/richieste di GCM:● Android 2.2 o superiore;● Google Play Store installato;● Lutente deve avere almeno un profilo Google configurato (per versioni < 4.0.4);● Payload dal server alle app limitato (~4K).
  4. 4. Google Cloud MessagingRegistrazione dei device 1 2 3
  5. 5. Google Cloud MessagingInvio di notifiche 1 2 3
  6. 6. Google Cloud MessagingPrima di cominciare: https://code.google.com/apis/console 1. Prendete nota dellID del progetto (nella URL); 2. Attivate il servizio Google Cloud Messaging; 3. Create una nuova chiave per il server.
  7. 7. Google Cloud MessagingAndroid 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!
  8. 8. Google Cloud MessagingAndroidManifest.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" />
  9. 9. Google Cloud MessagingRegistrazioneGCMRegistrar.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)) ... }}
  10. 10. Google Cloud MessagingGCMIntentServicepublic 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) {}}
  11. 11. Google Cloud MessagingServer side1. Effettuare un POST al server GCM;2. Eliminare/aggiornare i device ID.
  12. 12. Google Cloud MessagingServer side (POST)https://android.googleapis.com/gcm/sendheader:Authorization: key= CHIAVE_SERVERContent-Type: application/jsonbody:{ registration_ids: [device_1, device2, ..., device1000], collapse_key: key, data: { payload_data: ABC123... }}
  13. 13. Google Cloud MessagingAltri campi "interessanti":"delay_while_idle": true| false"time_to_live": seconds (default 4 settimane)"dry_run": true| false
  14. 14. Google Cloud MessagingServer 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"} ]}
  15. 15. Google Cloud Messagingalcune 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

×