protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button)findViewById(R.id.send_notice);
sendNotice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.send_notice:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建通知渠道
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel =new NotificationChannel(getString(R.string.app_name), getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW);
mChannel.setDescription("test channel");
mChannel.setShowBadge(false);
manager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder =new NotificationCompat.Builder(MainActivity.this,getString(R.string.app_name));
builder.setLargeIcon(BitmapFactory.decodeResource(
getResources(), R.mipmap.ic_launcher)).setContentTitle("This is content title")
.setContentText("This is text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher);
manager.notify((int) System.currentTimeMillis(), builder.build());
break;
default:
break;
}
}
});
}
网友评论