zoukankan      html  css  js  c++  java
  • 如何彻底解决Android8.0(API26+)及以上版本中模拟器运行通告栏时报Developer warning for package "com.example.my"Failed to post notification on channel 'default' See log for more details错误问题

        如何彻底解决Android8.0(API26+)及以上版本中模拟器运行通告栏时报Developer warning for package "com.example.my"Failed to post notification on channel 'default' See log for more details

    错误问题

     

     

                             ————安德风QQ1652102745

     

    一、在模拟器中运行通知栏(Notification)报错效果演示:

     

     

    PS:声明一下我的MainActivity程序代码是没有任何问题的前提下,出现这种报错状况

     

     

    二、解决问题后正常运行效果:

    ①效果1(Android8.0 API26以下版本效果图)NotificationCompat.Builder类来实现通告栏功能

     

    效果2(Android8.0 API26及以上版本效果图)NotificationCompat.Builder类来实现通告栏功能

     

     

    三、解决方案:

    1、分析原因

    出现这种错误 主要原因是我们一般常用NotificationCompat.Builder类来实现通告栏功能

    NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");

    在Android8.0 (API26)以前手机上运行通知栏要么全部屏蔽通知栏(防止骚扰),要么全部正常运行通知栏。我们用NotificationCompat.Builder类实现是完全没有问题的。

    自从Android8.0(API26)出现时,为了让用户更好的体验,于是新增通道功能(也就是NotificationChannel类),通过通道功能(也就是NotificationChannel类)能够让用户有选择的权利,用户可以设置屏蔽无关重要的垃圾通告,用户也可以设置开启选择重要的通告。

     NotificationChannel mChannel = new NotificationChannel(id, "123", importance);//建立通知栏通道类(需要有ID,重要属性)

     

    也就是说我们在Android8.0(API26)及以上的版本实现通告栏功能用NotificationCompat.Builder类,是无法正常运行的就会导致我们出现报错现象出现:

    Developer warning for package "com.example.my"Failed to post notification on channel 'default' See log for more details

    错误问题

     

    2、解决方法:

     

     

    ①如果你的模拟器运行系统版本是Android8.0以下的就用NotificationCompat.Builder类来实现通告栏功能

     

    布局设计源代码:https://www.cnblogs.com/adf520/p/12601872.html

    2-1、MainActivity.java功能实现源代码

     

      1 package com.example.my;
      2 
      3 import androidx.annotation.RequiresApi;
      4 import androidx.appcompat.app.AppCompatActivity;
      5 import androidx.core.app.NotificationCompat;
      6 
      7 import android.app.Notification;
      8 import android.app.NotificationChannel;
      9 import android.app.NotificationManager;
     10 import android.content.Context;
     11 import android.content.ContextWrapper;
     12 import android.content.Intent;
     13 import android.content.pm.PackageManager;
     14 import android.graphics.Bitmap;
     15 import android.graphics.BitmapFactory;
     16 import android.graphics.Color;
     17 import android.os.Build;
     18 import android.os.Bundle;
     19 import android.provider.Settings;
     20 import android.text.Layout;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.widget.Button;
     24 import android.widget.CheckBox;
     25 import android.widget.TextView;
     26 import android.widget.Toast;
     27 
     28 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     29 CheckBox cb1,cb2,cb3;
     30 Button pay;
     31 int count=0;
     32     @RequiresApi(api = Build.VERSION_CODES.O)
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.activity_main);
     37         
     38         cb1=findViewById(R.id.cb1);
     39         cb2=findViewById(R.id.cb2);
     40         cb3=findViewById(R.id.cb3);
     41         pay=findViewById(R.id.pay);
     42         pay.setOnClickListener(this);
     43 
     44 
     45     }
     46 
     47     @RequiresApi(api = Build.VERSION_CODES.O)
     48     @Override
     49     public void onClick(View v) {
     50         if (cb1.isChecked())
     51             count+=45;
     52         if (cb2.isChecked())
     53             count+=60;
     54         if (cb3.isChecked())
     55             count+=55;
     56 
     57 
     58 //        final NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)
     59 //        String id = "channel_1"; //自定义设置通道ID属性
     60 //        String description = "123";//自定义设置通道描述属性
     61 //        int importance = NotificationManager.IMPORTANCE_HIGH;//通知栏管理重要提示消息声音设定
     62 //        /**
     63 //         * Oreo不用Priority了,用importance
     64 //         * IMPORTANCE_NONE 关闭通知
     65 //         * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
     66 //         * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
     67 //         * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
     68 //         * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
     69 //         */
     70 //        NotificationChannel mChannel = new NotificationChannel(id, "123", importance);//建立通知栏通道类(需要有ID,重要属性)
     71 ////        mChannel.setDescription(description); // 配置通知渠道的属性
     72 ////        mChannel.enableLights(true);// 设置通知出现时的闪灯(如果 android 设备支持的话)
     73 ////        mChannel.setLightColor(Color.RED);//设置闪灯颜色为红色
     74 ////        mChannel.enableVibration(true);   // 设置通知出现时的震动(如果 android 设备支持的话)
     75 ////        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
     76 //       manager.createNotificationChannel(mChannel);////最后在notificationmanager中创建该通知渠道
     77 //        Notification notification = new Notification.Builder(this, id)//创建Notification对象。
     78 //                .setContentTitle("付款通知")  //设置通知标题
     79 //                .setSmallIcon(R.drawable.q)//设置通知小图标
     80 //                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q))//设置通知大图标
     81 //                .setContentText("您已付款"+count+"元")//设置通知内容
     82 //                .setAutoCancel(true)//设置自动删除通知
     83 //                .build();//运行
     84 //
     85 //        manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知
     86 //        count=0;//清空金额
     87 
     88 //
     89 //     LayoutInflater inflater=getLayoutInflater();
     90 //     View layout=inflater.inflate(R.layout.activity_main2,null);
     91 //     TextView tv= layout.findViewById(R.id.tv);
     92 //     tv.setText("您支付了"+count+"元");
     93 //     Toast toast=new Toast(MainActivity.this);
     94 //     toast.setView(layout);
     95 //     toast.setDuration(Toast.LENGTH_SHORT);
     96 //     toast.show();
     97 //     count=0;
     98 
     99         
    100 
    101         //第一步:创建通知构造器NotificationCompat.Builder对象。
    102         NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");
    103         //第二步:调用NotificationCompat.Builder对象的方法设置通知相关内容。
    104         builder.setSmallIcon(R.drawable.q);//设置通知小图标
    105         builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q));//设置通知大图标
    106         builder.setContentTitle("付款通知");//设置通知标题
    107         builder.setContentText("您已付款"+count+"");//设置通知内容
    108         builder.setAutoCancel(true);//设置自动删除通知
    109         Notification notification=builder.build();//:创建Notification对象。
    110         NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)
    111 //        manager.notify(1,notification); //通知栏保留单条通知
    112         manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知
    113         count=0;//清空金额
    114 
    116         
    117     }
    118 }

     

    ②如果你的模拟器运行系统版本是Android8.0及以上的就用  NotificationChannel类来实现通告栏功能

    2-1、MainActivity.java功能实现源代码

      1 package com.example.my;
      2 
      3 import androidx.annotation.RequiresApi;
      4 import androidx.appcompat.app.AppCompatActivity;
      5 import androidx.core.app.NotificationCompat;
      6 
      7 import android.app.Notification;
      8 import android.app.NotificationChannel;
      9 import android.app.NotificationManager;
     10 import android.content.Context;
     11 import android.content.ContextWrapper;
     12 import android.content.Intent;
     13 import android.content.pm.PackageManager;
     14 import android.graphics.Bitmap;
     15 import android.graphics.BitmapFactory;
     16 import android.graphics.Color;
     17 import android.os.Build;
     18 import android.os.Bundle;
     19 import android.provider.Settings;
     20 import android.text.Layout;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.widget.Button;
     24 import android.widget.CheckBox;
     25 import android.widget.TextView;
     26 import android.widget.Toast;
     27 
     28 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     29 CheckBox cb1,cb2,cb3;
     30 Button pay;
     31 int count=0;
     32     @RequiresApi(api = Build.VERSION_CODES.O)
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.activity_main);
     37 
     38         cb1=findViewById(R.id.cb1);
     39         cb2=findViewById(R.id.cb2);
     40         cb3=findViewById(R.id.cb3);
     41         pay=findViewById(R.id.pay);
     42         pay.setOnClickListener(this);
     43 
     44 
     45     }
     46 
     47     @RequiresApi(api = Build.VERSION_CODES.O)
     48     @Override
     49     public void onClick(View v) {
     50         if (cb1.isChecked())
     51             count+=45;
     52         if (cb2.isChecked())
     53             count+=60;
     54         if (cb3.isChecked())
     55             count+=55;
     56 
     57 
     58         final NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)
     59         String id = "channel_1"; //自定义设置通道ID属性
     60         String description = "123";//自定义设置通道描述属性
     61         int importance = NotificationManager.IMPORTANCE_HIGH;//通知栏管理重要提示消息声音设定
     62         /**
     63          * Oreo不用Priority了,用importance
     64          * IMPORTANCE_NONE 关闭通知
     65          * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
     66          * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
     67          * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
     68          * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
     69          */
     70         NotificationChannel mChannel = new NotificationChannel(id, "123", importance);//建立通知栏通道类(需要有ID,重要属性)
     71 //        mChannel.setDescription(description); // 配置通知渠道的属性
     72 //        mChannel.enableLights(true);// 设置通知出现时的闪灯(如果 android 设备支持的话)
     73 //        mChannel.setLightColor(Color.RED);//设置闪灯颜色为红色
     74 //        mChannel.enableVibration(true);   // 设置通知出现时的震动(如果 android 设备支持的话)
     75 //        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
     76        manager.createNotificationChannel(mChannel);////最后在notificationmanager中创建该通知渠道
     77         Notification notification = new Notification.Builder(this, id)//创建Notification对象。
     78                 .setContentTitle("付款通知")  //设置通知标题
     79                 .setSmallIcon(R.drawable.q)//设置通知小图标
     80                 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q))//设置通知大图标
     81                 .setContentText("您已付款"+count+"")//设置通知内容
     82                 .setAutoCancel(true)//设置自动删除通知
     83                 .build();//运行
     84 
     85         manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知
     86         count=0;//清空金额
     87 
     88 //
     89 //     LayoutInflater inflater=getLayoutInflater();
     90 //     View layout=inflater.inflate(R.layout.activity_main2,null);
     91 //     TextView tv= layout.findViewById(R.id.tv);
     92 //     tv.setText("您支付了"+count+"元");
     93 //     Toast toast=new Toast(MainActivity.this);
     94 //     toast.setView(layout);
     95 //     toast.setDuration(Toast.LENGTH_SHORT);
     96 //     toast.show();
     97 //     count=0;
     98 
     99 
    100 
    101 //        //第一步:创建通知构造器NotificationCompat.Builder对象。
    102 //        NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");
    103 //        //第二步:调用NotificationCompat.Builder对象的方法设置通知相关内容。
    104 //        builder.setSmallIcon(R.drawable.q);//设置通知小图标
    105 //        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q));//设置通知大图标
    106 //        builder.setContentTitle("付款通知");//设置通知标题
    107 //        builder.setContentText("您已付款"+count+"元");//设置通知内容
    108 //        builder.setAutoCancel(true);//设置自动删除通知
    109 //        Notification notification=builder.build();//:创建Notification对象。
    110 //        NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)
    111 ////        manager.notify(1,notification); //通知栏保留单条通知
    112 //        manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知
    113 //        count=0;//清空金额
    114 
    115 
    116 
    117     }
    118 }

     

    以上代码仅作为参考,希望能给你带来帮助,我是安德风,感谢大家的关注与支持,有问题欢迎在下方留言;看到后一一答复。

     

  • 相关阅读:
    二级缓存
    java面试题
    BRD,MRD,PRD文档
    程序做处理时,try..catch和if..else的区别
    关于java中字符串截取
    导出oracle数据库表(备份表)操作命令
    WSDL文件生成java类
    Linux下redis安装(单机版)
    Springboot2.0访问Redis集群
    Spring Cloud Sleuth通过Kafka将链路追踪日志输出到ELK
  • 原文地址:https://www.cnblogs.com/adf520/p/12638956.html
Copyright © 2011-2022 走看看