gcm presentation 2.0 - university of iowahomepage.divms.uiowa.edu/~ochipara/classes/sensing... ·...

Post on 31-May-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

INTRODUCTION  Google  Cloud  Messaging  (GCM)  

10/30/12  

Footprint  • A  loca8on  based  socializa8on  App.  • Message  Management  System.  

• Messages  Detec*on  • New  messages.  • New  comments.  

10/30/12  

PULL  

Footprint  Server   Footprint  APP  on  mobile  device  

H:pResponse  [Messages]    

HDpPost  [Update]  

§  Efficiency  problems  §  Unnecessary  BaDery  usage  §  Unnecessary  data  usage  

   

10/30/12  

Push  

•  The  system  can  • Wake  up  the  App  when  the  message  arrives  •  Store  the  message  in  case  the  device  is  offline.  •  Mul8cas8ng  

Footprint  Server   Footprint  APP  on  mobile  device  

Messages    

10/30/12  

C2DM  &  GCM  

• A  Google  Service  that  helps  developers  send  data  from  servers  to  their  Android  applica8ons  on  Android  devices.  

• Handle  all  aspects  of  queuing  of  messages  and  delivery  to  the  target  Android  applica8on  running  on  the  target  device.  

C2DM  has  been  officially  deprecated  and  replaced  by  GCM  

10/30/12  

Footprint  Server   Footprint  APP  on  mobile  device  

Google  Server  

Offline  Messages    Messages    

10/30/12  

Advantages  of  GCM  (comparing  with  C2DM)  •  Simple  •  No  client  login  token  •  No  sign  up  form  •  No  quotas  

•  Powerful  • Mul*cas*ng  •  Expiring  messages  • Messages  with  payload  –  4kb  

•  Fast  •  4.7  milliseconds  to  send  a  message  

10/30/12  

ARCHITECTURAL  OVERVIEW  

10/30/12  

Architectural  Overview  •  Components        The  physical  en88es  that  play  a  role  in  GCM.    •  Creden*als  The  IDs  and  tokens  that  are  used  in  different  stages  of  GCM  to  ensure  that  all  par8es  have  been  authen8cated,  and  that  the  message  is  going  to  the  correct  place.    •  Lifecycle  Flow  The  primary  processes  involved  in  cloud-­‐to-­‐device  messaging.  

10/30/12  

Components  

GCM  Server  

Mobile  Device  

Footprint  Server  

10/30/12  

Credentials  •  Sender  ID    •  Project  ID  instead  of  email  

•  Applica8on  ID    •  Package  name  •  Iden8fy  Android  applica8on  

•  Registra8on  ID    •  Tied  to  a  par8cular  APP  on  device    •  Permission  to  receive  massages  

•  User  Account    •  Not  required  for  Android  4.0.4  or  higher  

•  Sender  Auth  Token    •  An  API  key  instead  of  a  ClientLogin  Auth  token  •  Included  in  the  header  of  POST  requests  

10/30/12  

Lifecycle  Flow  

•  Enabling  GCM.    •  An  Android  applica8on  running  on  a  mobile  device  registers  to  receive  messages.  

•  Sending  a  message.    •  A  3rd-­‐party  applica8on  server  sends  messages  to  the  device.  

•  Receiving  a  message.    •  An  Android  applica8on  receives  a  message  from  a  GCM  server  

10/30/12  

GCM  –  Registration      •  Ini8ally,  Applica8on  request  to  GCM  server  that  it  wants  to  use  GCM.  In  response,  GCM  assign  a  Reg-­‐id  to  device.  

 

Server   GCM  

Applica8on  

Reg.  Request  

Reg-­‐id  Reg-­‐id  

Reg-­‐id  /  Response  

Response  

10/30/12  

GCM  -­‐  Unregistration  •  At  first,  our  Server  doesn’t  know  that  Applica8on  has  been  uninstalled.  

Server   GCM  

Applica8on  

Update  /  Response  

Update  /  Response  

Invalidate  this  address  

Update  /  Response  Device  not  registered  

Get  rid  of  Reg-­‐id    from  Database  GCM  Framework  on  device  iden8fies  and  no8fy  GCM  Server  that  

applica8on  has  been  uninstall.    

10/30/12  

