android8.0以上版本區別于android7.0通知的差距是,以上的版本必須添加渠道NotificationChannel
創建通知之前需要對android版本進行一個判斷
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
activity_main.xml代碼里僅有一個Button用于回應通知,代碼不再展示
MainActivity.java代碼如下:
public class MainActivity extends AppCompatActivity {
private Button GetNotification;
private static final int ID = 1;
private static final String CHANNELID ="1";
private static final String CHANNELNAME = "channel1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetNotification = (Button) findViewById(R.id.GetNotification);
GetNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// manager.cancel(1);
//安卓8.0以上彈出通知需要添加渠道NotificationChannel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
//創建渠道
/**
* importance:用于表示渠道的重要程度,這可以控制發布到此頻道的中斷通知的方式,
* 有以下6種重要性,是NotificationManager的靜態常量,依次遞增:
* IMPORTANCE_UNSPECIFIED(值為-1)意味著用戶沒有表達重要性的價值,此值用于保留偏好設定,不應與實際通知關聯,
* IMPORTANCE_NONE(值為0)不重要的通知:不會在陰影中顯示,
* IMPORTANCE_MIN(值為1)最低通知重要性:只顯示在陰影下,低于折疊,這不應該與Service.startForeground一起使用,因為前臺服務應該是用戶關心的事情,所以它沒有語意意義來將其通知標記為最低重要性,如果您從Android版本O開始執行此操作,系統將顯示有關您的應用在后臺運行的更高優先級通知,
* IMPORTANCE_LOW(值為2)低通知重要性:無處不在,但不侵入視覺,
* IMPORTANCE_DEFAULT (值為3):默認通知重要性:隨處顯示,產生噪音,但不會在視覺上侵入,
* IMPORTANCE_HIGH(值為4)更高的通知重要性:隨處顯示,造成噪音和窺視,可以使用全屏的Intent,
*/
NotificationChannel channel = new NotificationChannel(CHANNELID,CHANNELNAME,NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);//開啟渠道
Intent intent = new Intent(MainActivity.this,notification.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,CHANNELID);
builder .setContentTitle("Title")//通知標題
.setContentText("ContentText")//通知內容
.setWhen(System.currentTimeMillis())//通知顯示時間
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.smile)
.setAutoCancel(true)//點擊通知取消
//.setSound()
//第一個引數為手機靜止時間,第二個引數為手機震動時間,周而復始
.setVibrate(new long[] {0,1000,1000,1000})//手機震動
//第一個引數為LED等顏色,第二個引數為亮的時長,第三個引數為滅的時長
.setLights(Color.BLUE,1000,1000)
/**表示通知的重要程度
* RIORITY_DEFAULT
* RIORITY_MIN
* RIORITY_LOW
* RIORITY_HIGE
* RIORITY_MAX
**/
.setPriority(NotificationCompat.PRIORITY_MAX)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.smile)).build();
manager.notify(1,builder.build());
} else{
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("Title")
.setContentText("ContentText")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.smile)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.smile))
.build();
manager.notify(1,notification);
}
}
});
}
}
我們還需要創建一個活動notification,用于回應點擊通知后執行的操作,此處就使用了一個TextView顯示一段文本
如果要設定手機震動還得在清單檔案中宣告權限
<uses-permission android:name="android.permission.VIBRATE"/>
效果圖如下:
我們點擊按鈕,通知出現

點擊通知跳轉到notification活動

因為我們設定了 .setAutoCancel(true),所有點擊通知后,通知消失

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/261760.html
標籤:其他
