Google cloud messaging
Aithough we use SyncManager and SyncAdapters to improve the battery usage when update.The HZ that a client check to the server higher, the informatation more update,However, if the hZ is high,will use more power. Do we have amethod let the power usage low and can get the must update information?One method is the server tell the client there has a update.
In this method, we need Google cloud messaging
Server can send message to GCM,GCM send message to the app there is a update. The message can brinh information.
now create the notification for the sunshine app
The aim is:when get the updated weather information,and more than or equal 1 day no notification ,than show notification .The time that show notification before is use share preference to save.
You’ll be guided through the first part of adding notifications and then do the rest yourself. The changes you will make now can be found in this diff.
Instructions
1. Add Notification Format
2. Add a Projection
Because your notification data will be pulled from the database, you need to add projection and column indices values in your SunshineSyncAdapter:
private static final String[] NOTIFY_WEATHER_PROJECTION = new String[] {
WeatherContract.WeatherEntry.COLUMN_WEATHER_ID,
WeatherContract.WeatherEntry.COLUMN_MAX_TEMP,
WeatherContract.WeatherEntry.COLUMN_MIN_TEMP,
WeatherContract.WeatherEntry.COLUMN_SHORT_DESC
};
// these indices must match the projection
private static final int INDEX_WEATHER_ID = 0;
private static final int INDEX_MAX_TEMP = 1;
private static final int INDEX_MIN_TEMP = 2;
private static final int INDEX_SHORT_DESC = 3;
WeatherContract.WeatherEntry.COLUMN_WEATHER_ID,
WeatherContract.WeatherEntry.COLUMN_MAX_TEMP,
WeatherContract.WeatherEntry.COLUMN_MIN_TEMP,
WeatherContract.WeatherEntry.COLUMN_SHORT_DESC
};
// these indices must match the projection
private static final int INDEX_WEATHER_ID = 0;
private static final int INDEX_MAX_TEMP = 1;
private static final int INDEX_MIN_TEMP = 2;
private static final int INDEX_SHORT_DESC = 3;
3. Add Related Constants
You’ll also add some additional constants at the top of SunshineSyncAdapter; DAY_IN_MILLIS is the amount of milliseconds in a day and WEATHER_NOTIFICATION_ID is an id you create that is matched to your notification so that you can reuse it. If you reuse the notification ID, your application will post at most one notification.
private static final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
private static final int WEATHER_NOTIFICATION_ID = 3004;
private static final int WEATHER_NOTIFICATION_ID = 3004;
4. Create notifyWeather Function
Then you will create the notifyWeather function in SunshineSyncAdapter. Right now this function:
- Checks whether you’ve already shown a notification today.
- If you haven’t, then it connects to the database and gets a cursor for the current day.
- Fetches the data from the database.
- Formats the data in a string contentText. Notice how there is also the iconId and title for you to use.
5. Call notifyWeather
Call notifyWeather(); within the onPerformSync function. You should call it when weather data is inserted into the database (right after bulkInsert is called).
Your Task
Right now your notifyWeather function is called and pulls data from the database, but it is not actually showing a notification. Your goal is to finish the notifyWeather function.
To do this, you will need to:
- Create the Notification using NotificationCompat.builder.
- Create an explicit intent for what the notification should open.
- Using TaskStackBuilder, create an artificial “backstack” so that when the user clicks the back button, it is clear to Android where the user will go.
- Tell the NotificationManager to show the notification.
soultion:
//build your notification here.
//Create a Notification builder first
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);
//set the small icon(Must)
nBuilder.setSmallIcon(R.mipmap.ic_launcher);
//set setContentTitle(must)
nBuilder.setContentTitle(title);
//set setContentText(must)
nBuilder.setContentText(contentText);
//Create an explicit intent for what the notification should open.
Intent openAActivityIntent = new Intent(context, MainActivity.class);
//Using TaskStackBuilder, create an artificial “backstack” so that when the user clicks the back button, it is clear to Android where the user will go.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
//create the Pending intent that use by the click
stackBuilder.addNextIntent(openAActivityIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
//sett the PendingIntent to the builder
nBuilder.setContentIntent(resultPendingIntent);
//use NotificationManager to show the Notification we build
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(WEATHER_NOTIFICATION_ID, nBuilder.build());
Related classes:
answer of the whole project:
the guide of notification: