Notifications and Services

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2010 Android UI, and Networking Mobile Computing Based on android-sdk_2.2 Unless otherwise stated, images are from.
Advertisements

Staying in Sync with Cloud 2 Device Messaging. About Me Chris Risner Twitter: chrisrisner.
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
CSE2102 Introduction to Software Engineering Lab 2 Sept/4/2013.
Getting Started with Android Development Rohit Ghatol.
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
DUE Hello World on the Android Platform.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
FCM Workflow using GCM.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Android Programming Steve Ko Computer Sciences and Engineering University at Buffalo.
Lecture 2: Android Concepts
Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Cosc 4735 Nougat API 24+ additions.
Randy Dalrymple HillyRoad, LLC
Concurrency in Android
Small talk with the UI thread
Lecture 2: Android Concepts
Mobile Applications (Android Programming)
Tracking device movements
Android Application Development 1 6 May 2018
Remote execution of long-running CGIs
SQLite in Android Landon Cox March 2, 2017.
CS371m - Mobile Computing Services and Broadcast Receivers
Basic Activities and Intents
Instructor: Mazhar Hussain
Android Runtime – Dalvik VM
Section 13 - Integrating with Third Party Tools
Scheduler activations
Android Mobile Application Development
Task Management System (TMS)
Mobile Device Development
Firebase Cloud messaging A primer
Android training in Chandigarh. What is ADB ADB stands for Android Debug Bridge. It is a command line tool that is used to communicate with the emulator.
Android Programming Lecture 9
Developing Android Services
intro to notifications in iOS 10
Android Programming Lecture 8
Application Fundamentals
PredictRemainingTime
Mobile Software Development Framework: Android
Zicheng Wan and Yuan Gao CPSC 6820, Clemson University
Android Topics UI Thread and Limited processing resources
Architecture Competency Group
CS323 Android Topics Network Basics for an Android App
Android Topics Asynchronous Callsbacks
Activities and Intents
Android Topics Threads and the MessageQueue
Activities and Intents
Android Developer Fundamentals V2 Lesson 5
Threads, Handlers, and AsyncTasks
Service Services.
Google Cloud Messaging (sort of)
Mobile Programming Dr. Mohsin Ali Memon.
Application Fundamentals
Lecture 2: Android Concepts
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Android Development Tools
CIS 470 Mobile App Development
Mobile Programming Broadcast Receivers.
Presentation transcript:

Notifications and Services Landon Cox March 28, 2017

Networking so far Volley (retrieve quiz stored at URL) User starts app Check with server for new quiz Say you’re building a messaging app … How else might you use the network? Are the workflows different than those we’ve seen?

How do we keep device data fresh?

Solution 1: polling Basic idea Very simple to implement App periodically sends message to server Asks, “is there anything new for me?” Very simple to implement Create a timer When timer fires, exchange messages with server What’s the problem?

Impact of polling on battery Network power LTE: 600 – 1700 mW WiFi: 77 – 130 mW Assume radio stays in high-power state for 10 sec. Energy per LTE poll: 1500 mW x 10 seconds = 15 Joules 5 min frequency: 480 J/day 30 sec. frequency: 4800 J/day Total Pixel battery: ~40k Joules You probably have more than one app that needs to sync If you have a lot of apps, your radios will always be in a high-power state Choosing the right period right is difficult Check too often and drain the battery with useless messages Check too infrequently and user experience will suffer http://dl.acm.org/citation.cfm?id=2307658

Every outgoing query will put the radio in a high-power state.

Solution 2: push notifications Share persistent network connection across apps Device and server maintain (TCP) connection Radio can enter low-power state when no updates Server pushes updates to device Device can push updates to server Updates should be small (< 4KB) On-device service routes updates to apps Example: Firebase Cloud Messaging (FCM)

Share one persistent connection. High-power only when data is ready.

Firebase cloud messaging (FCM) Formerly Google Cloud Messaging (GCM) On your device Google Play Services (global, maintains connection to cloud) FirebaseInstanceIDService (per app, receives instance ID) FirebaseMessageService (per app, receives notifications) In the cloud Firebase (routes messages to app server, devices) App server (stores data, manages messaging)

