In this tutorial, we’ll be discussing the changes introduced in the Notification System and its UI with Android P. We’ll be demonstrating it with a simple Android Application.
在本教程中,我们将讨论通过Android P在Notification System及其UI中引入的更改。我们将通过一个简单的Android应用程序进行演示。
Android P通知 (Android P Notifications)
Android Pie has introduced a new Messaging Style Notification UI which provides simplified conversations.
Moreover, we can now display images with text in the messaging style as well.
Android Pie引入了新的消息传递样式通知UI,该UI提供了简化的对话。
而且,我们现在也可以以消息传递样式显示带有文本的图像。
With Android P, the addMessage()
function of the MessagingStyle Class has changed. Now instead of passing the String, we pass the Person
object which ties that notification and its message to that person.
使用Android P,MessagingStyle类的addMessage()
函数已更改。 现在,而不是传递字符串,我们传递了Person
对象,该对象将通知及其消息绑定到该人员。
setData
is used to display the image on the message.
setData
用于在消息上显示图像。
setSemanticAction
is used to set predefined references to notification actions.
setSemanticAction
用于设置对通知操作的预定义引用。
Following are the semantic actions available:
以下是可用的语义动作:
- SEMANTIC_ACTION_NONE SEMANTIC_ACTION_NONE
- SEMANTIC_ACTION_REPLY SEMANTIC_ACTION_REPLY
- SEMANTIC_ACTION_MARK_AS_READ SEMANTIC_ACTION_MARK_AS_READ
- SEMANTIC_ACTION_MARK_AS_UNREAD SEMANTIC_ACTION_MARK_AS_UNREAD
- SEMANTIC_ACTION_DELETE SEMANTIC_ACTION_DELETE
- SEMANTIC_ACTION_ARCHIVE SEMANTIC_ACTION_ARCHIVE
- SEMANTIC_ACTION_MUTE SEMANTIC_ACTION_MUTE
- SEMANTIC_ACTION_UNMUTE SEMANTIC_ACTION_UNMUTE
- SEMANTIC_ACTION_THUMBS_UP SEMANTIC_ACTION_THUMBS_UP
- SEMANTIC_ACTION_THUMBS_DOWN SEMANTIC_ACTION_THUMBS_DOWN
- SEMANTIC_ACTION_CALL SEMANTIC_ACTION_CALL
setGroupConversation
is used to identify a notification group as a conversation.
setGroupConversation
用于将通知组标识为对话。
In the next section, we’ll implement the different types of Notifications in Android P.
在下一节中,我们将在Android P中实现不同类型的通知。
简单消息通知 (Simple Message Notifications)
private void simpleNotification() {
Person jd = new Person.Builder()
.setName("JournalDev")
.setImportant(true)
.build();
new NotificationCompat.MessagingStyle(jd)
.addMessage("Check me out", new Date().getTime(), jd)
.setBuilder(builder);
notificationManager.notify(1, builder.build());
}
Inside the addMessage
, we pass the message, time(long), and the Person object.
setImportant
true indicates that the message would be displayed at the top in the conversation if the notification is collapsed.
在addMessage
内部,我们传递消息,time(long)和Person对象。
setImportant
true表示如果折叠通知,则消息将显示在对话的顶部。
带图标的消息通知 (Message Notification With Icon)
By default, the icon displayed is the first letter of the Person.
We can set a custom icon in place as shown below:
默认情况下,显示的图标是“人员”的首字母。
我们可以在地方设置自定义图标,如下所示:
Person anupam = new Person.Builder()
.setName("Anupam")
.setIcon(IconCompat.createWithResource(this, R.drawable.sample_photo))
.setImportant(true)
.build();
new NotificationCompat.MessagingStyle(anupam)
.addMessage("Check out my latest article!", new Date().getTime(), anupam)
.setBuilder(builder);
notificationManager.notify(2, builder.build());
带有图像的通知 (Notification With Image)
We can set the image type and uri in the setData
method as shown below:
我们可以在setData
方法中设置图像类型和uri,如下所示:
private void notificationWithImage() {
Person bot = new Person.Builder()
.setName("Bot")
.setImportant(true)
.setBot(true)
.build();
Uri uri = Uri.parse("android.resource://com.journaldev.androidpnotifications/drawable/"+R.drawable.sample_bg);
NotificationCompat.MessagingStyle.Message message = new NotificationCompat.MessagingStyle.Message("Check out my latest article!", new Date().getTime(), bot);
message.setData("image/*",uri);
new NotificationCompat.MessagingStyle(bot)
.addMessage(message)
.setGroupConversation(true)
.setBuilder(builder);
notificationManager.notify(3, builder.build());
}
setBot
to true indicates that the Person type is a machine.
setBot
为true表示“人员”类型是机器。
消息对话通知 (Message Conversation Notification)
private void notificationWithGroupConvo()
{
Person jd = new Person.Builder()
.setName("JournalDev")
.build();
Person anupam = new Person.Builder()
.setName("Anupam")
.setIcon(IconCompat.createWithResource(this, R.drawable.sample_photo))
.setImportant(true)
.build();
Person bot = new Person.Builder()
.setName("Bot")
.setBot(true)
.build();
Uri uri = Uri.parse("android.resource://com.journaldev.androidpnotifications/drawable/"+R.drawable.sample_bg);
NotificationCompat.MessagingStyle.Message message = new NotificationCompat.MessagingStyle.Message("", new Date().getTime(), bot);
message.setData("image/*",uri);
new NotificationCompat.MessagingStyle(bot)
.addMessage("Hi. How are you?", new Date().getTime(), anupam)
.addMessage(message)
.addMessage("Does this image look good?", new Date().getTime(), bot)
.addMessage("Looks good!", new Date().getTime(), jd)
.setGroupConversation(true)
.setConversationTitle("Sample Conversation")
.setBuilder(builder);
notificationManager.notify(4, builder.build());
}
The messages are displayed in the order in which the addMessage
methods are set.
消息以设置addMessage
方法的顺序显示。
In the following section, we’ll aggregate the above-learned concepts along with semantic actions.
We’ll be using the AndroidX Packaging System.
在以下部分中,我们将汇总以上学习的概念以及语义动作。
我们将使用AndroidX包装系统。
项目结构 (Project Structure)
![Android P Notification Project](https://cdn.journaldev.com/wp-content/uploads/2019/04/android-p-notification-project.png)
Android P Notification Project
Android P通知项目
码 (Code)
The code for the activity_main.xml
layout is given below:
下面给出了activity_main.xml
布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnSimpleNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Notification" />
<Button
android:id="@+id/btnNotificationIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification With Icon" />
<Button
android:id="@+id/btnNotificationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification With Image" />
<Button
android:id="@+id/btnNotificationWithGroupConvo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification With Group Conversation" />
<Button
android:id="@+id/btnNotificationSemantic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Semantic Action" />
</LinearLayout>
The code for the MainActivity.java class is given below:
MainActivity.java类的代码如下:
package com.journaldev.androidpnotifications;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.core.app.NotificationCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.Person;
import androidx.core.graphics.drawable.IconCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
NotificationManager notificationManager;
NotificationCompat.Builder builder;
NotificationChannel channel;
CharSequence charSequence = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSimpleNotification = findViewById(R.id.btnSimpleNotification);
Button btnNotificationIcon = findViewById(R.id.btnNotificationIcon);
Button btnNotificationImage = findViewById(R.id.btnNotificationImage);
Button btnNotificationWithGroupConvo = findViewById(R.id.btnNotificationWithGroupConvo);
Button btnNotificationSemantic = findViewById(R.id.btnNotificationSemantic);
charSequence = btnNotificationIcon.getText();
btnSimpleNotification.setOnClickListener(this);
btnNotificationIcon.setOnClickListener(this);
btnNotificationImage.setOnClickListener(this);
btnNotificationWithGroupConvo.setOnClickListener(this);
btnNotificationSemantic.setOnClickListener(this);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence name = "My Notification";
String description = "yadda yadda";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
channel = new NotificationChannel("1", name, importance);
channel.setDescription(description);
builder = new NotificationCompat.Builder(MainActivity.this, channel.getId())
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.createNotificationChannel(channel);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSimpleNotification:
simpleNotification();
break;
case R.id.btnNotificationIcon:
notificationWithIcon();
break;
case R.id.btnNotificationImage:
notificationWithImage();
break;
case R.id.btnNotificationWithGroupConvo:
notificationWithGroupConvo();
break;
case R.id.btnNotificationSemantic:
notificationSemantic();
break;
}
}
private void simpleNotification() {
Person jd = new Person.Builder()
.setName("JournalDev")
.setImportant(true)
.build();
new NotificationCompat.MessagingStyle(jd)
.addMessage("Check me out", new Date().getTime(), jd)
.setBuilder(builder);
notificationManager.notify(1, builder.build());
}
private void notificationWithIcon() {
Person anupam = new Person.Builder()
.setName("Anupam")
.setIcon(IconCompat.createWithResource(this, R.drawable.sample_photo))
.setImportant(true)
.build();
new NotificationCompat.MessagingStyle(anupam)
.addMessage("Check out my latest article!", new Date().getTime(), anupam)
.setBuilder(builder);
notificationManager.notify(2, builder.build());
}
private void notificationWithImage() {
Person bot = new Person.Builder()
.setName("Bot")
.setImportant(true)
.setBot(true)
.build();
Uri uri = Uri.parse("android.resource://com.journaldev.androidpnotifications/drawable/"+R.drawable.sample_bg);
NotificationCompat.MessagingStyle.Message message = new NotificationCompat.MessagingStyle.Message("Check out my latest article!", new Date().getTime(), bot);
message.setData("image/*",uri);
new NotificationCompat.MessagingStyle(bot)
.addMessage(message)
.setGroupConversation(true)
.setBuilder(builder);
notificationManager.notify(3, builder.build());
}
private void notificationWithGroupConvo()
{
Person jd = new Person.Builder()
.setName("JournalDev")
.build();
Person anupam = new Person.Builder()
.setName("Anupam")
.setIcon(IconCompat.createWithResource(this, R.drawable.sample_photo))
.setImportant(true)
.build();
Person bot = new Person.Builder()
.setName("Bot")
.setBot(true)
.build();
Uri uri = Uri.parse("android.resource://com.journaldev.androidpnotifications/drawable/"+R.drawable.sample_bg);
NotificationCompat.MessagingStyle.Message message = new NotificationCompat.MessagingStyle.Message("", new Date().getTime(), bot);
message.setData("image/*",uri);
new NotificationCompat.MessagingStyle(bot)
.addMessage("Hi. How are you?", new Date().getTime(), anupam)
.addMessage(message)
.addMessage("Does this image look good?", new Date().getTime(), bot)
.addMessage("Looks good!", new Date().getTime(), jd)
.setGroupConversation(true)
.setConversationTitle("Sample Conversation")
.setBuilder(builder);
notificationManager.notify(4, builder.build());
}
private void notificationSemantic()
{
Person jd = new Person.Builder()
.setName("JournalDev")
.build();
Person anupam = new Person.Builder()
.setName("Anupam")
.setIcon(IconCompat.createWithResource(this, R.drawable.sample_photo))
.setImportant(true)
.build();
Person bot = new Person.Builder()
.setName("Bot")
.setBot(true)
.build();
Uri uri = Uri.parse("android.resource://com.journaldev.androidpnotifications/drawable/"+R.drawable.sample_bg);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("hi","Notifications were read");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.MessagingStyle.Message message = new NotificationCompat.MessagingStyle.Message("", new Date().getTime(), bot);
message.setData("image/*",uri);
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.sample_bg,
"MARK READ",
pendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.build();
NotificationCompat.Builder separateBuilder = builder;
separateBuilder.addAction(replyAction);
new NotificationCompat.MessagingStyle(bot)
.addMessage("Hi. How are you?", new Date().getTime(), anupam)
.addMessage(message)
.addMessage("Does this image look good?", new Date().getTime(), bot)
.addMessage("Looks good!", new Date().getTime(), jd)
.setGroupConversation(true)
.setConversationTitle("Sample Conversation")
.setBuilder(separateBuilder);
notificationManager.notify(5, separateBuilder.build());
}
@Override
protected void onResume() {
super.onResume();
if(getIntent()!=null && getIntent().getExtras()!=null)
{
String value = getIntent().getStringExtra("hi");
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_LONG).show();
}
}
}
When the semantic action button is clicked, we show the intent data in a Toast.
单击语义动作按钮时,我们将在Toast中显示意图数据。
The output of the above application in action is given below:
上面应用程序的输出如下:
![Android P Notification Output](https://cdn.journaldev.com/wp-content/uploads/2019/04/android-p-notification-output.gif)
Android P Notification Output
Android P通知输出
That brings an end to this tutorial. You can download the project from the link below or view the full source code in our Github Repository below.
这样就结束了本教程。 您可以从下面的链接下载该项目,或者在下面的Github存储库中查看完整的源代码。
翻译自: https://www.journaldev.com/28473/android-p-notifications