Heads up notification not showing in android O and higher

独自空忆成欢 提交于 2021-02-11 13:10:37

问题


I literally have tried everything seriously every method and every snippet but still, I was not able to show heads-up notifications on Chinese brand devices.

so yesterday I thought why not try it again but after all again still I'm not able to show heads-up notification until I manually goto the app into the settings and give floating permission for the app.

Now most of you may say why not to navigate the user to the setting when he/she first opens up the app but nobody likes that even there are other apps (I'm not talking about white list app like WhatsApp) which have 10K downloads are able to show heads up notification

Here is my code, btw I have tried setting the sound, vibration, and light but still heads up are not showing, and yes I do uninstall my app after every build

    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());
    }

回答1:


I have two solutions. Try them out and see if any works.

  1. Yours actually works Like this

     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. Mine notifyUser("Order Delivery", "Your order has arrived"):

     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());
     }
    


来源:https://stackoverflow.com/questions/65263749/heads-up-notification-not-showing-in-android-o-and-higher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!