版权声明:转载请注明原文链接。 https://blog.csdn.net/ting1406525501/article/details/81298404
Android8.0通知栏适配
String channelId="channel_id";
NotificationManager manager=getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=BUILD.VERSION_CODES.O){
Charsequence name="channel_name";
String description="channel_description";
int importance=NotificationManager.IMPORTANCE_DEFAULT;//根据项目的自行选择
NotificationChannel channel=new NotificationChannel(channelId,name,importance);
channel.setDescription(description);
manager.createNotificationChannel(channel);
}
Intent intent=new Intent(MainActivity.this,NotificationActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(Mainactivity.this,0,intent,0);
Notification notification=new NotificationCompat.Builder(MainActivity.this,channelId)
.setContentTitle("This is a title")
.setContentText("This is a text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher)
.setChannelId(channelId)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to build notifications,send and sync data,and use voice actions.Get the official Adnroid IDE and developers tools to build app for Android."))
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.add_data-img)))
.setAutoCancel(true)
.build();
manager.notify(234,notification);
即使是在服务中,也需要有之前的步骤。