【问题标题】:Heads up notification not showing in android O and higher提醒通知未在 android O 及更高版本中显示
【发布时间】:2021-03-23 14:25:28
【问题描述】:

我确实认真地尝试了每一种方法和每一个 sn-p,但我仍然无法在中国品牌设备上显示提示通知。

所以昨天我想为什么不再试一次,但毕竟我仍然无法显示提示通知,直到我手动将应用程序转到设置并授予应用程序的浮动权限。

现在你们中的大多数人可能会说为什么不将用户导航到设置,当他/她第一次打开应用程序时,但没有人喜欢即使有其他应用程序(我不是在谈论像 WhatsApp 这样的白名单应用程序)有 10K 的下载量可以显示抬头通知

这是我的代码,顺便说一句,我尝试设置声音、振动和灯光,但仍然没有显示抬头,是的,我在每次构建后都会卸载我的应用

    public void showNotification(View v){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
            nc.enableLights(true);
            nc.setLightColor(Color.BLUE);
            nc.enableVibration(true);
            nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager nm = getSystemService(NotificationManager.class);
            nm.createNotificationChannel(nc);
        }

        Notification.Builder notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification.Builder(this,"n")
                    .setContentTitle("Pop up notification")
                    .setSmallIcon(R.drawable.ic_launcher_background);
        }else{
            notification = new Notification.Builder(this)
                    .setContentTitle("Pop up notification")
//                    .setPriority(Notification.PRIORITY_MAX)
                    .setSmallIcon(R.drawable.ic_launcher_background);
        }
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1,notification.build());
    }

【问题讨论】:

    标签: java android notifications redmi-device


    【解决方案1】:

    我有两个解决方案。试一试,看看有没有用。

    1. 你的实际上是这样的

       public void showNotification(){
      
       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
           NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
           nc.enableLights(true);
           nc.setLightColor(Color.BLUE);
           nc.enableVibration(true);
           nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
           NotificationManager nm = getSystemService(NotificationManager.class);
           nm.createNotificationChannel(nc);
       }
      
       Notification.Builder notification = null;
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
           notification = new Notification.Builder(this,"n")
                   .setContentTitle("Pop up notification")
                   .setSmallIcon(R.drawable.alex);
       }else{
           notification = new Notification.Builder(this)
                   .setContentTitle("Pop up notification")
                   //.setPriority(Notification.PRIORITY_MAX)
                   .setSmallIcon(R.drawable.alex);
       }
       NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
       notificationManager.notify(1,notification.build());
      
    2. 我的 notifyUser("订单发货", "您的订单已到"):

       public void notifyUser(String title, String text){
       NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify_001");
       Intent intent = new Intent(this, ActivityToOpenWhenNotificationIsTapped.class);
       intent.setAction("android.intent.action.MAIN");
       intent.addCategory("android.intent.category.LAUNCHER");
       intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
       intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
       PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
       NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
       bigTextStyle.setBigContentTitle(title);
       bigTextStyle.bigText(text);
       bigTextStyle.setSummaryText("Big summary text. Comment if you want.");
       builder.setAutoCancel(true);
       builder.setContentIntent(activity);
       builder.setContentTitle(title);
       builder.setContentText(text);
       builder.setPriority(1);
       builder.setSmallIcon(R.drawable.alex);
       builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.alex));
       builder.setStyle(bigTextStyle);
       builder.setAutoCancel(true);
       builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
       builder.setGroup("Pixels");
       Notification build = builder.build();
       build.flags |= 16;
       NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       if (Build.VERSION.SDK_INT >= 26) {
           String str3 = "Pixels_V1.0";
           notificationManager.createNotificationChannel(new NotificationChannel(str3, "Pixels Notification", NotificationManager.IMPORTANCE_DEFAULT));
           builder.setChannelId(str3);
       }
       int NOTIFICATION_ID =  new Random(System.currentTimeMillis()).nextInt(1000);
       notificationManager.notify(NOTIFICATION_ID, builder.build());
       }
      

    【讨论】:

    • sill 没有显示弹出,我复制了你的整个代码:(
    • 在另一台设备上测试它,三星、中兴或华为。您的代码适用于我的旧中兴 Z812。 Android 5.1 棒棒糖。
    • 我知道它可以在其他设备上运行我的问题是如何在其他设备上运行它,例如 redmi realme
    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多