安卓消息横幅(悬浮)通知

1.notification的 important 优先级要提高到high或者high以上级别

2.修改代码编译后需要卸载清除缓存,安装才会生效

情况:代码写好了,运行却不能像微信来消息一样显示横幅,只能在通知栏上出个小图标,拉下通知栏才能看到。

按照教学过程,把channel对象的importance设置为NotificationManager.IMPORTANCE_DEFAULT,然后运行代码,不会在屏幕顶蹦出横幅。default不蹦横幅,high才蹦,问题就在这个规则上,并且这里有个坑,耗费了我一天时间:

一开始按教程发现不蹦横幅,然后就各种找原因,各种查,改各种set,也把这个importance改为了NotificationManager.IMPORTANCE_HIGH,按理说应该蹦了,但当时改了也不蹦,你没听错,改了也不蹦!为什么呢?我后面找了个能蹦出来的demo死磕,才发现从default改为high,要先卸载了app再安装才生效!这是什么鬼坑?然而从high改为default直接覆盖运行就生效了,立马不蹦了。。。

一般最好要设置

.setPriority(NotificationCompat.PRIORITY_MAX)

和 importance

int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
notificationManager.createNotificationChannel(channel);

注意 这里是卸载重新安装 adb uninstall 包名 再安卓

发送端的apk 不是接收展示端的。有时候这样干了。还不能立刻生效,很奇怪,多试几次

adb root
adb remount
adb shell pm clear 包名 
清除缓存关机重启再试下看看

这段代码是可以的

Intent broadcastIntent = new Intent(this, NotifyReceiver.class);
        broadcastIntent.setAction("com.jmc.otaui.broadcast.notify");
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_MUTABLE);

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
        notificationManager.createNotificationChannel(channel);

//        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
//                .setSmallIcon(R.drawable.ic_ota_icon)
//                .setContentTitle("系统更新")
//                .setContentText("您的车机系统有更新的版本,请及时更新。")
//                .setAutoCancel(true)
//                .setPriority(importance);
//
//        //builder.setContentIntent(pendingIntent);
        builder.setFullScreenIntent(pendingIntent, true);
//        notificationManager.notify(NOTIFICATION_ID, builder.build());

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_ota_icon)
                .setContentTitle("系统更新")
                .setContentText("您的车机系统有更新的版本,请及时更新。")
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_MAX);

        //builder.setContentIntent(pendingIntent);
//        builder.setFullScreenIntent(pendingIntent, true);
        notificationManager.notify(UUID.randomUUID().hashCode(), builder.build());

我测试的手机是小米8se,我不知道是miui给挖的坑还是谷歌给挖的,总之一直以来都觉得安卓体系太缺乏规范了
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/u010111268/article/details/113869193

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值