Я недавно экспериментировал с новым Bubbles API. Независимо от того, что я делаю, уведомления, которые я ожидаю, чтобы появиться как пузырь, всегда появляются в системном трее как обычное уведомление.
Я написал свое собственное игрушечное приложение, которое я добавлю сюда. Я также удалил несколько других приложений из учебных пособий ( здесь и здесь ), которые я изучал. В каждом отдельном случае, без пузыря, просто уведомление в системном трее.
Поскольку примеры приложений утверждают, что они могут представлять пузыри, я предполагаю, что проблема должна быть где-то в моей среде эмулятора. Я использую эмулятор, который использует Android API R
. И я включил пузыри в опциях разработчика:
Вот соответствующий код из приложения, которое я разработал:
AndroidManifest. xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bubbles">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".BubbleActivity"
android:allowEmbedded="true"
android:documentLaunchMode="always"
android:resizeableActivity="true" />
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.kt
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
class MainActivity : AppCompatActivity() {
private lateinit var bubbleViewModel: BubbleViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bubbleViewModel = ViewModelProvider(
this, BubbleViewModelFactory(this.application))
.get(BubbleViewModel::class.java)
}
fun blowBubble(view: View) {
bubbleViewModel.showBubble()
}
}
BubbleViewModel.kt
class BubbleViewModel(application: Application): AndroidViewModel(application) {
private val notificationHelper = NotificationHelper(getApplication())
init {
notificationHelper.createNotificationChannels()
}
fun showBubble() {
viewModelScope.launch{
withContext(Dispatchers.Main) {
with (notificationHelper) {
if (canBubble())
showNotification()
}
}
}
}
}
NotificationHelper.kt
class NotificationHelper(private val context: Context) {
private val notificationManager = context.getSystemService(NotificationManager::class.java)
private lateinit var channel: NotificationChannel
fun createNotificationChannels() {
channel = NotificationChannel(
CHANNEL_NEW_MESSAGES,
context.getString(R.string.channel_name),
NotificationManager.IMPORTANCE_HIGH)
with(channel) {
enableVibration(true)
lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
description = context.getString(R.string.channel_description)
setAllowBubbles(true)
}
Log.d("bubble", "Can Bubble: $channel.canBubble()")
notificationManager?.let {
it.createNotificationChannel(channel)
}
}
@WorkerThread
fun showNotification() {
val bubbleIntent = PendingIntent.getActivity(
context,
REQUEST_BUBBLE,
Intent(context, BubbleActivity::class.java).setAction(Intent.ACTION_VIEW),
PendingIntent.FLAG_UPDATE_CURRENT)
val bubbleMetaData = Notification.BubbleMetadata.Builder()
.setDesiredHeight(600)
.createIntentBubble(bubbleIntent, Icon.createWithResource(context, R.drawable.baseball))
.setAutoExpandBubble(false)
.setSuppressNotification(false)
.build()
val person = Person.Builder()
.setIcon(Icon.createWithResource(context, R.drawable.baseball))
.setName("Bubbles...")
.setImportant(true)
.build()
val style = Notification.MessagingStyle(person)
.addMessage("...are the Best!", System.currentTimeMillis(), person)
val builder = Notification.Builder(context, CHANNEL_NEW_MESSAGES)
.setBubbleMetadata(bubbleMetaData)
.setContentIntent(bubbleIntent)
// .setContentTitle("Title")
// .setContentText("Hello this is a notification")
.setSmallIcon(R.drawable.baseball)
.setShowWhen(true)
.setAutoCancel(true)
.setStyle(style)
// .addPerson(person.uri)
notificationManager?.notify(0, builder.build())
}
fun canBubble(): Boolean {
notificationManager?.let {
val channel = it.getNotificationChannel(CHANNEL_NEW_MESSAGES)
return it.areBubblesAllowed() && channel.canBubble()
}
return false
}
companion object {
private const val CHANNEL_NEW_MESSAGES = "new_messages"
const val REQUEST_BUBBLE = 2
}
}
И, наконец, целевое действие, которое, я думаю, не имеет большого значения, поскольку оно срабатывает только при наличии пузырька. нажать: BubbleActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class BubbleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bubble)
}
}
И это действительно все, что нужно сделать. Но когда я запускаю это и нажимаю кнопку, чтобы отобразить пузырь, я получаю такой результат: