heads-up通知

6psbrbz9  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(402)

我确实认真地尝试了每一种方法和每一个片段,但仍然无法在中国品牌的设备上显示提示。
所以昨天我想为什么不再试一次,但毕竟我仍然无法显示提示通知,直到我手动转到应用程序的设置,并给予浮动权限的应用程序。
现在,你们大多数人可能会说,为什么不导航用户设置时,他/她第一次打开应用程序,但没有人喜欢,即使有其他应用程序(我不是说像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());
    }
ryevplcw

ryevplcw1#

我有两个解决办法。试一试,看看有没有效果。
你的工作原理是这样的

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

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

相关问题