关于NotificationCompat.Builder()方法废弃解决方式
今天在学习系统通知时有一个问题就是NotificationCompat.Builder()不能只传入context还要加上一个String,但是随意加又不可以,在网上搜索后找到一篇文章有解决方法,下面是代码:
public class MainActivity extends AppCompatActivity {
private Button sendNotice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendNotice = 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(NOTIFICATION_SERVICE);
//高版本需要渠道
if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
//只在Android O之上需要渠道
NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH);
//如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
manager.createNotificationChannel(notificationChannel);
}
Notification notification = new NotificationCompat.Builder(MainActivity.this,"channelid1")
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.build();
manager.notify(1,notification);
break;
default:
break;
}
}
});
}
}
主要解决问题的就是IF语句中的代码