Android O notification用法

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;

}

}

});

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容