如何徹底解決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 }
以上代碼僅作為參考,希望能給你帶來幫助,我是安德風,感謝大家的關注與支持,有問題歡迎在下方留言;看到后一一答復。