【问题标题】:Flutter Custom Notification Application.kt: Unresolved referenceFlutter 自定义通知 Application.kt:未解决的参考
【发布时间】:2022-01-13 14:36:39
【问题描述】:

我是 Flutter 的新手,我想要自定义铃声通知,flutter 版本是 2.8,我发现一些教程谈到在 kotlin 文件夹中创建 application.kt 与找到 mainactivity 文件相同,将 wave 文件保存在 android 的资源文件夹中和在 ios 的 runner 中,我发现只是在应用程序文件中有以下代码并修改了 AndroidMainifest 文件,这对我有用,但我需要将颤振从旧版本更新到当前版本,更新后,应用程序由于应用程序文件返回以下错误而停止工作,我该怎么做才能让它再次工作,对不起我的基本语言,希望您对此问题有明确的解决方案,谢谢

错误

 /android/app/src/main/kotlin/com/foodeasy/merchant/Application.kt: (7, 31): Unresolved reference: firebasemessaging
  e: /app/src/main/kotlin/com/foodeasy/merchant/Application.kt: (19, 5): Class 'Application' is not abstract and does not implement abstract member public abstract fun registerWith(p0: PluginRegistry!): Unit defined in io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback

AndroidMainifest

  <application
    android:name=".Application"
    android:label="myapp"
    android:requestLegacyExternalStorage="true"
    android:usesCleartextTraffic="true"
    android:icon="@mipmap/ic_launcher">


package com.*****.****

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import android.util.Log



class Application : FlutterApplication(), PluginRegistrantCallback {
    private fun createChannel(){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val sound = Uri.parse("android.resource://" + applicationContext.packageName + "/raw/" + R.raw.ntf)
            // Create the NotificationChannel
            val channel = NotificationChannel("mychannel", "default", NotificationManager.IMPORTANCE_HIGH)
            val audioAttributes = AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build()
            channel.setSound(sound, audioAttributes)
            val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

    override fun onCreate() {
        super.onCreate()
    }

}

【问题讨论】:

  • 你找到解决办法了吗?
  • 还没有抱歉!!
  • 使用我在下面提供的解决方案,它对我有用。

标签: android flutter


【解决方案1】:

您可以从context 获得getSystemService 更改您的代码

来自:

val notificationManager: NotificationManager = getSystemService(context.NOTIFICATION_SERVICE) 作为 NotificationManager

到:

val notificationManager: NotificationManager = context.getSystemService(context.NOTIFICATION_SERVICE) 作为 通知管理器

上下文为context = flutterPluginBinding.getApplicationContext();

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2021-04-01
    • 2022-06-15
    • 2021-10-14
    • 2019-11-19
    • 2020-03-11
    • 1970-01-01
    • 2019-05-28
    • 2020-12-26
    相关资源
    最近更新 更多