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_NONESEMANTIC_ACTION_NONE
  • SEMANTIC_ACTION_REPLYSEMANTIC_ACTION_REPLY
  • SEMANTIC_ACTION_MARK_AS_READSEMANTIC_ACTION_MARK_AS_READ
  • SEMANTIC_ACTION_MARK_AS_UNREADSEMANTIC_ACTION_MARK_AS_UNREAD
  • SEMANTIC_ACTION_DELETESEMANTIC_ACTION_DELETE
  • SEMANTIC_ACTION_ARCHIVESEMANTIC_ACTION_ARCHIVE
  • SEMANTIC_ACTION_MUTESEMANTIC_ACTION_MUTE
  • SEMANTIC_ACTION_UNMUTESEMANTIC_ACTION_UNMUTE
  • SEMANTIC_ACTION_THUMBS_UPSEMANTIC_ACTION_THUMBS_UP
  • SEMANTIC_ACTION_THUMBS_DOWNSEMANTIC_ACTION_THUMBS_DOWN
  • SEMANTIC_ACTION_CALLSEMANTIC_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

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"><Buttonandroid:id="@+id/btnSimpleNotification"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Simple Notification" /><Buttonandroid:id="@+id/btnNotificationIcon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Notification With Icon" /><Buttonandroid:id="@+id/btnNotificationImage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Notification With Image" /><Buttonandroid:id="@+id/btnNotificationWithGroupConvo"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Notification With Group Conversation" /><Buttonandroid: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 = "";@Overrideprotected 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);}@Overridepublic 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());}@Overrideprotected 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

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存储库中查看完整的源代码。

AndroidPNotificationsAndroidP通知
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/28473/android-p-notifications

Android P通知相关推荐

  1. android系统 通知管理,Android的通知系统

    Android的通知系统 默认分类 | 2015-07-07 08:21:24 | 阅读 1581 次 | 评论(0) : 将应用程序的一些重要信息通知给用户. 1.Toast 形式:一般在界面下半部 ...

  2. Android Notification通知详解

    Android Notification通知详解 Notification: (一).简介: 显示在手机状态栏的通知.Notification所代表的是一种具有全局效果的通知,程序一般通过Notifi ...

  3. Android R 通知新特性—人与对话(气泡窗)

    文章目录 对话 Conversation Space Bubbles 通知中心的Bubble 如何弹出Bubble(app端相关) 系统是如何弹出Bubble的(源码相关) Android R 通知新 ...

  4. Android Notification通知详细解释

    Android Notification通知具体解释 Notification: (一).简单介绍: 显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通知,程序一般通过N ...

  5. 利用android的通知Notification来实现msn的登录状态

    Notification,状态栏.看得出来就是和我们PC机一样,在某一区域有个标志,需要请求它就去点这个标志就可以打开这个程序了. 先来介绍下需要用到的一些属于android下的类:Intent.No ...

  6. android用来管理通知,Android 中通知的基本使用

    通知--一般当我在手记下拉的时候会发现一排信息提示(天气情况,qq消息,UC推荐,58推荐等等),这些就是通知. 通知分为三种:普通广播  自定义广播   大视图广播. 普通广播的创建: 通知的内容和 ...

  7. Android 消息通知

    Android 消息通知 文章目录 Android 消息通知 1. Toast 2. AlertDialog 2.1 普通对话框 2.2 带列表的对话框 2.3 带单选按钮的对话框 2.4 带多选按钮 ...

  8. Android发送通知——通知栏(Notification)

    Android发送通知--通知栏(Notification) 通知是指 Android 在您应用的界面之外显示的消息,旨在向用户提供提醒.来自他人的通信信息或您应用中的其他实时信息.在发出一条通知后, ...

  9. Android手机关闭短信提醒,有打扰 漏消息?那是Android手机通知设置没弄好!

    原标题:有打扰 漏消息?那是Android手机通知设置没弄好! 在Android系统手机的设置内容中,"通知"是最容易被我们忽略的选项.实际上,如果你每天休息时都会被各种推送提醒打 ...

最新文章

  1. java中 运算符
  2. 量化指标公式源码_通达信实用指标:《看涨跌》指标公式源码
  3. MVC模式在Java Web应用程序中的实例分析
  4. poj 2976 基础01分数规划
  5. 内核并发控制---读写自旋锁 (来自网易)
  6. Android Studio的Gradle的加速
  7. java可视化编程软件有哪些_几款Java开发者必备常用的工具,准点下班不在话下...
  8. Photoshop安装:详细安装步骤
  9. win10开启自带wifi共享操作步骤
  10. 几何光学学习笔记(20)- 5.3视场光阑
  11. Hadoop3.2.1 【 HDFS 】源码分析 : Secondary Namenode解析
  12. 数据库---四中连接查询(交叉、左连接、右连接、完整查询)
  13. 从《透视小邪医》谈玄幻修仙小说
  14. 2022年下半年软考网络规划设计师下午真题及答案解析
  15. H5图片高度根据宽度自适应
  16. 计算机基础题word,计算机应用基础试题(带答案)Word版
  17. 区间选点(贪心,区间问题)
  18. eNSP三个路由器两个pc连接
  19. noip2011 瑞士轮 (归并排序)
  20. 1234组成无重复三位数

热门文章

  1. PHP正则表达式详解(三)
  2. C#部分类与部分方法
  3. 用ajax向处理页面传送路径问题解决方法
  4. fatal error LNK1169: one or more multiply defined symbols found
  5. XTU 二分图和网络流 练习题 C. 方格取数(1)
  6. [转载] pandas将Series变成键值对
  7. [转载] Java构造方法、重载和重写
  8. PMP学习系列5:PMBOK(5th)第三章-项目管理过程
  9. vue路由传参丢失问题
  10. 记录一次有意思的XSS过滤绕过