22. GCM/FCM
Use FCM with High priority - but treat it with special care
{
"to" : "...",
"priority" : "high",
"notification" : {
...
},
"data" : {
...
}
}
23. WhiteList
● An app can fire the
ACTION_IGNORE_BATTERY_OPTIMIZATION_SE
TTINGS intent to take the user directly to the
Battery Optimization, where they can add the
app.
24. Note: Google Play policies prohibit apps from
requesting direct exemption from Power
Management features in Android 6.0+ (Doze
and App Standby) unless the core function of
the app is adversely affected.
51. public class RebelService extends Service implements
Handler.Callback
public void onCreate() {
handlerThread = new HandlerThread("RebelServiceHandlerThread");
handlerThread.start();
Looper looper = handlerThread.getLooper();
handler = new Handler(looper, this);
handler.sendEmptyMessage(WHAT_MAKE_NETWORK_REQUEST);
}
52. public class RebelService extends Service implements
Handler.Callback
@Override public boolean handleMessage(Message msg) {
StarWarsUtils.doingNetworkCommunication();
handler.sendEmptyMessageDelayed(
WHAT_MAKE_NETWORK_REQUEST,
DELAY_MILLIS);
return true;
}
57. What?
Schedule the task to execute it when certain conditions
met.
(charging, idle, connected to a network or connected to
an unmetered network)
58. Why two?
JobScheduler was introduced in API >= 21 (Lollipop).
GCMNetworkManager - is part of GCM package. When using on
devices >= 21, use JobScheduler underneath.
64. BestTimeService.java
/**
* Task run by GcmNetworkManager when all the
requirements of the scheduled
* task are met.
*/
public class BestTimeService extends GcmTaskService {
...
}
65. BestTimeService.java
@Override public int onRunTask(TaskParams taskParams) {
switch (taskParams.getTag()) {
case LUKE_DECISION:
...
return GcmNetworkManager.RESULT_SUCCESS;
case HAN_SOLO_LOCATION:
...
return GcmNetworkManager.RESULT_RESCHEDULE;
default:
return GcmNetworkManager.RESULT_FAILURE;
}
}
66. BestTimeService.java
public int onRunTask(TaskParams taskParams) {
…
case LUKE_DECISION:
Bundle extras = taskParams.getExtras();
LukeDecision decision = (LukeDecision)
extras.getSerializable(...);
StarWarsUtils.makeDecision(decision, this);
return GcmNetworkManager.RESULT_SUCCESS;
…
}
68. Task task = new PeriodicTask.Builder()
.setService(BestTimeService.class)
.setPeriod(SIXTY_SEC)
.setFlex(10)
.setTag(BestTimeService.HAN_SOLO_LOCATION)
.setPersisted(true)
.build();
public class FinalBattleActivity
72. Not all devices shipped with Play Services
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode == ConnectionResult.SUCCESS) {
mGcmNetworkManager.schedule(task);
} else {
// Deal with this networking task some other way
}
73. When Google Play updated it removes all
scheduled periodic tasks
public class BestTimeService extends GcmTaskService {
@Override
public void onInitializeTasks() {
super.onInitializeTasks();
// Reschedule removed tasks here
}
}
79. public class RebelService extends Service implements
Handler.Callback
public boolean handleMessage(Message msg) {
StarWarsUtils.doingNetworkCommunication());
handler.sendEmptyMessageDelayed(
WHAT_MAKE_NETWORK_REQUEST,
DELAY_MILLIS);
return true;
}
80. public class RebelService extends Service implements
Handler.Callback
public void onCreate() {
Notification notification =
...
startForeground(101, notification);
}