Android Notification 的四種使用方式
實現通知步驟
一般實現通知需要如下步驟:
1.獲取 NotificationManager 例項管理通知;
2.例項 Notification 物件;
3.管理事件 Intent;
4.傳送通知。
注:如不需在通知出現時,點選時有事件執行,步驟3可以忽略。1. 普通通知
獲取 NotificationManager 例項:
NotificationManager 對通知進行管理,呼叫 Context 的 getSystemService() 方法獲取。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
例項 Notification 物件:
Notification notification= new NotificationCompat.Builder(Context).build();
此時只建立了一個空的 Notification 物件,沒有實際作用,可以在build() 方法之前連綴任意多的方法設定 Notification 物件。現在來設定一些基本設定
Notification notification = new NotificationCompat.Builder(Context)
.setContentText("通知內容")
.setContentTitle ("通知標題")
.setSmallIcon(android.R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_round))
.setWhen(System.currentTimeMillis())
.build();
以上設定了五個方法:
setSmallIcon() 用於設定通知的小圖示,只能使用純 alpha 圖層的圖片進行設定,小圖示會顯示在系統狀態列上。(alpha 圖層的圖片你不知道沒關係,UI 會知道的,哈哈,這個我也不知P出來,這裡我只是暫時用預設圖示代替)
setLargeIcon()
setWhen() 指定通知被建立的時間,以毫秒為單位,下拉通知後會將時間顯示在相應的通知上。
現在可以傳送一個基本通知了
manager.notify(1,notification);
notify() 方法接收兩個引數,引數一 id 指定通知的 id,要保證每個通知的 id 是不同的;引數二 Notification 物件,傳入之前建立好的即可。
當次通知出現時,點選是不會有任何效果的,這是因為沒有關聯 Intent,沒有還不簡單,給他新增一個就可以了,此時會用到 PendingIntent ,PendingIntent 的獲取可以根據需求選擇
getActivity(),getBroadcast(),getService() 等靜態方法來獲取。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(Content)
.setContentIntent(pi)
.build();
呼叫 setContentIntent() 方法,傳入 PendingIntent 例項即可,當點選同時會開啟瀏覽器進入百度主頁。
我們現在在完善下 Notification
Notification notification = new NotificationCompat.Builder(Context)
...
.setAutoCancel(true)//點選通知頭自動取消
.setDefaults(NotificationCompat.DEFAULT_ALL)//設定鈴聲及震動效果等
.build();
控制手機震動等還需要相應的許可權
<uses-permission android:name="android.permission.VIBRATE"/>
也可對鈴聲,LED燈,震動等分別進行設定
setSound()//鈴聲
setLights()//LED燈
setVibrate()//震動
- 2.懸掛式通知
//在 build()之前設定 .setFullScreenIntent()
Notification builder = new NotificationCompat.Builder(Context);
Notification notify = builder.setSmallIcon(R.mipmap.ic_launcher_round)
.setPriority(Notification.PRIORITY_DEFAULT) //通知的優先順序
.setCategory(Notification.CATEGORY_MESSAGE) //通知的型別
.setContentTitle("通知")
.setAutoCancel(true)
.setContentIntent(pi)
.setContentText("Heads - Up Notification on Android 5.0")
.setFullScreenIntent(pi, true) //不設定此項不會懸掛,false 不會出現懸掛
.build();
setPriority() 方法共有5個等級:
1. PRIORITY_MIN - 最低級別(-2);
2. PRIORITY_LOW - 較低級別(-1);
3. PRIORITY_DEFAULT - 預設級別(0);
4. PRIORITY_HIGH - 較高級別(1);
5. PRIORITY_MAX - 最高級別(2);
當發出此型別的通知時,通知會以懸掛的方法顯示在螢幕上。
- 3.摺疊式通知
摺疊式同時需要藉助 RemoteViews 來實現
Notification builder = new NotificationCompat.Builder(Context);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.sina.com"));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
// 未下拉的樣式 R.layout.collapsed
RemoteViews collapsed = new RemoteViews(getPackageName(), R.layout.collapsed);
collapsed.setTextViewText(R.id.collapsed_text, "關閉狀態");
//下拉後的樣式R.layout.show
RemoteViews show = new RemoteViews(getPackageName(), R.layout.show);
Notification notify = builder.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.setContentText("新浪微博")
.setCustomContentView(collapsed)//下拉前
.setCustomBigContentView(show)//下拉後
.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, notify);
- 4.鎖屏通知
Android 5.0(API level 21)開始,通知可以顯示在鎖屏上,通過設定選擇是否允許敏感的通知內容顯示在安全的鎖屏上。
//通過 setVisibility() 方法設定即可
...
.setVisibility(VISIBILITY_PUBLIC)
.build();
setVisibility() 方法共有三個選值:
1.VISIBILITY_PRIVATE : 顯示基本資訊,如通知的圖示,但隱藏通知的全部內容;
2.VISIBILITY_PUBLIC : 顯示通知的全部內容;
3.VISIBILITY_SECRET : 不顯示任何內容,包括圖示。
- 總結
以上列舉了通知的四種用法,關於 Notification 還有很多設定在這裡沒有寫,大家可以自己去挖掘,去嘗試。文章如有不足之處,歡迎大家之處,共同進步!