Firebase cloud messaging (FCM) Formerly Google Cloud Messaging (GCM) On your device Google Play Services (global, maintains connection to cloud) FirebaseInstanceIDService (per app, receives instance ID) FirebaseMessageService (per app, receives data) In the cloud Firebase (routes messages to app server, devices) App server (stores data, manages messaging) Next time

Services Service Who schedules most work on threads? Performs tasks on main thread: onStartCommand Defined as an app component Because it is a component, it is externally visible Who schedules most work on threads? Code already running inside our app

Services Service Why might external visibility be useful? Performs tasks on main thread: onStartCommand Defined as an app component Because it is a component, it is externally visible Why might external visibility be useful? Allows processes to communicate with each other

Services Service Why are services essential for FCM? Performs tasks on main thread: onStartCommand Defined as an app component Because it is a component, it is externally visible Why are services essential for FCM? Gives Google Play Services a place to send messages Forward new data from Firebase to app’s Service

Services Service Need to add Service to Android Manifest Performs tasks on main thread: onStartCommand Defined as an app component Because it is a component, it is externally visible Need to add Service to Android Manifest AndroidManifest.xml: <application … <service android:name=“.MyService” /> />

What thread does this run on? Services Example Service Create a subclass of Service Use a worker thread to process IPC requests Handle requests serially public class MyService extends Service { protected int onStartCommand(Intent intent) { // process IPC request } What thread does this run on?

Services Example Service Create a subclass of Service Use a worker thread to process IPC requests Handle requests serially public class MyService extends Service { protected int onStartCommand(Intent intent) { // process IPC request } What is an Intent?

Quick Intents review

How does Android know which components could receive this Intent? Displaying a web URL How does Android know which components could receive this Intent? Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(" http://www.google.com/images?q=duke ")); startActivity(browserIntent);

Intent filters Restricts which actions the Activity will accept (Intent Action must match) <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

Intent filters Describes categories of Intent Activity accepts <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> Describes categories of Intent Activity accepts (Intent categories must match all)

Implementing MyService We can do this! Tools to use Handler Looper HandlerThread Useful to peak under the FCM hood

Implementing MyService public class MyService extends Service { private Looper mLooper; private ServiceHandler mHandler; protected void onCreate() { // on which thread does this code run? } protected int onStartCommand(Intent intent) { private class ServiceHandler extends Handler { public ServiceHandler(Looper l) { super (l); } public void handleMessage(Message m) { … } What needs to happen? https://developer.android.com/reference/android/app/Service.html

Implementing MyService public class MyService extends Service { private Looper mLooper; private ServiceHandler mHandler; protected void onCreate() { HandlerThread thread = new HandlerThread(…); thread.start(); // Get the Looper and use it for our Handler mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); // on which thread does this code run? } … private class ServiceHandler extends Handler { public ServiceHandler(Looper l) { super (l); } public void handleMessage(Message m) { … }

Implementing MyService public class MyService extends Service { private Looper mLooper; private ServiceHandler mHandler; protected void onCreate() { // on which thread does this code run? } protected int onStartCommand(Intent intent) { private class ServiceHandler extends Handler { public ServiceHandler(Looper l) { super (l); } public void handleMessage(Message m) { … } What needs to happen?

Implementing MyService public class MyService extends Service { private Looper mLooper; private ServiceHandler mHandler; … protected int onStartCommand(Intent intent) { Toast.makeText(…).show(); // For each start request, forward the Intent Message msg = mServiceHandler.obtainMessage(); msg.arg1 = intent; mServiceHandler.sendMessage(msg); return 0; // on which thread does this code run? }

Implementing MyService public class MyService extends Service { private Looper mLooper; private ServiceHandler mHandler; protected void onCreate() { // on which thread does this code run? } protected int onStartCommand(Intent intent) { private class ServiceHandler extends Handler { public ServiceHandler(Looper l) { super (l); } public void handleMessage(Message m) { … } Lucky for you, you can just subclass Firebase Services!

Firebase services FirebaseInstanceIDService FirebaseMessageService Why does each app instance need a unique ID? Who manages mapping from users to instance IDs? FirebaseMessageService What should happen if your app is in the foreground? What should happen if your app is in the background? Note: you will likely need to update via SDK manager git clone https://github.com/firebase/quickstart-android.git