Android O notification用法

2018-08-10  本文已影响0人  light2131

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;

}

}

});

}

上一篇下一篇

猜你喜欢

热点阅读