public void showNotifictionIcon(Context context, PushBean pushBean) { mNotificationId = hashCode(); LogUtils.loge("mNotificationId=" + mNotificationId); Intent broadcastIntent = new Intent(context, NotificationReceiver.class); broadcastIntent.setAction(Intent.ACTION_VIEW); broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); broadcastIntent.putExtra(AppConstant.PUSH_TYPE, pushBean); PendingIntent pendingIntent = PendingIntent. getBroadcast(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder.setSmallIcon(R.drawable.logo); } else { builder.setSmallIcon(R.drawable.logo); } builder.setContentTitle(pushBean.getTitle()) .setTicker(pushBean.getDescription()) .setContentIntent(pendingIntent) .setChannelId("zhuhai")//适配8.0 .setAutoCancel(true)//用户点击就自动消失 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo)); //适配8.0 NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if(Build.VERSION.SDK_INT >= 26){ NotificationChannel channel = new NotificationChannel("zhuhai", getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH); builder.setChannelId("zhuhai"); manager.createNotificationChannel(channel); } manager.notify(mNotificationId, builder.build());//每次改变mNotificationId的值才能在通知栏产生盖楼的效果 }
收到推送后显示到通知栏且适配android8.0
最新推荐文章于 2021-05-26 22:31:16 发布