Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 import java.util.Random;
     20 
     21 import com.example.android.apis.R;
     22 
     23 import android.app.Activity;
     24 import android.app.Notification;
     25 import android.app.NotificationManager;
     26 import android.app.PendingIntent;
     27 import android.content.ComponentName;
     28 import android.content.Context;
     29 import android.content.Intent;
     30 import android.os.Bundle;
     31 import android.view.View;
     32 import android.widget.Button;
     33 
     34 /**
     35  * UI for posting an example notification.
     36  */
     37 public class IncomingMessage extends Activity {
     38     @Override
     39     protected void onCreate(Bundle savedInstanceState) {
     40         super.onCreate(savedInstanceState);
     41 
     42         setContentView(R.layout.incoming_message);
     43 
     44         Button button = (Button) findViewById(R.id.notify_app);
     45         button.setOnClickListener(new Button.OnClickListener() {
     46                 public void onClick(View v) {
     47                     showAppNotification();
     48                 }
     49             });
     50 
     51         button = (Button) findViewById(R.id.notify_interstitial);
     52         button.setOnClickListener(new Button.OnClickListener() {
     53                 public void onClick(View v) {
     54                     showInterstitialNotification();
     55                 }
     56             });
     57     }
     58 
     59 //BEGIN_INCLUDE(app_notification)
     60 //BEGIN_INCLUDE(intent_array)
     61     /**
     62      * This method creates an array of Intent objects representing the
     63      * activity stack for the incoming message details state that the
     64      * application should be in when launching it from a notification.
     65      */
     66     static Intent[] makeMessageIntentStack(Context context, CharSequence from,
     67             CharSequence msg) {
     68         // A typical convention for notifications is to launch the user deeply
     69         // into an application representing the data in the notification; to
     70         // accomplish this, we can build an array of intents to insert the back
     71         // stack stack history above the item being displayed.
     72         Intent[] intents = new Intent[4];
     73 
     74         // First: root activity of ApiDemos.
     75         // This is a convenient way to make the proper Intent to launch and
     76         // reset an application's task.
     77         intents[0] = Intent.makeRestartActivityTask(new ComponentName(context,
     78                 com.example.android.apis.ApiDemos.class));
     79 
     80         // "App"
     81         intents[1] = new Intent(context, com.example.android.apis.ApiDemos.class);
     82         intents[1].putExtra("com.example.android.apis.Path", "App");
     83         // "App/Notification"
     84         intents[2] = new Intent(context, com.example.android.apis.ApiDemos.class);
     85         intents[2].putExtra("com.example.android.apis.Path", "App/Notification");
     86 
     87         // Now the activity to display to the user.  Also fill in the data it
     88         // should display.
     89         intents[3] = new Intent(context, IncomingMessageView.class);
     90         intents[3].putExtra(IncomingMessageView.KEY_FROM, from);
     91         intents[3].putExtra(IncomingMessageView.KEY_MESSAGE, msg);
     92 
     93         return intents;
     94     }
     95 //END_INCLUDE(intent_array)
     96 
     97     /**
     98      * The notification is the icon and associated expanded entry in the
     99      * status bar.
    100      */
    101     void showAppNotification() {
    102         // look up the notification manager service
    103         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    104 
    105         // The details of our fake message
    106         CharSequence from = "Joe";
    107         CharSequence message;
    108         switch ((new Random().nextInt()) % 3) {
    109             case 0: message = "r u hungry?  i am starved"; break;
    110             case 1: message = "im nearby u"; break;
    111             default: message = "kthx. meet u for dinner. cul8r"; break;
    112         }
    113 
    114         // The PendingIntent to launch our activity if the user selects this
    115         // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there
    116         // is already an active matching pending intent, cancel it and replace
    117         // it with the new array of Intents.
    118         PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
    119                 makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT);
    120 
    121         // The ticker text, this uses a formatted string so our message could be localized
    122         String tickerText = getString(R.string.imcoming_message_ticker_text, message);
    123 
    124         // Set the info for the views that show in the notification panel.
    125         Notification.Builder notifBuilder = new Notification.Builder(this)
    126                 .setSmallIcon(R.drawable.stat_sample)  // the status icon
    127                 .setTicker(tickerText)  // the status text
    128                 .setWhen(System.currentTimeMillis())  // the time stamp
    129                 .setContentTitle(from)  // the label of the entry
    130                 .setContentText(message)  // the contents of the entry
    131                 .setContentIntent(contentIntent);  // The intent to send when the entry is clicked
    132 
    133         // We'll have this notification do the default sound, vibration, and led.
    134         // Note that if you want any of these behaviors, you should always have
    135         // a preference for the user to turn them off.
    136         notifBuilder.setDefaults(Notification.DEFAULT_ALL);
    137 
    138         // Note that we use R.layout.incoming_message_panel as the ID for
    139         // the notification.  It could be any integer you want, but we use
    140         // the convention of using a resource id for a string related to
    141         // the notification.  It will always be a unique number within your
    142         // application.
    143         nm.notify(R.string.imcoming_message_ticker_text, notifBuilder.build());
    144     }
    145 //END_INCLUDE(app_notification)
    146 
    147 //BEGIN_INCLUDE(interstitial_notification)
    148     /**
    149      * The notification is the icon and associated expanded entry in the
    150      * status bar.
    151      */
    152     void showInterstitialNotification() {
    153         // look up the notification manager service
    154         NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    155 
    156         // The details of our fake message
    157         CharSequence from = "Dianne";
    158         CharSequence message;
    159         switch ((new Random().nextInt()) % 3) {
    160             case 0: message = "i am ready for some dinner"; break;
    161             case 1: message = "how about thai down the block?"; break;
    162             default: message = "meet u soon. dont b late!"; break;
    163         }
    164 
    165         // The PendingIntent to launch our activity if the user selects this
    166         // notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there
    167         // is already an active matching pending intent, cancel it and replace
    168         // it with the new Intent.
    169         Intent intent = new Intent(this, IncomingMessageInterstitial.class);
    170         intent.putExtra(IncomingMessageView.KEY_FROM, from);
    171         intent.putExtra(IncomingMessageView.KEY_MESSAGE, message);
    172         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    173         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    174                 intent, PendingIntent.FLAG_CANCEL_CURRENT);
    175 
    176         // The ticker text, this uses a formatted string so our message could be localized
    177         String tickerText = getString(R.string.imcoming_message_ticker_text, message);
    178 
    179         // Set the info for the views that show in the notification panel.
    180         Notification.Builder notifBuilder = new Notification.Builder(this)
    181                 .setSmallIcon(R.drawable.stat_sample)  // the status icon
    182                 .setTicker(tickerText)  // the status text
    183                 .setWhen(System.currentTimeMillis())  // the time stamp
    184                 .setContentTitle(from)  // the label of the entry
    185                 .setContentText(message)  // the contents of the entry
    186                 .setContentIntent(contentIntent);  // The intent to send when the entry is clicked
    187 
    188         // We'll have this notification do the default sound, vibration, and led.
    189         // Note that if you want any of these behaviors, you should always have
    190         // a preference for the user to turn them off.
    191         notifBuilder.setDefaults(Notification.DEFAULT_ALL);
    192 
    193         // Note that we use R.layout.incoming_message_panel as the ID for
    194         // the notification.  It could be any integer you want, but we use
    195         // the convention of using a resource id for a string related to
    196         // the notification.  It will always be a unique number within your
    197         // application.
    198         nm.notify(R.string.imcoming_message_ticker_text, notifBuilder.build());
    199     }
    200 //END_INCLUDE(interstitial_notification)
    201 }
    202