1*90c8c64dSAndroid Build Coastguard Worker package com.example.android.basicnotifications;
2*90c8c64dSAndroid Build Coastguard Worker 
3*90c8c64dSAndroid Build Coastguard Worker import android.app.Activity;
4*90c8c64dSAndroid Build Coastguard Worker import android.app.NotificationManager;
5*90c8c64dSAndroid Build Coastguard Worker import android.app.PendingIntent;
6*90c8c64dSAndroid Build Coastguard Worker import android.content.Intent;
7*90c8c64dSAndroid Build Coastguard Worker import android.graphics.BitmapFactory;
8*90c8c64dSAndroid Build Coastguard Worker import android.net.Uri;
9*90c8c64dSAndroid Build Coastguard Worker import android.os.Bundle;
10*90c8c64dSAndroid Build Coastguard Worker import android.support.v4.app.NotificationCompat;
11*90c8c64dSAndroid Build Coastguard Worker import android.view.View;
12*90c8c64dSAndroid Build Coastguard Worker 
13*90c8c64dSAndroid Build Coastguard Worker /**
14*90c8c64dSAndroid Build Coastguard Worker  * The entry point to the BasicNotification sample.
15*90c8c64dSAndroid Build Coastguard Worker  */
16*90c8c64dSAndroid Build Coastguard Worker public class MainActivity extends Activity {
17*90c8c64dSAndroid Build Coastguard Worker     /**
18*90c8c64dSAndroid Build Coastguard Worker      * A numeric value that identifies the notification that we'll be sending.
19*90c8c64dSAndroid Build Coastguard Worker      * This value needs to be unique within this app, but it doesn't need to be
20*90c8c64dSAndroid Build Coastguard Worker      * unique system-wide.
21*90c8c64dSAndroid Build Coastguard Worker      */
22*90c8c64dSAndroid Build Coastguard Worker     public static final int NOTIFICATION_ID = 1;
23*90c8c64dSAndroid Build Coastguard Worker 
onCreate(Bundle savedInstanceState)24*90c8c64dSAndroid Build Coastguard Worker     public void onCreate(Bundle savedInstanceState) {
25*90c8c64dSAndroid Build Coastguard Worker         super.onCreate(savedInstanceState);
26*90c8c64dSAndroid Build Coastguard Worker         setContentView(R.layout.sample_layout);
27*90c8c64dSAndroid Build Coastguard Worker 
28*90c8c64dSAndroid Build Coastguard Worker     }
29*90c8c64dSAndroid Build Coastguard Worker 
30*90c8c64dSAndroid Build Coastguard Worker     /**
31*90c8c64dSAndroid Build Coastguard Worker      * Send a sample notification using the NotificationCompat API.
32*90c8c64dSAndroid Build Coastguard Worker      */
sendNotification(View view)33*90c8c64dSAndroid Build Coastguard Worker     public void sendNotification(View view) {
34*90c8c64dSAndroid Build Coastguard Worker 
35*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE(build_action)
36*90c8c64dSAndroid Build Coastguard Worker         /** Create an intent that will be fired when the user clicks the notification.
37*90c8c64dSAndroid Build Coastguard Worker          * The intent needs to be packaged into a {@link android.app.PendingIntent} so that the
38*90c8c64dSAndroid Build Coastguard Worker          * notification service can fire it on our behalf.
39*90c8c64dSAndroid Build Coastguard Worker          */
40*90c8c64dSAndroid Build Coastguard Worker         Intent intent = new Intent(Intent.ACTION_VIEW,
41*90c8c64dSAndroid Build Coastguard Worker                 Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));
42*90c8c64dSAndroid Build Coastguard Worker         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
43*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(build_action)
44*90c8c64dSAndroid Build Coastguard Worker 
45*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE (build_notification)
46*90c8c64dSAndroid Build Coastguard Worker         /**
47*90c8c64dSAndroid Build Coastguard Worker          * Use NotificationCompat.Builder to set up our notification.
48*90c8c64dSAndroid Build Coastguard Worker          */
49*90c8c64dSAndroid Build Coastguard Worker         NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
50*90c8c64dSAndroid Build Coastguard Worker 
51*90c8c64dSAndroid Build Coastguard Worker         /** Set the icon that will appear in the notification bar. This icon also appears
52*90c8c64dSAndroid Build Coastguard Worker          * in the lower right hand corner of the notification itself.
53*90c8c64dSAndroid Build Coastguard Worker          *
54*90c8c64dSAndroid Build Coastguard Worker          * Important note: although you can use any drawable as the small icon, Android
55*90c8c64dSAndroid Build Coastguard Worker          * design guidelines state that the icon should be simple and monochrome. Full-color
56*90c8c64dSAndroid Build Coastguard Worker          * bitmaps or busy images don't render well on smaller screens and can end up
57*90c8c64dSAndroid Build Coastguard Worker          * confusing the user.
58*90c8c64dSAndroid Build Coastguard Worker          */
59*90c8c64dSAndroid Build Coastguard Worker         builder.setSmallIcon(R.drawable.ic_stat_notification);
60*90c8c64dSAndroid Build Coastguard Worker 
61*90c8c64dSAndroid Build Coastguard Worker         // Set the intent that will fire when the user taps the notification.
62*90c8c64dSAndroid Build Coastguard Worker         builder.setContentIntent(pendingIntent);
63*90c8c64dSAndroid Build Coastguard Worker 
64*90c8c64dSAndroid Build Coastguard Worker         // Set the notification to auto-cancel. This means that the notification will disappear
65*90c8c64dSAndroid Build Coastguard Worker         // after the user taps it, rather than remaining until it's explicitly dismissed.
66*90c8c64dSAndroid Build Coastguard Worker         builder.setAutoCancel(true);
67*90c8c64dSAndroid Build Coastguard Worker 
68*90c8c64dSAndroid Build Coastguard Worker         /**
69*90c8c64dSAndroid Build Coastguard Worker          *Build the notification's appearance.
70*90c8c64dSAndroid Build Coastguard Worker          * Set the large icon, which appears on the left of the notification. In this
71*90c8c64dSAndroid Build Coastguard Worker          * sample we'll set the large icon to be the same as our app icon. The app icon is a
72*90c8c64dSAndroid Build Coastguard Worker          * reasonable default if you don't have anything more compelling to use as an icon.
73*90c8c64dSAndroid Build Coastguard Worker          */
74*90c8c64dSAndroid Build Coastguard Worker         builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
75*90c8c64dSAndroid Build Coastguard Worker 
76*90c8c64dSAndroid Build Coastguard Worker         /**
77*90c8c64dSAndroid Build Coastguard Worker          * Set the text of the notification. This sample sets the three most commononly used
78*90c8c64dSAndroid Build Coastguard Worker          * text areas:
79*90c8c64dSAndroid Build Coastguard Worker          * 1. The content title, which appears in large type at the top of the notification
80*90c8c64dSAndroid Build Coastguard Worker          * 2. The content text, which appears in smaller text below the title
81*90c8c64dSAndroid Build Coastguard Worker          * 3. The subtext, which appears under the text on newer devices. Devices running
82*90c8c64dSAndroid Build Coastguard Worker          *    versions of Android prior to 4.2 will ignore this field, so don't use it for
83*90c8c64dSAndroid Build Coastguard Worker          *    anything vital!
84*90c8c64dSAndroid Build Coastguard Worker          */
85*90c8c64dSAndroid Build Coastguard Worker         builder.setContentTitle("BasicNotifications Sample");
86*90c8c64dSAndroid Build Coastguard Worker         builder.setContentText("Time to learn about notifications!");
87*90c8c64dSAndroid Build Coastguard Worker         builder.setSubText("Tap to view documentation about notifications.");
88*90c8c64dSAndroid Build Coastguard Worker 
89*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE (build_notification)
90*90c8c64dSAndroid Build Coastguard Worker 
91*90c8c64dSAndroid Build Coastguard Worker         // BEGIN_INCLUDE(send_notification)
92*90c8c64dSAndroid Build Coastguard Worker         /**
93*90c8c64dSAndroid Build Coastguard Worker          * Send the notification. This will immediately display the notification icon in the
94*90c8c64dSAndroid Build Coastguard Worker          * notification bar.
95*90c8c64dSAndroid Build Coastguard Worker          */
96*90c8c64dSAndroid Build Coastguard Worker         NotificationManager notificationManager = (NotificationManager) getSystemService(
97*90c8c64dSAndroid Build Coastguard Worker                 NOTIFICATION_SERVICE);
98*90c8c64dSAndroid Build Coastguard Worker         notificationManager.notify(NOTIFICATION_ID, builder.build());
99*90c8c64dSAndroid Build Coastguard Worker         // END_INCLUDE(send_notification)
100*90c8c64dSAndroid Build Coastguard Worker     }
101*90c8c64dSAndroid Build Coastguard Worker }
102