我一直在浏览网络寻找答案,但我自己找不到它,尽管阅读官方Android文档网站上的开发人员指南和来自互联网的代码片段.
我一直在练习在一个新的干净项目上创建通知,尽管创建了一个通知通道和通知本身,我仍然无法让它运行并收到一条消息:"无法在空通道上发布通知".但我正在创建频道,并将其分配给我的通知,就像它说到处都是.我完全没有想法,老实说我现在已经浏览堆栈至少几个小时,每个答案看起来都一样,但它根本没有帮助我,所以也许如果有人比一个初学者更好看我的代码可以告诉我我的代码有什么问题?这很烦人,因为通知是我工作的一个项目的重要组成部分,它阻止了我的追踪.
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
findViewById(R.id.buckysButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createNotification(view);
}
});
}
public void createNotification(View view){
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, …Run Code Online (Sandbox Code Playgroud)