通知频道未发送通知

时间:2020-09-15 03:01:06

标签: java android

/mnt/wsl

我有两种简单的方法,一种用于发送通知,另一种用于创建通知频道 一切对我来说似乎都不错,但是没有发送任何通知,我也想提及一下,同一频道创建了两次(我查看了应用的通知设置),尽管文档中提到了 private void sendNotif(String title , String text , int id) { // Create an explicit intent for an Activity in your app Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "mychannel") .setSmallIcon(android.R.drawable.screen_background_light) .setContentTitle(title) .setContentText(text) .setStyle(new NotificationCompat.BigTextStyle() .bigText(text)) // Set the intent that will fire when the user taps the notification .setContentIntent(pendingIntent) .setChannelId("mychannel") .setAutoCancel(true) .setOngoing(false); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); // notificationId is a unique int for each notification that you must define notificationManager.notify(id , builder.build()); } private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "notif channel"; String description = "this is the main notif channel"; int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel channel = new NotificationChannel("mychannel", name, importance); channel.setDescription(description); // Register the channel with the system; you can't change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } 一次:

安全地重复调用此操作是因为创建了一个现有的 通知通道不执行任何操作。

但是它确实很明显

我正在使用android 10,这是我的build.gradle文件:

createNotificationChanne()

预先感谢

1 个答案:

答案 0 :(得分:0)

我已经尝试了您的代码,效果很好。我认为您可能忘记了在创建通知之前致电createNotificationChannel()

private void sendNotif(String title , String text , int id) {
     createNotificationChannel()
     // create notification code goes here
}