如下是使用示例代碼:android
//建立一個NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns); //定義Notification的各類屬性 int icon = R.drawable.icon; //通知圖標 CharSequence tickerText = "Hello"; //狀態欄顯示的通知文本提示 long when = System.currentTimeMillis(); //通知產生的時間,會在通知信息裏顯示 //用上面的屬性初始化Nofification Notification notification = new Notification(icon,tickerText,when); /* * 添加聲音 * notification.defaults |=Notification.DEFAULT_SOUND; * 或者使用如下幾種方式 * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); * 若是想要讓聲音持續重複直到用戶對通知作出反應,則能夠在notification的flags字段增長"FLAG_INSISTENT" * 若是notification的defaults字段包括了"DEFAULT_SOUND"屬性,則這個屬性將覆蓋sound字段中定義的聲音 */ /* * 添加振動 * notification.defaults |= Notification.DEFAULT_VIBRATE; * 或者能夠定義本身的振動模式: * long[] vibrate = {0,100,200,300}; //0毫秒後開始振動,振動100毫秒後中止,再過200毫秒後再次振動300毫秒 * notification.vibrate = vibrate; * long數組能夠定義成想要的任何長度 * 若是notification的defaults字段包括了"DEFAULT_VIBRATE",則這個屬性將覆蓋vibrate字段中定義的振動 */ /* * 添加LED燈提醒 * notification.defaults |= Notification.DEFAULT_LIGHTS; * 或者能夠本身的LED提醒模式: * notification.ledARGB = 0xff00ff00; * notification.ledOnMS = 300; //亮的時間 * notification.ledOffMS = 1000; //滅的時間 * notification.flags |= Notification.FLAG_SHOW_LIGHTS; */ /* * 更多的特徵屬性 * notification.flags |= FLAG_AUTO_CANCEL; //在通知欄上點擊此通知後自動清除此通知 * notification.flags |= FLAG_INSISTENT; //重複發出聲音,直到用戶響應此通知 * notification.flags |= FLAG_ONGOING_EVENT; //將此通知放到通知欄的"Ongoing"即"正在運行"組中 * notification.flags |= FLAG_NO_CLEAR; //代表在點擊了通知欄中的"清除通知"後,此通知不清除, * //常常與FLAG_ONGOING_EVENT一塊兒使用 * notification.number = 1; //number字段表示此通知表明的當前事件數量,它將覆蓋在狀態欄圖標的頂部 * //若是要使用此字段,必須從1開始 * notification.iconLevel = ; // */ //設置通知的事件消息 Context context = getApplicationContext(); //上下文 CharSequence contentTitle = "My Notification"; //通知欄標題 CharSequence contentText = "Hello World!"; //通知欄內容 Intent notificationIntent = new Intent(this,Main.class); //點擊該通知後要跳轉的Activity PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //把Notification傳遞給NotificationManager mNotificationManager.notify(0,notification);
(2)、使用自定義的Notification若是想要更新一個通知,只須要在設置好notification以後,再次調用 setLatestEventInfo(),而後從新發送一次通知便可,即再次調用notify()。數組
要建立一個自定義的Notification,可使用RemoteViews。要定義本身的擴展消息,首先要初始化一個RemoteViews對象,而後將它傳遞給Notification的contentView字段,再把PendingIntent傳遞給contentIntent字段。如下示例代碼是完整步驟:佈局
//一、建立一個自定義的消息佈局 view.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#000" /> </LinearLayout>
//二、在程序代碼中使用RemoteViews的方法來定義image和text。而後把RemoteViews對象傳到contentView字段 RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view); contentView.setImageViewResource(R.id.image,R.drawable.icon); contentView.setTextViewText(R.id.text,」Hello,this message is in a custom expanded view」); notification.contentView = contentView; //三、爲Notification的contentIntent字段定義一個Intent(注意,使用自定義View不須要setLatestEventInfo()方法) Intent notificationIntent = new Intent(this,Main.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.contentIntent = contentIntent; //四、發送通知 mNotificationManager.notify(2,notification); //如下是所有示例代碼 //建立一個NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns); //定義Notification的各類屬性 int icon = R.drawable.icon; //通知圖標 CharSequence tickerText = "Hello"; //狀態欄顯示的通知文本提示 long when = System.currentTimeMillis(); //通知產生的時間,會在通知信息裏顯示 //用上面的屬性初始化Nofification Notification notification = new Notification(icon,tickerText,when); RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view); contentView.setImageViewResource(R.id.image, R.drawable.iconempty); contentView.setTextViewText(R.id.text, "Hello,this is JC"); notification.contentView = contentView; Intent notificationIntent = new Intent(this,Main.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0); notification.contentIntent = contentIntent; //把Notification傳遞給NotificationManager mNotificationManager.notify(0,notification);