0

我真的认真地尝试了每一种方法和每一个片段,但我仍然无法在中国品牌设备上显示提醒通知。

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

现在你们中的大多数人可能会说为什么不在用户第一次打开应用程序时将其导航到设置但没有人喜欢即使有其他应用程序(我不是在谈论像 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());
    }
4

1 回答 1

0

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

  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());
     }
    
于 2020-12-12T10:56:55.103 回答