MainActivity.kt
Код: Выделить всё
class MainActivity: FlutterActivity() {
private val CHANNEL = "flutter.native/helper"
private lateinit var mediaSession: MediaSessionCompat
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
mediaSession = MediaSessionCompat(this, "MediaSession")
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
when (call.method) {
"showNotificationFromNative" -> {
val title = call.argument("title") ?: "Empty"
val message = call.argument("message") ?: "Empty"
val greetings = showSimpleNotification(title, message)
result.success(greetings)
}
"showCustomNotificationFromNative" -> {
val args = call.arguments as? Map
if (args != null) {
val greetings = showCustomNotification(args)
result.success(greetings)
} else {
result.error("INVALID_ARGUMENTS", "Arguments must be a Map", null)
}
}
"showMediaNotification" -> {
val title = call.argument("title") ?: ""
val artist = call.argument("artist") ?: ""
val album = call.argument("album") ?: ""
val duration = call.argument("duration") ?: 0
val position = call.argument("position") ?: 0
showMediaNotification(title, artist, album, duration, position)
result.success(null)
}
else -> {
result.notImplemented()
}
}
}
}
private fun showCustomNotification(args: Map): String {
val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelId = "i.apps.custom.notification"
val description = "Custom Charging Notification"
val isFirstUpdate = args["is_first_update"] as Boolean
val isStillCharging = args["isStillCharging"] as Boolean
// Create and configure the notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isFirstUpdate) {
val notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(false)
notificationManager.createNotificationChannel(notificationChannel)
}
// Build the notification
val builder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_charging_small)
.setOngoing(isStillCharging)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle("${args["device_name"]}")
.setContentText("${args["current_percentage"]}% / ${args["estimate_percentage"]}% by ${args["estimate_time"]}")
// Add a progress bar
val maxProgress = args["estimate_percentage"].toString().toInt()
val currentProgress = args["current_percentage"].toString().toInt()
// Create a custom layout
val customLayout = RemoteViews(packageName, R.layout.notification_expanded)
customLayout.setTextViewText(R.id.charging_title, "${args["device_name"]}")
customLayout.setTextViewText(R.id.charging_estimate, "${args["estimate_percentage"]}% by ${args["estimate_time"]}")
customLayout.setProgressBar(R.id.charging_progress, maxProgress, currentProgress, false)
customLayout.setTextViewText(R.id.charging_percentage, "${args["current_percentage"]}%")
// Set the custom layout
builder.setCustomBigContentView(customLayout)
builder.setStyle(NotificationCompat.DecoratedCustomViewStyle())
// builder.setStyle(MediaStyle())
// Show the notification
notificationManager.notify(12345, builder.build())
return "Success"
}
Код: Выделить всё
Подробнее здесь: https://stackoverflow.com/questions/790 ... icationcom