Android P通知
时间:2020-02-23 14:29:08 来源:igfitidea点击:
在本教程中,我们将讨论在通知系统及其Android P用户界面中引入的更改。
我们将通过一个简单的Android应用程序进行演示。
Android P通知
Android Pie引入了新的消息样式通知UI,该UI提供了简化的对话。
而且,我们现在也可以以消息传递样式显示带有文本的图像。
在Android P中,MessagingStyle类的addMessage()函数已更改。
现在,我们没有传递字符串,而是传递了" Person"对象,该对象将通知及其消息绑定到该人。
setData用于在消息上显示图像。
setSemanticAction用于设置对通知动作的预定义引用。
以下是可用的语义动作:
- SEMANTIC_ACTION_NONE
- SEMANTIC_ACTION_REPLY
- SEMANTIC_ACTION_MARK_AS_READ
- SEMANTIC_ACTION_MARK_AS_UNREAD
- SEMANTIC_ACTION_DELETE
- SEMANTIC_ACTION_ARCHIVE
- SEMANTIC_ACTION_MUTE
- SEMANTIC_ACTION_UNMUTE
- SEMANTIC_ACTION_THUMBS_UP
- SEMANTIC_ACTION_THUMBS_DOWN
- SEMANTIC_ACTION_CALL
setGroupConversation用于将通知组标识为对话。
在下一节中,我们将在Android P中实现不同类型的通知。
简单消息通知
private void simpleNotification() {
Person jd = new Person.Builder()
.setName("theitroad")
.setImportant(true)
.build();
new NotificationCompat.MessagingStyle(jd)
.addMessage("Check me out", new Date().getTime(), jd)
.setBuilder(builder);
notificationManager.notify(1, builder.build());
}
在addMessage内部,我们传递消息,time(long)和Person对象。
setImportant true表示如果该通知被折叠,则该消息将显示在对话的顶部。
带图标的消息通知
默认情况下,显示的图标是"人员"的首字母。
我们可以如下所示设置自定义图标:
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());
带有图像的通知
我们可以在setData方法中设置图像类型和uri,如下所示:
private void notificationWithImage() {
Person bot = new Person.Builder()
.setName("Bot")
.setImportant(true)
.setBot(true)
.build();
Uri uri = Uri.parse("android.resource://com.theitroad.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设置为true表示Person类型是机器。
消息对话通知
private void notificationWithGroupConvo()
{
Person jd = new Person.Builder()
.setName("theitroad")
.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.theitroad.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());
}
消息按照设置" addMessage"方法的顺序显示。
在以下部分中,我们将汇总以上学习的概念以及语义动作。
我们将使用AndroidX包装系统。
项目结构
Android P通知项目
代码
下面给出了" 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>
MainActivity.java类的代码如下:
package com.theitroad.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("theitroad")
.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.theitroad.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("theitroad")
.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.theitroad.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("theitroad")
.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.theitroad.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();
}
}
}
单击语义动作按钮时,我们将在Toast中显示意图数据。

