1.Notification簡介
通知是Android系統(tǒng)的一種特色的功能,當(dāng)某個app希望給用戶提示信息,但是該app又不在運行在前臺時,就可以利用通知。
發(fā)送一條通知后,手機上方的狀態(tài)欄就會顯示一個小圖標(biāo),下拉狀態(tài)欄,會顯示通知的具體信息。

實現(xiàn)代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.send_notice:
String CHANNEL_ID = "my_channel_01";
CharSequence name = getString(R.string.channel_01);
String description = "你好世界";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationManager manager = (NotificationManager) getSystemService
(NOTIFICATION_SERVICE);
//Android 8.0開始需要為通知設(shè)置channel
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance);
channel.setDescription(description);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400});
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(this)
//設(shè)置通知的標(biāo)題
.setContentTitle("This is content title")
// 設(shè)置通知的詳細(xì)信息
.setContentText("打到皇家馬德里")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher ))
.setChannelId(CHANNEL_ID)
.build();
manager.notify(1,notification );
break;
default:break;
}
}
}
**從Android O開始 每個通知必須為其設(shè)置對應(yīng)的channel **
channel的構(gòu)造函數(shù)有三個參數(shù) channel的id,name,和優(yōu)先級。 創(chuàng)建完channel后通過 NotificationManager的createNotificationChannel(channel)方法添加chanel,并在在notification里設(shè)置setChannelId。
通常 通知會在廣播接收器(broadcastReciver)或者服務(wù)(Server)中創(chuàng)建,不在活動中創(chuàng)建,因為app給我們發(fā)送通知的時候并不是運行在前臺。
通過點擊通知跳轉(zhuǎn)活動
如果我們想要用戶點擊app發(fā)出的通知有反應(yīng),比如進(jìn)入進(jìn)入一個頁面,這里就需要用到pendingIntent.
PendingIntent和Intent很類似,都可以用于啟動活動,啟動服務(wù),以及發(fā)送廣播。但是pendingIntent更傾向于某個合適的時機執(zhí)行某個動作,相當(dāng)于延遲類型的Intent.
//設(shè)置pendingIntent讓用戶點擊通知時跳轉(zhuǎn)活動。
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0 ,intent ,0 );
獲取PendingIntent實例可以通過PendingIntent的getActivity(),getBroadCast(),getService()方法
第一個參數(shù)是Context,第二個參數(shù)一般為0,第三個是Intent對象,第四個是確定pendingIntent的行為,有FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCLE_CURRENT,FLAG_UPDATE_CURRENT四種值,一般傳入0
最后通過NotificationCompat的setContentIntent(pi)加上點擊功能。
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("打到皇家馬德里")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher ))
.setChannelId(CHANNEL_ID)
.setContentIntent(pi)
.build();

設(shè)置當(dāng)用戶點擊完通知操作后,通知圖標(biāo)就消失
第一種方法

當(dāng)點擊了通知后,通知會自動取消掉。
第二種方法,在跳轉(zhuǎn)的活動里設(shè)置
public class NotificationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
}
}
這里manager.cancel()傳入的參數(shù)是給每條參數(shù)指定的id.

2.Notification進(jìn)階技巧

需要聲明權(quán)限
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

也可以直接默認(rèn)設(shè)置

3.Notification的高級技巧
設(shè)置通知欄詳情的長文本內(nèi)容


顯示大圖片

設(shè)置通知的重要程度
- PRIORITY_DEFAULT 默認(rèn)的重要程度
- PRIORITY_MIN 表示最低的重要程度,系統(tǒng)可能只會在特定的場景才會顯示這條通知
- PRIORITY_LOW 表示較低的重要程度
- PRIORITY_HIGH 表示較高的重要程度,系統(tǒng)可能會將這類通知放大,或改變其顯示順序,比較靠前的位置
- PRIORITY_MAX 最高的重要程度,必須讓用戶立刻看到,甚至做出相應(yīng)的操作。
