美文网首页
关于安卓Notification8.0后,顶部通知

关于安卓Notification8.0后,顶部通知

作者: Pino | 来源:发表于2019-03-15 10:53 被阅读0次

从Android 8.0(API 26)开始,所有的Notification都要指定Channel(通道),对于每一个Channel你都可以单独去设置它;比如通知开关、提示音、是否震动或者是重要程度等;这样每个应用程序的通知在用户面前都是透明的。

1.遇到的问题
在设置弹出通知的时候,找了好多文章,都没有找到,后面才发现Channel(通道).
我们知道手机是可以设置,是否允许app推送的消息出现在顶部的.(有些厂商默认不打开,那就没有办法跳动或者振动了,需要用户手动设置)

// 消息推送
    public void sendTopNotice(int event_id,String title,String content,int isSuscces) {
        String CHANNEL_ID="channel_id";   //通道渠道id
        String  CHANEL_NAME="F_notice"; //通道渠道名称
        NotificationChannel channel = null;
        Notification notification = null;
        NotificationManager   notificationManager;
        notificationManager= (NotificationManager) UiUtil.getContext().getSystemService(NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            channel = new NotificationChannel(CHANNEL_ID, CHANEL_NAME, NotificationManager.IMPORTANCE_HIGH);

            notificationManager.createNotificationChannel(channel);
            Intent intent3 = new Intent(UiUtil.getContext(),isSuscces==1?Compass.class:UploadStatusActivity.class);
            PendingIntent pendingIntent3=PendingIntent.getActivity(getContext(),0,intent3,0);

            Intent XuanIntent=new Intent();
            XuanIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            XuanIntent.setClass(getContext(),MainActivity.class);
            //PendingIntent xuanpengdIntent=PendingIntent.getActivity(getContext(),0,XuanIntent,PendingIntent.FLAG_CANCEL_CURRENT);
            notification = new Notification.Builder(UiUtil.getContext(),CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.my_axon)
                    .setColor(Color.parseColor("#FEDA26"))
                    .setContentIntent(pendingIntent3)
                    .setAutoCancel(true)// 点击自动消失
                    .setLargeIcon(BitmapFactory.decodeResource(UiUtil.getContext().getResources(),isSuscces==1?R.mipmap.great:R.mipmap.icon_close))
                    .build();
        }
        notificationManager.notify(event_id,notification);
    }

区别就是在声明通道的时候,进行设置
默认是:
channel = new NotificationChannel(CHANNEL_ID, CHANEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
// 修改通知的重要程度,如果是很重要就会跳出通知 NotificationManager.IMPORTANCE_HIGH
channel = new NotificationChannel(CHANNEL_ID, CHANEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//其他属性就不多说了,跟原本的通知差不多

相关文章

网友评论

      本文标题:关于安卓Notification8.0后,顶部通知

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