首页 > 解决方案 > 制作Flutter插件时如何在原生android端实现Notification?

问题描述

我正在开发一个 Flutter 插件,它需要我在 android 端本身安排一个通知。我正在调用下面的函数

  fun setAlarm(){
    val name = "ALARM_CHANNEL"
    val descriptionText = "All alarms will be shown in this channel"
    val importance = NotificationManager.IMPORTANCE_DEFAULT
    val channel = NotificationChannel("CHANNEL_ID", name, importance).apply {
      description = descriptionText
    }
    val notificationManager: NotificationManager =
      context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(channel)
    val builder= NotificationCompat.Builder(context, "SCREEN")
      .setSmallIcon(context.resources.getIdentifier("ic_access_alarms", "drawable", context.packageName))
      .setContentTitle("Title")
      .setContentText("Body")
      .setPriority(NotificationCompat.PRIORITY_HIGH)
    notificationManager.notify(Random.nextInt(), builder.build())
//        Tried this as well, not working
//        with(NotificationManagerCompat.from(context)){
//            notify(Random.nextInt(), builder.build())
//        }
  }

出于调试目的,我还没有安排通知。日志中也没有显示错误,它只是默默地失败。我怀疑我是如何获取小图标的。通过所有其他方式,我试图让应用程序崩溃的图标。

标签: androidflutterkotlinflutter-plugin

解决方案


答案非常非常愚蠢。我没有为通知分配频道


推荐阅读