
情境
如果你想要讓左上角能夠彈跳出一個訊息 
只要利用 Notification 這個工具就可以完成
完整程式碼
你可以到 GitHub 上面觀看或下載完成程式碼
程式碼說明
首先定義一個 Button 在 main.xml 裡面 
這個按鈕是用來當我們按下去的時候 
左上角就會跳出一個訊息
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Push me send Message."
        android:id="@+id/send_notify_btn"/>
</RelativeLayout>
接著主程式定義了一個函式 init() 
用來初始化一些必要的物件 
首先我們必須定義一個訊息管理者 
這個管理者必須跟系統要 
這樣我們就可以取得一個訊息管理者的物件
NotificationManager notificationManger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
宣告一個 Intent 物件 
當我們按下 Notification 的時候 
它會帶我們到另外一個Activity
intent= new Intent();
intent.setClass(NotificationDemoActivity.this, HandleNotification.class);
這時候我們就必須寫一個 Activity 來接這個訊息 
當它被 Click 的時候 
這個 Activity 很單純 
只有一個 TextView 顯示一個簡單的一段文字
public class HandleNotification extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handle_notification);
    }
}
notification.xml layout 部分
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="personal.givemepass.notificationdemo.HandleNotification">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Notification."
        />
</RelativeLayout>
記得要在 AndroidManifest.xml 加上這個 Activity  
不然會跳出錯誤訊息。
<activity android:name=".HandleNotification"></activity>
回到剛剛的 init 方法
PendingIntent pendingIntent = PendingIntent.getActivity(this,NOTIFICATION_ID,intent,0);
我們宣告一個 PendingIntent 的物件 
這個類別的物件用途很廣 
跟 Intent 不太一樣 
它執行完並不會馬上啟動 
你可以想像成一個延遲啟動的 Intent 
當使用者點訊息的時候 
它才會跳到別的 Activity 
但是你會說 
我利用 Intent 也辦的到啊 
不同的地方是 
Intent 是我們本身的 Activity 去啟動 Intent 讓它跳到別的 Activity 
而 PendingIntent 則是把 Intent 包起來 
丟給別人當有需要的時候 
別人才會去啟動 Intent 
簡單來說 
就是我就不幫你轉到其他的 Activity 
但是我幫你包好了 
如果你想跳到別的 Activity 的話 
你只要執行 PendingIntent 就可以了
接著我們定義一個訊息 
第一個參數給它一個圖示 
使用內建的圖示 
再來就是訊息出現在左上角時的標題 
內容跟指定的 PendingIntent
notification = new Notification.Builder(this)
    .setSmallIcon(android.R.drawable.sym_def_app_icon)
    .setContentTitle("Hi")
    .setContentText("Nice to meet you.")
    .setContentIntent(pendingIntent)
    .build(); // available from API level 11 and onwards
一切都準備好了以後 
當我們按下 Button 的時候 
就會告知系統 
我要求傳送一個訊息
sendNotification.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {
        notificationManger.notify(0, notification);
    }
});
如果你想要讓使用者點完 Notification 就清除可以加上這一行
notification.flags = Notification.FLAG_AUTO_CANCEL;
這樣一來就大功告成。 
 
 
 
 這樣就是一個簡單的 Notification 範例了