我正在尝试对高于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'
}
但是当我尝试在项目内部的KT类中添加 NotificationCompat 时,它会给出未解决的引用错误。
和代码:
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构建无效。但仍然没有希望。