使用通知
在Android 8.0(API26)后对通知更改了一些内容,以前是通过Notification,Builder(Context context).se.来设置通知的震动、灯光、音效的设置,新内容加了NotificationChannel(通知渠道),通过NotificatonChannel来进行震动、灯光、音效的设置,且通知必须添加通知渠道,同样需进行版本判断,否则通知不会被发送。
- 首先创建NotificationManager对象
- .创建Notificationchannel对象,指定id,name,importance(根据这个播放对应的效果),也可以设置相应的震动、音效、如果没设置按默认的importance,调用NotificationManager的createNotificationChannel(NotificationChannel channel)方法
- 通过Notificationcompat.Builder(Context.Notification)对象指定channel并设置通知的各项信息,调用buid()方法创建Notifcation对象
- 调用NotificationManager的notify(id,Notification)方法发送通知
基础用法
新建NotificationTest项目
修改activity中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/send_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send notice"/>
</LinearLayout>
简单加了一个Send notice按钮
下来修改MainActivity中的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
}
@SuppressLint("NotificationPermission")
@Override
public void onClick(View v) {
if (v.getId() == R.id.send_notice) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
manager.notify(1, notification);
}
}
}
上面是Android8.0版本前才能使用
在Android13,版本通知的使用发生了新的变化:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String CHANNEL_ID = "notification_channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
createNotificationChannel();
}
// 创建通知渠道
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Notification Channel";
String description = "Channel for notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
@SuppressLint("NotificationPermission")
@Override
public void onClick(View v) {
// 申请通知权限
if (ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
android.Manifest.permission.POST_NOTIFICATIONS}, 1);
}
if (v.getId() == R.id.send_notice) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher) // 设置小图标
.setContentTitle("This is content title") // 设置通知标题
.setContentText("This is content text") // 设置通知内容
.setPriority(NotificationCompat.PRIORITY_DEFAULT) // 设置通知优先级
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) // 设置大图标
.setWhen(System.currentTimeMillis()); // 设置通知时间
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build()); // 发送通知
}
}
}
现在可以为弹出的消息设置点击事件:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String CHANNEL_ID = "notification_channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Notification Channel";
String description = "Channel for notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
@SuppressLint("NotificationPermission")
@Override
public void onClick(View v) {
//点击通知后跳转页面
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
//申请通知权限
if (ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String