Implementation  •  Go  to  Google  APIs  console  page  (hDps://code.google.com/apis/console)  and  create  API  project.   10

/30/12  

Implementation  –  Cont.  

10/30/12  

Library  Installation  •  In  Android  SDK  Manager  ,  Extras  -­‐>  Google  Cloud  Messaging  for  Android.  

10/30/12  

Code  -­‐  Register  •  Applica8on  have  to  send  Intent  to  GCM  Framework  on  device.    

•  GCM  Framework  will  communicate  with  GCM  Server.  

GCM  

Applica8on  

GCM  Framework  

Intent  

10/30/12  

Code  –  Send  Intent  import com.google.android.gcm.GCMRegistrar @Override Protected void onCreate(Buldle savedInstanceState){

if(GCMRegistrar.getRegistrationId(this).equals(“ ”)) { GCMRegistrar.register(this, “98067174480”);    

Project-­‐ID  

10/30/12  

Code  –  Listen  Intent  •  GCM  Server  will  create  Reg-­‐id  for  applica8on  and  returns  to  GCM  Framework  ,  which  will  send  Intent  to  applica8on.  

•  Applica8on  has  to  Listen  this  Intent.  

GCM  

Applica8on  

GCM  Framework  

Reg-­‐id  

Intent  

10/30/12  

Code  –  Listen  Intent  –  cont.  import com.google.android.gcm.GCMBaseIntentService public class GCMIntentService extends

GCMBaseIntentService { @override protected void onRegistered (Context ctx , String regId)

{ sendToAppServer(regId); }

@override protected void onMessage(Context ctx, Intent intent)

{ final Bundle bundle = intent.getExtras(); yourfunction(bundle); }

}

10/30/12  

Permissions    In  Manifest  file  :-­‐        < uses-permission android:name=“com.google.android.c2dm.permission.RECEIVE >

10/30/12  

•  Able  to  communicate  with  your  client.  •  Able  to  fire  off  HTTPS  requests  to  the  GCM  server.  •  Able  to  handle  requests  and  resend  then  as  needed,  using  exponen8al  back-­‐off.  

•  Able  to  store  the  API  key  and  client  registra8on  IDs.    

Role  of  the  Server  

10/30/12  

Sending  Messages  

Server   Auth  

App  

Reg  ID,  API  Key  

API  Key  

Project  ID  

10/30/12  

An  Example  JSON  request  

Content-Type:application/json Authorization: key= AIzaSyB- 1uEai2WiUapxCs2Q0GZYzPu7Udno5aA { "registration_ids":"APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."], "data" : { ... }, }

If  Content-­‐Type  is  omi:ed,  the  format  is  assumed  to  be  plain  text  

10/30/12  

Can  we  send  a  single  message  to  multiple  devices?  

Yes!  GCM  supports  message  mul*cas*ng  

10/30/12  

•  A  3rd-­‐party  applica8on  server  can  either  send  messages  to  a  single  device  or  to  mul8ple  devices.    •  You  have  2  choices  in  how  you  construct  requests  and  responses:  plain  text  or  JSON.  •  A  message  sent  to  mul8ple  devices  simultaneously  is  called  a  mul$cast  message  •  However,  to  send  mul8cast  messages,  you  must  use  JSON.  Plain  text  will  not  work.  

 

Sending  Messages  

10/30/12  

Multicasting  

Server  

App   App   App  

10/30/12  

Example  request  (JSON)  

{ "collapse_key": "score_update", "time_to_live": 108, "delay_while_idle": true, "data": { ... }, "registration_ids":["4","8","15","16","23","42"] }

Up  to  1000  messages  at  a  *me!  

10/30/12  

Sample  Response  (JSON)  { "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"} ] }

10/30/12  

•  First  message:  success,  not  required.  •  Second  message:  should  be  resent  (to  registra8on  ID  8).  •  Third  message:  had  an  unrecoverable  error  (maybe  the  value  got  corrupted  in  the  database).  •  Fourth  message:  success,  nothing  required.  •  Fi\h  message:  success,  but  the  registra8on  ID  should  be  updated  in  the  server  database  (from  23  to  32).  •  Sixth  message:  registra8on  ID  (42)  should  be  removed  from  the  server  database  because  the  applica8on  was  uninstalled  from  the  device.  

In  this  Example:  

10/30/12  

Can  our  app  receive  message  from  multiple  senders?  

Yes  again!  

10/30/12  

Multiple  Senders  

Server  

App  

10/30/12  

