Home | History | Annotate | Download | only in app_package
      1 package ${packageName};
      2 
      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;
     19 </#if>
     20 
     21 /**
     22  * Helper class for showing and canceling ${display_title?lower_case}
     23  * notifications.
     24  * <p>
     25  * This class makes heavy use of the {@link NotificationCompat.Builder} helper
     26  * class to create notifications in a backward-compatible way.
     27  */
     28 public class ${className} {
     29     /**
     30      * The unique identifier for this type of notification.
     31      */
     32     private static final String NOTIFICATION_TAG = "${notificationName}";
     33 
     34     /**
     35      * Shows the notification, or updates a previously shown notification of
     36      * this type, with the given parameters.
     37      * <p>
     38      * TODO: Customize this method's arguments to present relevant content in
     39      * the notification.
     40      * <p>
     41      * TODO: Customize the contents of this method to tweak the behavior and
     42      * presentation of ${display_title?lower_case} notifications. Make
     43      * sure to follow the
     44      * <a href="https://developer.android.com/design/patterns/notifications.html">
     45      * Notification design guidelines</a> when doing so.
     46      *
     47      * @see #cancel(Context)
     48      */
     49     public static void notify(final Context context,
     50             final String exampleString, final int number) {
     51         final Resources res = context.getResources();
     52 
     53         <#if expandedStyle == "picture">
     54         // This image is used as the notification's large icon (thumbnail) when
     55         // the notification is collapsed, and as the big picture to show when
     56         // the notification is expanded.
     57         <#else>
     58         // This image is used as the notification's large icon (thumbnail).
     59         // TODO: Remove this if your notification has no relevant thumbnail.
     60         </#if>
     61         final Bitmap picture = BitmapFactory.decodeResource(res, R.drawable.example_picture);
     62 
     63         <#if expandedStyle == 'list'>
     64         final SpannableStringBuilder exampleItem = new SpannableStringBuilder();
     65         exampleItem.append("Dummy");
     66         exampleItem.setSpan(new ForegroundColorSpan(Color.WHITE), 0, exampleItem.length(), 0);
     67         exampleItem.append("   Example content");
     68         </#if>
     69 
     70         final String ticker = exampleString;
     71         final String title = res.getString(
     72                 R.string.${notification_name}_notification_title_template, exampleString);
     73         final String text = res.getString(
     74                 R.string.${notification_name}_notification_placeholder_text_template, exampleString);
     75 
     76         final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
     77 
     78                 // Set appropriate defaults for the notification light, sound,
     79                 // and vibration.
     80                 .setDefaults(Notification.DEFAULT_ALL)
     81 
     82                 // Set required fields, including the small icon, the
     83                 // notification title, and text.
     84                 .setSmallIcon(R.drawable.${icon_resource})
     85                 .setContentTitle(title)
     86                 .setContentText(text)
     87 
     88                 // All fields below this line are optional.
     89 
     90                 // Use a default priority (recognized on devices running Android
     91                 // 4.1 or later)
     92                 .setPriority(NotificationCompat.PRIORITY_DEFAULT)
     93 
     94                 // Provide a large icon, shown with the notification in the
     95                 // notification drawer on devices running Android 3.0 or later.
     96                 .setLargeIcon(picture)
     97 
     98                 // Set ticker text (preview) information for this notification.
     99                 .setTicker(ticker)
    100 
    101                 // Show a number. This is useful when stacking notifications of
    102                 // a single type.
    103                 .setNumber(number)
    104 
    105                 // If this notification relates to a past or upcoming event, you
    106                 // should set the relevant time information using the setWhen
    107                 // method below. If this call is omitted, the notification's
    108                 // timestamp will by set to the time at which it was shown.
    109                 // TODO: Call setWhen if this notification relates to a past or
    110                 // upcoming event. The sole argument to this method should be
    111                 // the notification timestamp in milliseconds.
    112                 //.setWhen(...)
    113 
    114                 // Set the pending intent to be initiated when the user touches
    115                 // the notification.
    116                 .setContentIntent(
    117                         PendingIntent.getActivity(
    118                                 context,
    119                                 0,
    120                                 new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")),
    121                                 PendingIntent.FLAG_UPDATE_CURRENT))
    122                 <#if expandedStyle == 'picture'>
    123 
    124                 // Show an expanded photo on devices running Android 4.1 or
    125                 // later.
    126                 .setStyle(new NotificationCompat.BigPictureStyle()<#--
    127                         TODO: .bigLargeIcon(null) when the support library supports it -->
    128                         .bigPicture(picture)
    129                         .setBigContentTitle(title)
    130                         .setSummaryText("Dummy summary text"))
    131                 <#elseif expandedStyle == 'list'>
    132 
    133                 // Show an expanded list of items on devices running Android 4.1
    134                 // or later.
    135                 .setStyle(new NotificationCompat.InboxStyle()
    136                         .addLine(exampleItem)
    137                         .addLine(exampleItem)
    138                         .addLine(exampleItem)
    139                         .addLine(exampleItem)
    140                         .setBigContentTitle(title)
    141                         .setSummaryText("Dummy summary text"))
    142                 <#elseif expandedStyle == 'text'>
    143 
    144                 // Show expanded text content on devices running Android 4.1 or
    145                 // later.
    146                 .setStyle(new NotificationCompat.BigTextStyle()
    147                         .bigText(text)
    148                         .setBigContentTitle(title)
    149                         .setSummaryText("Dummy summary text"))
    150                 </#if>
    151                 <#if moreActions>
    152 
    153                 // Example additional actions for this notification. These will
    154                 // only show on devices running Android 4.1 or later, so you
    155                 // should ensure that the activity in this notification's
    156                 // content intent provides access to the same actions in
    157                 // another way.
    158                 .addAction(
    159                         R.drawable.ic_action_stat_share,
    160                         res.getString(R.string.action_share),
    161                         PendingIntent.getActivity(
    162                                 context,
    163                                 0,
    164                                 Intent.createChooser(new Intent(Intent.ACTION_SEND)
    165                                         .setType("text/plain")
    166                                         .putExtra(Intent.EXTRA_TEXT, "Dummy text"), "Dummy title"),
    167                                 PendingIntent.FLAG_UPDATE_CURRENT))
    168                 .addAction(
    169                         R.drawable.ic_action_stat_reply,
    170                         res.getString(R.string.action_reply),
    171                         null)
    172                 </#if>
    173 
    174                 // Automatically dismiss the notification when it is touched.
    175                 .setAutoCancel(true);
    176 
    177         notify(context, builder.build());
    178     }
    179 
    180     @TargetApi(Build.VERSION_CODES.ECLAIR)
    181     private static void notify(final Context context, final Notification notification) {
    182         final NotificationManager nm = (NotificationManager) context
    183                 .getSystemService(Context.NOTIFICATION_SERVICE);
    184         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
    185             nm.notify(NOTIFICATION_TAG, 0, notification);
    186         } else {
    187             nm.notify(NOTIFICATION_TAG.hashCode(), notification);
    188         }
    189     }
    190 
    191     /**
    192      * Cancels any notifications of this type previously shown using
    193      * {@link #notify(Context, String, int)}.
    194      */
    195     @TargetApi(Build.VERSION_CODES.ECLAIR)
    196     public static void cancel(final Context context) {
    197         final NotificationManager nm = (NotificationManager) context
    198                 .getSystemService(Context.NOTIFICATION_SERVICE);
    199         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
    200             nm.cancel(NOTIFICATION_TAG, 0);
    201         } else {
    202             nm.cancel(NOTIFICATION_TAG.hashCode());
    203         }
    204     }
    205 }