简体   繁体   English

Flutter 自定义通知 Application.kt:未解决的参考

[英]Flutter Custom Notification Application.kt: Unresolved reference

Im new to flutter, I want to have custom ringtone notifications, flutter version is 2.8, I found some tutorials talk about creating application.kt in kotlin folder same where locate mainactivity file, save wave file in the resource folder for android and in runner for ios, all I found just to have the below code in Application file and modify the AndroidMainifest file, this was working with me, but I need to update flutter from old version to the current one, after I update it, the app stop working because of the Application file returns the error below, what can I do to let this works again, sorry for my basic language, hopefully you have a clear solution for this issue,thank you Im new to flutter, I want to have custom ringtone notifications, flutter version is 2.8, I found some tutorials talk about creating application.kt in kotlin folder same where locate mainactivity file, save wave file in the resource folder for android and in runner for ios,我发现只是在应用程序文件中有以下代码并修改 AndroidMainifest 文件,这对我有用,但我需要将 flutter 从旧版本更新到当前版本,更新后,应用程序停止工作,因为应用程序文件返回以下错误,我该怎么做才能让它再次工作,对不起我的基本语言,希望您对此问题有明确的解决方案,谢谢

Error错误

 /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 Android主程序

  <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()
    }

}

you can get getSystemService from context change your code您可以从context中获取getSystemService更改您的代码

from:从:

val notificationManager: NotificationManager = getSystemService(context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager: NotificationManager = getSystemService(context.NOTIFICATION_SERVICE) as NotificationManager

to:至:

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

where context is context = flutterPluginBinding.getApplicationContext();其中上下文是context = flutterPluginBinding.getApplicationContext();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 flutter 中创建 Application.kt 的最佳方法是什么 - What is the best way of creating Application.kt in flutter Flutter 错误:ListTileNativeAdFactory.kt: (19, 22): 未解决的参考:R - Flutter error: ListTileNativeAdFactory.kt: (19, 22): Unresolved reference: R 未解析的引用:嵌入和未解析的引用:MainActivity.kt 中的 FlutterActivity - Unresolved reference: embedding and Unresolved reference: FlutterActivity in MainActivity.kt ID 在 MainActivity.kt 中显示为 Unresolved 引用 - ID showing up as Unresolved reference in MainActivity.kt 未解决的参考:Android 中 MainActivity.kt 中按钮的 btn_login - Unresolved reference: btn_login for button in MainActivity.kt in Android 为什么在 Mainactivity.kt 中对“id”调用 setBackgroundResource() 时出现未解决的引用错误? - Why it's unresolved reference error for calling setBackgroundResource() on an ' id ' in Mainactivity.kt? Flutter插件开发未解决参考:io - Flutter plugin development Unresolved reference: io `Unresolved reference: android` 在新的 Flutter 项目上 - `Unresolved reference: android` on new Flutter project Flutter 构建 Apk 错误“未解决的参考:snackbar” - Flutter Build Apk Error “Unresolved reference: snackbar” 自定义通知 Flutter - Custom Notification Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM