美文网首页
Android O notification用法

Android O notification用法

作者: light2131 | 来源:发表于2018-08-10 12:20 被阅读0次

    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;

    }

    }

    });

    }

    相关文章

      网友评论

          本文标题:Android O notification用法

          本文链接:https://www.haomeiwen.com/subject/iaykbftx.html