android 气泡

One of the many new features that have launched with Android Q is notification bubbles. You might remember the Facebook Chatheads from a few years ago — you might have even wanted to implement them or already implemented some sort of variant using the SYSTEM_ALERT_WINDOW permission.

通知气泡是Android Q推出的众多新功能之一。 您可能还记得几年前的Facebook Chatheads,甚至可能想要实现它们,或者已经使用SYSTEM_ALERT_WINDOW权限实现了某种变体。

Notification Bubbles are aimed at providing the same functionality natively without using such an all-encompassing permission. The documentation describes these Bubble notifications as follows:

通知气泡旨在本机提供相同的功能,而不使用这种无所不包的权限。 该文档描述了这些气泡通知,如下所示:

Bubbles are built into the Notification system. They float on top of other app content and follow the user wherever they go. Bubbles can be expanded to reveal app functionality and information, and can be collapsed when not being used.

气泡内置于通知系统中。 它们漂浮在其他应用程序内容之上,并且随时随地跟随用户。 气泡可以扩展以显示应用程序的功能和信息,并且在不使用时可以折叠。

Implementing Bubbles with Notify

使用通知实施气泡

For anyone that’s worked with notifications, saying that they’re pretty complex is rather generous, from Notification Channels to Notification Priority and overall API compatibility, Android Notifications are quite a piece of work. With the release of Android Q, let’s take a quick look at how we can easily render some notification bubbles using Notify!

对于使用通知的任何人来说,从通知通道到通知优先级以及整体API兼容性,它们都相当复杂,从通知通道到通知优先级以及整体API兼容性,Android通知是一项艰巨的工作。 随着Android Q的发布,让我们快速看一下如何使用Notify轻松呈现一些通知气泡!

A screenshot of a sample Bubble Notification.

气泡通知示例的屏幕截图。

To begin you have to first have a version of Android Q, be it on a device or on the emulator. Navigate to the Developer Settings and enable the Bubbles setting.

首先,您必须先拥有一个Android Q版本,无论是在设备上还是在模拟器上。 导航到“开发人员设置”并启用“气泡”设置。

Now let's get down to business. Let’s start by importing Notify into our project.

现在开始做生意。 让我们从将Notify导入我们的项目开始。

// Project level build.gradle
// ...
repositories {
    maven { url 'https://jitpack.io' }
}
// ...
// Module level build.gradle
dependencies {
    // Use the latest development branch
    implementation "io.karn:notify:develop"
}

Also, don’t forget to set your targetSdkVersion and compileSdkVersion to 29.

另外,不要忘记将targetSdkVersion和compileSdkVersion设置为29 。

Now that we’ve added Notify to the project, we can dive into creating the notification Bubbles. When expanded, Notification Bubbles can show an Activity to the user. We can create a simple Activity as follows:

现在,我们已将Notify添加到项目中,我们可以深入创建通知气泡。 展开后,通知气泡可以向用户显示活动。 我们可以创建一个简单的活动,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_notify_logo" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Bubbles are ready!" />


</LinearLayout>

A preview of the sample activity we’ll be using.

我们将使用的示例活动的预览。

Next, we update the AndroidManifest.xml file to allow the Notification Bubble to render the activity properly, you’ll have to do this for any activity that you want to show as a Bubble.

接下来,我们更新AndroidManifest.xml文件,以允许Notification Bubble正确呈现活动,对于要显示为Bubble的任何活动,您都必须执行此操作。

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>


    <application
        ...>
        
        <activity
            android:name=".BubbleActivity"
            ...
            <!-- Add the three lines below to the activity being shown -->
            android:allowEmbedded="true"
            android:documentLaunchMode="always"
            android:resizeableActivity="true" />
    </application>
    
</manifest>

Great! Now that your Activity is ready we can construct the Notification and show it, Notify will handle everything under the hood to make sure that you can display the notification properly (including the Notification Channel creation and use, and if you want to change that you can!).

大! 现在您的活动已准备就绪,我们可以构建通知并进行显示,Notify将处理所有内容,以确保您可以正确显示通知(包括创建和使用通知通道,如果要更改,则可以!)。

Notify.with(context)
    // Defines the content of the Notification
    .content {
        title = "New dessert menu"
        text = "The Cheesecake Factory has a new dessert for you to try!"
    }
    // Bubblize the Notfication!
    .bubblize {
        // Create bubble intent
        val target = Intent(context, BubbleActivity::class.java)
        val bubbleIntent = PendingIntent.getActivity(context, 0, target, 0 /* flags */)


        // Set the image for the Bubble
        bubbleIcon = IconCompat.createWithResource(context, R.drawable.ic_app_icon)
        // Set the activity that is being shown when the Bubble is expanded.
        targetActivity = bubbleIntent
    }
    .show()

That’s it! Once the snippet above executes you’ll have an awesome Bubble Notification to play around with!

而已! 上面的代码段执行完毕后,您将获得一个很棒的泡泡通知!

Notifications bubbles made easy. 通知气泡变得容易。

Notify allows you to do this and much more, the goal is to take care of building and showing notifications so you can get back to dev work that matters most. You shouldn’t have to fight with APIs to make things work and Notify is built to remove some of that pain.

通知允许您执行此操作以及执行更多操作,目标是负责构建和显示通知,以便您可以重新开始最重要的开发工作。 您不必为了使事情正常工作而与API斗争,而Notify旨在消除其中的某些麻烦。

Check it out all the notification styles and file feature requests and bugs. If you’re interested in contributing, it’s super easy!

检查所有通知样式以及文件功能请求和错误 。 如果您有兴趣做出贡献,那就超级容易了!

If you have any questions or comments be sure to leave them below! You can find me on GitHub or reach out to me on Twitter. 👋🏽

翻译自: https://medium.com/swlh/taking-advantage-of-android-q-notification-bubbles-with-notify-4762b3099074

android 气泡