android - 未解决的参考 NotificationCompat 即使在添加支持库之后
问题描述
我正在尝试对 4.4 以上的 sdk 使用通知。
我已将支持库添加到我的代码中,这是我的 build.gradle :
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId "com.my.app"
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.2.51'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:support-core-utils:27.1.1'
implementation 'com.android.support:support-core-ui:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-compat:27.1.1'
implementation 'com.android.support:gridlayout-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation "org.jetbrains.anko:anko:$anko_version"
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.0'
implementation 'com.github.ittianyu:BottomNavigationViewEx:1.2.1'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.github.GrenderG:Toasty:1.2.5'
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
implementation project(':Library:rangeseekbar')
implementation 'com.github.esafirm.android-image-picker:imagepicker:1.12.0'
implementation 'org.apache.commons:commons-io:1.3.2'
implementation 'com.github.hamsaadev:Persian-Date-Picker-Dialog:V1.2'
implementation 'jp.wasabeef:blurry:2.1.1'
implementation 'com.github.firdausmaulan:GlideSlider:1.3.1'
implementation('io.socket:socket.io-client:1.0.0') {
exclude group: 'org.json', module: 'json'
}
但是当我尝试将NotificationCompat添加到项目内的 KT 类中时,它会给出未解决的参考错误。
和代码:
import android.app.Notification
import android.content.Context
import android.content.Intent
import android.util.Log;
import okhttp3.*
import io.socket.client.IO
import org.json.JSONException
import org.json.JSONObject
import io.socket.emitter.Emitter
import io.socket.client.Socket
import org.jetbrains.anko.AnkoAsyncContext
import android.graphics.BitmapFactory
import android.app.NotificationManager
import android.app.NotificationChannel
import android.os.Build
import android.app.PendingIntent
import android.graphics.Color
import com.khcai.mifrooshe.R
import java.util.concurrent.atomic.AtomicInteger
class SocketListener : WebSocketListener() {
private var mSocket: Socket? = null
private var SOCKET_SERVER_URL = "http://10.0.2.2:6001/"
private var outsideContext : Context? = null
fun start(context : Context?) {
outsideContext = context
mSocket = IO.socket(SOCKET_SERVER_URL);
mSocket!!.on("App\\Events\\NotificationEvent", NotificationEvent)
attemptLogin()
}
private fun attemptLogin() {
val `object` = JSONObject()
try {
`object`.put("channel", "everyone")
} catch (e: JSONException) {
return
}
mSocket!!.connect()
mSocket!!.emit("subscribe", `object`)
}
private val NotificationEvent = Emitter.Listener { args ->
try {
val data = args[1] as JSONObject
val msg = data.getString("msg")
// Toasty.error(getApplicationContext(), msg, Toast.LENGTH_LONG, true).show();
var builder = Notification.Builder(outsideContext)
.setContentTitle("New Message")
.setContentText(msg)
.build();
Log.e("server msg: ", msg)
} catch (e: Exception) {
Log.e("TAG error : ", e.message)
}
}
private val c = AtomicInteger(0)
var Notification_ID = c.incrementAndGet()
fun showNotification(from: String, notification: String, intent: Intent) {
val pendingIntent = PendingIntent.getActivity(
outsideContext,
Notification_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val NOTIFICATION_CHANNEL_ID = "my_channel_id_01"
val notificationManager = outsideContext?.getSystemService(Context.NOTIFICATION_SERVICE)
as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
"My Notifications", NotificationManager.IMPORTANCE_DEFAULT)
// Configure the notification channel.
notificationChannel.description = "Channel description"
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
}
val builder = NotificationCompat.Builder(outsideContext, NOTIFICATION_CHANNEL_ID)
val mNotification = builder
.setContentTitle(from)
.setContentText(notification)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(outsideContext?.getResources(), R.mipmap.ic_launcher))
.build()
notificationManager.notify(/*notification id*/Notification_ID, mNotification)
}
}
我在 websocket 服务中使用通知来通知用户从服务器响应。我什至使缓存重新清理 gradle 构建无效。但仍然没有希望。
解决方案
推荐阅读
- java - 阻止 WebView 加载特定的 URL
- office-js - 如何在更新后使用 Internet Explorer 11 运行 Word 加载项(Windows 10 版本 >= 1903 / Office 365 版本 >= 16.0.116291)
- angular - PrimeNG:没有 MenuModel 的简单按钮下拉菜单
- docker - 新用户帐户 - 拒绝进入 docker 的权限
- r - 从嵌套列表数据框中删除 NA
- azure-mobile-services - 适用于 Xamarin 表单的 Azure 移动服务 - 冲突解决
- ruby-on-rails - 如何在Rails中将Array的值一一输入到数据库中?
- c - 仅在给定参数时在 Makefile 编译中添加标志
- c# - Asp.NET Core 3 - Azure Active Directory - 令牌验证失败 - 签名验证失败。无法匹配密钥
- typescript - 尝试将模块导入返回到其原始/默认模拟