private void initView() {
Notification notification;
//高版本的模拟器或手机还需要开启渠道才能显示通知
NotificationChannel notificationChannel=null;
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
notificationChannel=new NotificationChannel("001","channel_name",NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"001");
//实例化一个意图,当点击通知时会跳转执行这个意图
Intent intent = new Intent(this, MainActivity.class);
//将意图进行封装
PendingIntent pendingIntent= PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//设置Notification的点击之后执行的意图
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("酷我音乐");
builder.setContentText("正在播放的音乐:两只老虎");
notification = builder.build();
notificationManager.notify(0,notification);
//startForeground(0, builder.build());
}
网友评论