Android---通知栏之适配android8.0 且收到推送后显示到通知栏

郭霖:https://blog.csdn.net/guolin_blog/article/details/79854070


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的值才能在通知栏产生盖楼的效果

}

你可能感兴趣的:(Android---通知栏之适配android8.0 且收到推送后显示到通知栏)