1 package ${packageName};
3 import android.annotation.TargetApi;
4 import android.app.Notification;
5 import android.app.NotificationManager;
6 import android.app.PendingIntent;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.res.Resources;
10 import android.graphics.Bitmap;
11 import android.graphics.BitmapFactory;
12 import android.net.Uri;
13 import android.os.Build;
14 import android.support.v4.app.NotificationCompat;
15 <#if expandedStyle == 'list'>
16 import android.graphics.Color;
17 import android.text.SpannableStringBuilder;
18 import android.text.style.ForegroundColorSpan;
20 <#if applicationPackage??>import ${applicationPackage}.R;</#if>
23 * Helper class for showing and canceling ${display_title?lower_case}
26 * This class makes heavy use of the {@link NotificationCompat.Builder} helper
27 * class to create notifications in a backward-compatible way.
29 public class ${className} {
31 * The unique identifier for this type of notification.
33 private static final String NOTIFICATION_TAG = "${notificationName}";
36 * Shows the notification, or updates a previously shown notification of
37 * this type, with the given parameters.
39 * TODO: Customize this method's arguments to present relevant content in
42 * TODO: Customize the contents of this method to tweak the behavior and
43 * presentation of ${display_title?lower_case} notifications. Make
45 * <a href="https://developer.android.com/design/patterns/notifications.html">
46 * Notification design guidelines</a> when doing so.
48 * @see #cancel(Context)
50 public static void notify(final Context context,
51 final String exampleString, final int number) {
52 final Resources res = context.getResources();
54 <#if expandedStyle == "picture">
55 // This image is used as the notification's large icon (thumbnail) when
56 // the notification is collapsed, and as the big picture to show when
57 // the notification is expanded.
59 // This image is used as the notification's large icon (thumbnail).
60 // TODO: Remove this if your notification has no relevant thumbnail.
62 final Bitmap picture = BitmapFactory.decodeResource(res, R.drawable.example_picture);
64 <#if expandedStyle == 'list'>
65 final SpannableStringBuilder exampleItem = new SpannableStringBuilder();
66 exampleItem.append("Dummy");
67 exampleItem.setSpan(new ForegroundColorSpan(Color.WHITE), 0, exampleItem.length(), 0);
68 exampleItem.append(" Example content");
71 final String ticker = exampleString;
72 final String title = res.getString(
73 R.string.${notification_name}_notification_title_template, exampleString);
74 final String text = res.getString(
75 R.string.${notification_name}_notification_placeholder_text_template, exampleString);
77 final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
79 // Set appropriate defaults for the notification light, sound,
81 .setDefaults(Notification.DEFAULT_ALL)
83 // Set required fields, including the small icon, the
84 // notification title, and text.
85 .setSmallIcon(R.drawable.${icon_resource})
86 .setContentTitle(title)
89 // All fields below this line are optional.
91 // Use a default priority (recognized on devices running Android
93 .setPriority(NotificationCompat.PRIORITY_DEFAULT)
95 // Provide a large icon, shown with the notification in the
96 // notification drawer on devices running Android 3.0 or later.
97 .setLargeIcon(picture)
99 // Set ticker text (preview) information for this notification.
102 // Show a number. This is useful when stacking notifications of
106 // If this notification relates to a past or upcoming event, you
107 // should set the relevant time information using the setWhen
108 // method below. If this call is omitted, the notification's
109 // timestamp will by set to the time at which it was shown.
110 // TODO: Call setWhen if this notification relates to a past or
111 // upcoming event. The sole argument to this method should be
112 // the notification timestamp in milliseconds.
115 // Set the pending intent to be initiated when the user touches
118 PendingIntent.getActivity(
121 new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")),
122 PendingIntent.FLAG_UPDATE_CURRENT))
123 <#if expandedStyle == 'picture'>
125 // Show an expanded photo on devices running Android 4.1 or
127 .setStyle(new NotificationCompat.BigPictureStyle()<#--
128 TODO: .bigLargeIcon(null) when the support library supports it -->
130 .setBigContentTitle(title)
131 .setSummaryText("Dummy summary text"))
132 <#elseif expandedStyle == 'list'>
134 // Show an expanded list of items on devices running Android 4.1
136 .setStyle(new NotificationCompat.InboxStyle()
137 .addLine(exampleItem)
138 .addLine(exampleItem)
139 .addLine(exampleItem)
140 .addLine(exampleItem)
141 .setBigContentTitle(title)
142 .setSummaryText("Dummy summary text"))
143 <#elseif expandedStyle == 'text'>
145 // Show expanded text content on devices running Android 4.1 or
147 .setStyle(new NotificationCompat.BigTextStyle()
149 .setBigContentTitle(title)
150 .setSummaryText("Dummy summary text"))
154 // Example additional actions for this notification. These will
155 // only show on devices running Android 4.1 or later, so you
156 // should ensure that the activity in this notification's
157 // content intent provides access to the same actions in
160 R.drawable.ic_action_stat_share,
161 res.getString(R.string.action_share),
162 PendingIntent.getActivity(
165 Intent.createChooser(new Intent(Intent.ACTION_SEND)
166 .setType("text/plain")
167 .putExtra(Intent.EXTRA_TEXT, "Dummy text"), "Dummy title"),
168 PendingIntent.FLAG_UPDATE_CURRENT))
170 R.drawable.ic_action_stat_reply,
171 res.getString(R.string.action_reply),
175 // Automatically dismiss the notification when it is touched.
176 .setAutoCancel(true);
178 notify(context, builder.build());
181 @TargetApi(Build.VERSION_CODES.ECLAIR)
182 private static void notify(final Context context, final Notification notification) {
183 final NotificationManager nm = (NotificationManager) context
184 .getSystemService(Context.NOTIFICATION_SERVICE);
185 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
186 nm.notify(NOTIFICATION_TAG, 0, notification);
188 nm.notify(NOTIFICATION_TAG.hashCode(), notification);
193 * Cancels any notifications of this type previously shown using
194 * {@link #notify(Context, String, int)}.
196 @TargetApi(Build.VERSION_CODES.ECLAIR)
197 public static void cancel(final Context context) {
198 final NotificationManager nm = (NotificationManager) context
199 .getSystemService(Context.NOTIFICATION_SERVICE);
200 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
201 nm.cancel(NOTIFICATION_TAG, 0);
203 nm.cancel(NOTIFICATION_TAG.hashCode());