diff options
author | Paul Rashidi <paulrashidi@google.com> | 2016-03-07 15:26:58 -0800 |
---|---|---|
committer | Paul Rashidi <paulrashidi@google.com> | 2016-03-07 17:19:31 -0800 |
commit | 5ff2774b0f82979c4b4cfcae64f110da0e0ab618 (patch) | |
tree | 161b065575260edf6f8eb5514094bb4929628f98 /notification | |
parent | 8505cdf0a44194c58b42d636251590d80f3c1a45 (diff) | |
download | android-5ff2774b0f82979c4b4cfcae64f110da0e0ab618.tar.gz |
Updating sample to use notification groups and summaries for Android N.
Change-Id: Icf4cfcb19aeab7bcecc651410e1643068d5736d8
Diffstat (limited to 'notification')
3 files changed, 119 insertions, 56 deletions
diff --git a/notification/ActiveNotifications/Application/src/main/java/com/example/android/activenotifications/ActiveNotificationFragment.java b/notification/ActiveNotifications/Application/src/main/java/com/example/android/activenotifications/ActiveNotificationFragment.java index 3e87849a..7753b371 100644 --- a/notification/ActiveNotifications/Application/src/main/java/com/example/android/activenotifications/ActiveNotificationFragment.java +++ b/notification/ActiveNotifications/Application/src/main/java/com/example/android/activenotifications/ActiveNotificationFragment.java @@ -23,6 +23,7 @@ import android.content.Intent; import android.os.Bundle; import android.service.notification.StatusBarNotification; import android.support.v4.app.Fragment; +import android.support.v4.app.NotificationCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -31,17 +32,31 @@ import android.widget.TextView; import com.example.android.common.logger.Log; /** - * A fragment that enables display of notifications. + * A fragment that allows notifications to be enqueued. */ public class ActiveNotificationFragment extends Fragment { + /** + * The request code can be any number as long as it doesn't match another request code used + * in the same app. + */ + private static final int REQUEST_CODE = 2323; + private static final String TAG = "ActiveNotificationFragment"; + private static final String NOTIFICATION_GROUP = + "com.example.android.activenotifications.notification_type"; + + private static final int NOTIFICATION_GROUP_SUMMARY_ID = 1; + private NotificationManager mNotificationManager; + private TextView mNumberOfNotifications; - // Every notification needs a unique ID otherwise the previous one would be overwritten. - private int mNotificationId = 0; + // Every notification needs a unique ID otherwise the previous one would be overwritten. This + // variable is incremented when used. + private static int sNotificationId = NOTIFICATION_GROUP_SUMMARY_ID + 1; + private PendingIntent mDeletePendingIntent; @Override @@ -69,7 +84,7 @@ public class ActiveNotificationFragment extends Fragment { public void onClick(View v) { switch (v.getId()) { case R.id.add_notification: { - addNotificationAndReadNumber(); + addNotificationAndUpdateSummaries(); break; } } @@ -81,33 +96,72 @@ public class ActiveNotificationFragment extends Fragment { // Create a PendingIntent to be fired upon deletion of a Notification. Intent deleteIntent = new Intent(ActiveNotificationActivity.ACTION_NOTIFICATION_DELETE); mDeletePendingIntent = PendingIntent.getBroadcast(getActivity(), - 2323 /* requestCode */, deleteIntent, 0); + REQUEST_CODE, deleteIntent, 0); // [END create_pending_intent_for_deletion] } /** - * Add a new {@link Notification} with sample data and send it to the system. - * Then read the current number of displayed notifications for this application. + * Adds a new {@link Notification} with sample data and sends it to the system. + * Then updates the current number of displayed notifications for this application and + * creates a notification summary if more than one notification exists. */ - private void addNotificationAndReadNumber() { + private void addNotificationAndUpdateSummaries() { // [BEGIN create_notification] // Create a Notification and notify the system. - final Notification.Builder builder = new Notification.Builder(getActivity()) + final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity()) .setSmallIcon(R.mipmap.ic_notification) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.sample_notification_content)) .setAutoCancel(true) - .setDeleteIntent(mDeletePendingIntent); + .setDeleteIntent(mDeletePendingIntent) + .setGroup(NOTIFICATION_GROUP); final Notification notification = builder.build(); - mNotificationManager.notify(++mNotificationId, notification); + mNotificationManager.notify(getNewNotificationId(), notification); // [END create_notification] Log.i(TAG, "Add a notification"); + + updateNotificationSummary(); updateNumberOfNotifications(); } /** - * Request the current number of notifications from the {@link NotificationManager} and + * Adds/updates/removes the notification summary as necessary. + */ + protected void updateNotificationSummary() { + final StatusBarNotification[] activeNotifications = mNotificationManager + .getActiveNotifications(); + + int numberOfNotifications = activeNotifications.length; + // Since the notifications might include a summary notification remove it from the count if + // it is present. + for (StatusBarNotification notification : activeNotifications) { + if (notification.getId() == NOTIFICATION_GROUP_SUMMARY_ID) { + numberOfNotifications--; + break; + } + } + + if (numberOfNotifications > 1) { + // Add/update the notification summary. + String notificationContent = getString(R.string.sample_notification_summary_content, + "" + numberOfNotifications); + final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity()) + .setSmallIcon(R.mipmap.ic_notification) + .setStyle(new NotificationCompat.BigTextStyle() + .setSummaryText(notificationContent)) + .setGroup(NOTIFICATION_GROUP) + .setGroupSummary(true); + final Notification notification = builder.build(); + mNotificationManager.notify(NOTIFICATION_GROUP_SUMMARY_ID, notification); + } else { + // Remove the notification summary. + mNotificationManager.cancel(NOTIFICATION_GROUP_SUMMARY_ID); + } + } + + /** + * Requests the current number of notifications from the {@link NotificationManager} and * display them to the user. */ protected void updateNumberOfNotifications() { @@ -121,4 +175,19 @@ public class ActiveNotificationFragment extends Fragment { numberOfNotifications)); Log.i(TAG, getString(R.string.active_notifications, numberOfNotifications)); } + + /** + * Retrieves a unique notification ID. + */ + public int getNewNotificationId() { + int notificationId = sNotificationId++; + + // Unlikely in the sample, but the int will overflow if used enough so we skip the summary + // ID. Most apps will prefer a more deterministic way of identifying an ID such as hashing + // the content of the notification. + if (notificationId == NOTIFICATION_GROUP_SUMMARY_ID) { + notificationId = sNotificationId++; + } + return notificationId; + } } diff --git a/notification/ActiveNotifications/Application/src/main/res/values/strings.xml b/notification/ActiveNotifications/Application/src/main/res/values/strings.xml index 0f2977d1..a4275fdf 100644 --- a/notification/ActiveNotifications/Application/src/main/res/values/strings.xml +++ b/notification/ActiveNotifications/Application/src/main/res/values/strings.xml @@ -19,4 +19,5 @@ <string name="active_notifications">Active Notifications: %1$s</string> <string name="update_notification_count">Update count</string> <string name="sample_notification_content">This is a sample notification.</string> + <string name="sample_notification_summary_content">There are %s ActiveNotifications.</string> </resources> diff --git a/notification/ActiveNotifications/template-params.xml b/notification/ActiveNotifications/template-params.xml index 65fefd48..a4117c7c 100644 --- a/notification/ActiveNotifications/template-params.xml +++ b/notification/ActiveNotifications/template-params.xml @@ -16,57 +16,39 @@ --> <sample> <name>ActiveNotification</name> - <group>Notification</group> <!-- This field will be deprecated in the future - and replaced with the "categories" tags below. --> + <group>Android N Preview</group> <package>com.example.android.activenotifications</package> - <minSdk>23</minSdk> - - <!-- Include additional dependencies here.--> - <!-- dependency>com.google.android.gms:play-services:5.0.+</dependency --> + <minSdk>"N"</minSdk> + <targetSdkVersion>"N"</targetSdkVersion> + <compileSdkVersion>"android-N"</compileSdkVersion> <strings> <intro> <![CDATA[ -The NotificationManager can tell you how many notifications your application is currently showing. -This sample demonstrates how to use this API that has been introduced with Android M. -To get started, press the "add a notification" button. -When a notification is being canceled, the count gets updated via a PendingIntent. +Notifications can now be grouped in Android N. Since Android M, the +NotificationManager can tell you how many notifications your application +is currently showing. This sample demonstrates how to use these APIs +together for a nicer user experience when an app may have multiple +notifications. To get started, press the "add a notification" button. +If you add more than one notification a notification summary will be +added. When a notification is being canceled, the count gets updated +via a PendingIntent. ]]> </intro> </strings> - <!-- The basic templates have already been enabled. Uncomment more as desired. --> <template src="base" /> <template src="FragmentView" /> <common src="activities" /> <common src="logger" /> <metadata> - <!-- Values: {DRAFT | PUBLISHED | INTERNAL | DEPRECATED | SUPERCEDED} --> <status>DRAFT</status> - <!-- See http://go/sample-categories for details on the next 4 fields. --> - <!-- Most samples just need to udpate the Categories field. This is a comma- - seperated list of topic tags. Unlike the old category system, samples - may have multiple categories, so feel free to add extras. Try to avoid - simply tagging everything with "UI". :)--> - <categories>Getting Started, Notification</categories> + <categories>Getting Started, Notification, Android N Preview</categories> <technologies>Android</technologies> <languages>Java</languages> <solutions>Mobile</solutions> - <!-- Values: {BEGINNER | INTERMEDIATE | ADVANCED | EXPERT} --> - <!-- Beginner is for "getting started" type content, or essential content. - (e.g. "Hello World", activities, intents) - - Intermediate is for content that covers material a beginner doesn't need - to know, but that a skilled developer is expected to know. - (e.g. services, basic styles and theming, sync adapters) - - Advanced is for highly technical content geared towards experienced developers. - (e.g. performance optimizations, custom views, bluetooth) - - Expert is reserved for highly technical or specialized content, and should - be used sparingly. (e.g. VPN clients, SELinux, custom instrumentation runners) --> <level>INTERMEDIATE</level> <icon>screenshots/icon-web.png</icon> <screenshots> @@ -78,22 +60,33 @@ When a notification is being canceled, the count gets updated via a PendingInten <android>android.app.NotificationManager</android> </api_refs> <description> - The NotificationManager can tell you how many notifications your application is - currently showing. This sample demonstrates how to use this API that has been - introduced with Android M. - </description> +<![CDATA[ +Notification Groups and the Notification Manager can be used together to simplify +how users experience notifications. This sample demonstrates how +the NotificationManager can tell you how many notifications your +application is currently showing. It also shows how to manage the +notifications as a group and introduce a summary for the group, when +supported by the platform. +]]> + </description> <intro> - The [NotificationManager][1] has become more powerful. - Starting with Android M, you can query it for the active notifications that - your app sent using the [notify][2] methods. +<![CDATA[ +Notifications can now be grouped. + +Starting with Android N, you can set notification groups for notifications +and define a notification group summary. + +Starting with Android M, you can query it for the active notifications that +your app sent using the [notify][2] methods. - This sample demonstrates simple use of this newly added functionality by - allowing a user to add notifications and then querying how many notifications - are currently being displayed via the [getActiveNotifications()][3] method. +This sample demonstrates simple use of these features by allowing a user +to add notifications and then query how many notifications +are currently being displayed via the [getActiveNotifications()][3] method. - [1]: https://developer.android.com/reference/android/app/NotificationManager.html - [2]: https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification) - [2]: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications() +[1]: https://developer.android.com/reference/android/app/NotificationManager.html +[2]: https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification) +[3]: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications() +]]> </intro> </metadata> </sample> |