1借尿、創(chuàng)建 Notification NotificationManager
private NotificationManager manager;
private Notification notification;
2民鼓、設(shè)置通知屬性
public void tz(String strtitlt, String strcontent){
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ //版本大于等于 安卓8.0
NotificationChannel channel = new NotificationChannel("leo", "測試通知", NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
// 設(shè)置取消后的動作
Intent intent = new Intent();
intent.setClass(this, OtherActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
//初始化 notification
notification = new NotificationCompat.Builder(this,"leo")
.setContentTitle(strtitlt) //設(shè)置標(biāo)題
.setContentText(strcontent) //設(shè)置通知文字
.setSmallIcon(R.drawable.ic_launcher_foreground) //設(shè)置左邊的小圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground)) //設(shè)置大圖標(biāo)
.setColor(Color.parseColor("#ff0000"))
.setContentIntent(pendingIntent) // 設(shè)置點擊通知之后 進入相關(guān)頁面(此處進入NotificationActivity類,執(zhí)行oncreat方法打印日志)
.setAutoCancel(true) //設(shè)置點擊通知后 通知通知欄不顯示
.build();
}
3霹琼、設(shè)置發(fā)送標(biāo)題與內(nèi)容
String title = "通知標(biāo)題";
String content = "通知內(nèi)容";
4务傲、加載設(shè)置與發(fā)送通知
tz(title, content);
manager.notify(1,notification);
5、完整MainActivity內(nèi)容
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String title = "通知標(biāo)題";
String content = "通知內(nèi)容";
tz(title, content);
manager.notify(1,notification);
}
//開啟通知按鈕 點擊事件
public void setNotification(View view) {
manager.notify(1,notification); //點擊事件啟動喚醒manager枣申,用于通知notification
}
//關(guān)閉通知按鈕 點擊事件
public void cacelNotification(View view) {
manager.cancel(1); //取消通知
}
public void tz(String strtitlt, String strcontent){
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ //版本大于等于 安卓8.0
NotificationChannel channel = new NotificationChannel("leo", "測試通知", NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}
// 設(shè)置取消后的動作
Intent intent = new Intent();
intent.setClass(this, OtherActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
//初始化 notification
notification = new NotificationCompat.Builder(this,"leo")
.setContentTitle(strtitlt) //設(shè)置標(biāo)題
.setContentText(strcontent) //設(shè)置通知文字
.setSmallIcon(R.drawable.ic_launcher_foreground) //設(shè)置左邊的小圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground)) //設(shè)置大圖標(biāo)
.setColor(Color.parseColor("#ff0000"))
.setContentIntent(pendingIntent) // 設(shè)置點擊通知之后 進入相關(guān)頁面(此處進入OtherActivity類)
.setAutoCancel(true) //設(shè)置點擊通知后 通知通知欄不顯示
.build();
}
}