跳转至

Android 通知 Notification 使用

更新日期 2024-05-21
  • 2024-05-21 创建文档

介绍

  • NotificationManager - 用于提示的管理,例如发送、取消
  • NotificationCompat.Builder - Builder模式构造notification;可参考《Effective Java》第2条
  • Notification - 提示,能够显示在状态栏和下拉栏上;构造实例能设定flags

Notification使用示例

本文记录Android Notification的使用方法。

界面中放置了很多个按钮,每个按钮发送的提示并不完全相同。流程都一样。

设定一个NotificationManager,使用NotificationCompat.Builder来建立Notification;点击按钮时NotificationManager.notify发送提示

示例的gradle配置:

  • compileSdk 34
  • minSdk 23
  • targetSdk 34

点击启动MainActivity的例子

  • setSmallIcon是不可缺少的
  • MainActivity launchMode="singleInstance";便于返回 activity
  • Notification.FLAG_NO_CLEAR 下拉栏点击清除全部时,仍然保留
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent,
        PendingIntent.FLAG_IMMUTABLE);

String channelId = "defaultChannelId000";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(getApplicationContext(), channelId)
                // 必须设置small icon
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("通知1")
                .setContentText("Notification.FLAG_NO_CLEAR 点击回到app")
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(channelId,
            "Channel human readable title",
            NotificationManager.IMPORTANCE_HIGH);
    notificationManager.createNotificationChannel(channel);
}
Notification notification = notificationBuilder.build();
// 下拉栏点击清除全部时,仍然保留
notification.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(0, notification);

自动消失的例子

使用setTimeoutAfter()方法可以设置通知在多久之后自动消失,即使用户没有与之交互。

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent,
        PendingIntent.FLAG_IMMUTABLE);

String channelId = "defaultChannelId000";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(getApplicationContext(), channelId)
                // 必须设置small icon
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("2秒消失通知")
                .setContentText("设置了setTimeoutAfter")
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setTimeoutAfter(2000);

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(channelId,
            "Channel human readable title",
            NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(channel);
}
Notification notification = notificationBuilder.build();
// 用户点击后消失
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(3 /* ID of notification */, notification);

清除指定通知

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId); // notificationId是你之前用来显示通知的ID
本站说明

一起在知识的海洋里呛水吧。广告内容与本站无关。如果喜欢本站内容,欢迎投喂作者,谢谢支持服务器。如有疑问和建议,欢迎在下方评论~

📖AndroidTutorial 📚AndroidTutorial 🙋反馈问题 🔥最近更新 🍪投喂作者

Ads