您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > android-push-applications-android
最棒的Android开发社区ViewlivenotesandaskquestionsaboutthissessiononGoogleWave最棒的Android开发社区4Outline• AccessingDataintheCloud• PollingandPushing• AndroidCloudtoDeviceMessaging• Demos• Summary• Appsprovideseamlessaccesstodatainthecloud– MobileAlerts– SendtoPhone– BackgroundSync• Challenge:Howdoyoukeepdataonadevicefresh?• Simpletoimplement• Deviceperiodicallyasksserverfornewdata– Radiodrawsalotofpower,staysonforseveralseconds– Ideally,useIf-Modified-Since,If-None-Match,etc.• Makeno-opsascheapaspossible• Appropriateforcontentthatchangesconstantly– StockQuotes,NewsHeadlines– Pollinfrequently,updateondemand• Baseline:~5-8mA• Network:~180-200mA– TxmoreexpensivethanRx• Radiostaysonforfewsecs• ~0.50mAhforashortpoll– 5mfrequency:~144mAh/day– 15mfrequency:~48mAh/day• Tradeoffbetweenfreshnessandefficiency– Pollfrequently–morefresh,lessefficient• Desire:Push,don’tpoll– Onlyfetchdatawhenuseful• Enablesfreshnesswithlessimpactonbattery– Onlyusenetworkwhennecessary– Constantoverheadofpersistentconnection• GoogleContacts,Calendar,Gmail,etc.,usepushsync• Canbetrickytoimplement• AndroidCloudtoDeviceMessagingmakespusheasy• SimpleGoogleAPI– Android2.2deviceswithMarket– Willbeopentoalldevelopers• UsesexistingconnectionforGoogleservices• Allowsserverstosendlightweight“data”messagestoapps– Tellappnewdataavailable– Intentbroadcastwakesupapp– AppsuppliesUI,e.g.,Notification,if/asnecessary• Besteffortdelivery• Backgroundservice– Honorbackgrounddatasetting– Startwhennetworkavailable• Maintainconnectionwithserver– Useheartbeatstokeepalive,detectdeadconnections• Efficient– Minimizeperconnectoverhead– Minimizeheartbeatfrequency– Minimizeconcurrentconnections✓• UseAlarms• (Re)schedulepings• Waitforacks• Reconnectwhendead• Canalsoinitiateping• Maybehalfopen• Cleanupstatewhendead• Enablingcloudtodevicemessaging– App(ondevice)registerswithGoogle,getsregistrationID– AppsendsregistrationIDtoitsAppServer• Permessage– AppServersends(authenticated)messagetoGoogle– Googlesendsmessagetodevice• Disablingcloudtodevicemessaging– AppcanunregisterID,e.g.,whenusernolongerwantspush!–RequestingaRegistrationID//UsetheIntentAPItogetaregistrationID//RegistrationIDiscompartmentalizedperapp/deviceIntentregIntent=newIntent(“com.google.android.c2dm.intent.REGISTER”);//IdentifyyourappregIntent.putExtra(“app”,PendingIntent.getBroadcast(this,0,newIntent(),0);//IdentifyroleaccountserverwillusetosendregIntent.putExtra(“sender”,emailOfSender);//StarttheregistrationprocessstartService(regIntent);–ReceivingtheRegistrationID• AppreceivestheIDasanIntent– com.google.android.c2dm.intent.REGISTRATION• AppshouldsendthisIDtoitsserver• ServicemayissuenewregistrationIDatanytime– AppwillreceiveREGISTRATIONIntentbroadcast– AppmustupdateserverwithnewID–ReceivingtheRegistrationID//RegistrationIDreceivedviaanIntentpublicvoidonReceive(Contextcontext,Intentintent){Stringaction=intent.getAction();if(“…REGISTRATION”.equals(action)){handleRegistration(context,intent);}}privatevoidhandleRegistration(Contextcontext,Intentintent){Stringid=intent.getExtra(“registration_id”);if((intent.getExtra(“error”)!=null){//Registrationfailed.Tryagainlater,withbackoff.}elseif(id!=null){//SendtheregistrationIDtotheapp’sserver.//Besuretodothisinaseparatethread.}}• Get“ac2dm”authtoken,installonserver– • SendauthenticatedPOST– – Authorization:GoogleLoginauth=authtoken– URLEncodedparameters• registration_id• collapse_key• (optional)delay_while_idle• (optional)data.key*–ResponseCodes• 200OK– With“id”–requestsucceeded,messageenqueued– With“Error”–requestfailed• QuotaExceeded,DeviceQuotaExceeded:Retryafterawhile• InvalidRegistration,NotRegistered:Stopsendingmessages• MessageTooBig:Reducesizeofmessage• MissingCollapseKey:Includecollapsekeyinrequest• 401NotAuthorized:Getnewauthtoken• 503ServiceUnavailable:Retrywithbackoff• Devicereceivesmessage,convertstoIntent• Appwokenup/startedbyIntentbroadcast– com.
本文标题:android-push-applications-android
链接地址:https://www.777doc.com/doc-6219750 .html