How  to  do  it?  import com.google.android.gcm.GCMRegistrar; @override protected void onCreate(Bundle savedInstanceState){ GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { GCMRegistrar.register(this, "10", "12" ); }

Project  IDs  

10/30/12  

How  to  do  it?  import com.google.android.gcm.GCMRegistrar; Public class GCMIntentService extends GCMBaseIntentService{ @override protected void onRegistered(Context ctx,String redId){

sendToFootPrint(regId); sendToSocialUpdateServer(redId);

}

Multiple  servers  can  send  message  to  one  application  

Up  to  100  projects!  

One  common  registration  id  

10/30/12  

What  if  the  message  expires  before  it  is  delivered?  

GCM  supports  TTL  (Time  to  leave)  

10/30/12  

Time  To  Leave  

Server  

App  

TTL   10/30/12  

Example  request  (JSON)  

{ "collapse_key": "score_update", "time_to_live": 108, "delay_while_idle" : true, "data": { ... }, "registration_ids":["4","8","15","16","23","42"] }

10/30/12  

Time  To  Leave  

•  TTL  value  from  0  second  to  4  weeks.  • Messages  with  TTL  value  0  are  not  stored  by  GCM  • Default  TTL  value  is  4  weeks   10

/30/12  

Can  we  send  Instant  Messages  using  GCM?  

GCM  supports  message  with  payload  data  

10/30/12  

• GCM  supports  messages  contains  payload  data  up  to  4KB  •  The  GCM  service  handles  all  aspects  of  queuing  of  messages  

Messages  with  payload  

10/30/12  

Messages  with  payload  

Server  

App  

10/30/12  

Example  request  (JSON)  

{ "registration_ids":"APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."], "data" : { ... }, }

Messages  with  payload  are  non-­‐collapsible  messages  

10/30/12  

Messages  with  payload  

Server  

App  

10/30/12  

Messages  with  payload  

Server  

App  

4KB  

10/30/12  

Messages  with  payload  

Server  

App  

32KB  

10/30/12  

Messages  with  payload  

Server  

App  

256KB  

10/30/12  

Messages  with  payload  

Server  

App  

512KB  

Stores  up  to  100  messages  if  the  device  is  offline  

10/30/12  

JSON  message  and  helper  function  { "message_type":"deleted_message", "total_deleted" :"115“, },

import com.google.android.gcm.GCMRegistrar; Public class GCMIntentService extends GCMBaseIntentService{ @override protected void onDeletedMessages (Context ctx,int total){

fullySyncWithServer(total); }

10/30/12  

GCM  Reliability  &  Optimization  

10/30/12  

GCM  Reliability  

App  

10/30/12  

GCM  Reliability  

App  RMQ  

10/30/12  

• Provide  fresh  data  all  the  8me  • Op8mized  for  baDery  life  

GCM  Optimization  

10/30/12  

GCM  Optimization  

Server  

App  

Thro:le  Queue  

10/30/12  

Communication  in  our  Project

•  Our  app  is  a  loca8on  based  and  real-­‐8me  socializa8on  applica8on.  

•  Quality:  •  Real-­‐8me  data  update  •  Detect  new  messages  in  movements  •  Energy  saving  

•  Solu8on:  •  Combine  HTTP  and  GCM  together  

 

10/30/12  

Cases: •  Login:  

User

GCM

Server

Upload  current  loca*on

Messages  

10/30/12  

Cases: •  Movement:  

User

GCM

Server

Upload  current  loca*on

Messages  

10/30/12  

Cases: •  Create  a  new  message:  •  Create  a  new  comment:  

User1

GCM

Server Upload  the  message

Calculate  the  receivers

User2 Fetch

Message

10/30/12  

Cases: •  Off-­‐Line  

User1

GCM

Server

10/30/12  

Energy  Saving

• Use  “Push”  to  reduce  the  frequency  of  asking  server  • Only  moving  and  logging  will  cause  loca8on  update  • Use  the  power  of  server  to  do  the  majority  of  calcula8on

10/30/12  

References  

•  hDp://developer.android.com/guide/google/gcm/gcm.html  

•  hDps://developers.google.com/android/c2dm/  

•  Google  I/O  2012  -­‐  Google  Cloud  Messaging  for  Android  hDp://www.youtube.com/watch?v=YoaP6hcDctM&feature=plcp    

10/30/12  

10/30/12  

top related