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

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。