版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dhd040805/article/details/79974029
android各版本对应的api情况,而android8.0以及之后的版本,notifycation为了更安全,又增加了channel的写法,代码如下
if (Build.VERSION.SDK_INT >= 26) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_HIGH);
if (manager != null) {
manager.createNotificationChannel(channel);
}
Notification.Builder builder = new Notification.Builder(context, "channel_id");
builder.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setContentTitle(msg.title)
.setContentText(msg.text)
.setAutoCancel(true);
return builder.build();
} else {
Notification.Builder builder = new Notification.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setContentTitle(msg.title)
.setContentText(msg.text)
.setAutoCancel(true);
return builder.build();
}
这样就完美解决能收到推送消息,但是通知栏不显示的问题