美文网首页
Android---通知栏之适配android8.0 且收到推送

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

作者: CWaitingforLove | 来源:发表于2020-08-17 13:46 被阅读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 且收到推送

          本文链接:https://www.haomeiwen.com/subject/vkguohtx.html