android | 使用系统通知服务
小细节:8.0以上的安卓版本需要先注册通道
贴代码:
注册通知:
// init notification manager
nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// over 8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel nChannel = new NotificationChannel("Mz1notice", "测试通知", NotificationManager.IMPORTANCE_HIGH);
nManager.createNotificationChannel(nChannel);
}
notice = new NotificationCompat.Builder(this, "Mz1notice")
.setContentTitle("标题")
.setContentText("文本")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon))
.setSmallIcon(R.drawable.ic_nitice)
.setAutoCancel(true)
.build(); // create notice
发送通知:
nManager.notify(1, notice);