美文网首页
Android 通知的简单使用

Android 通知的简单使用

作者: 酷酷的Demo | 来源:发表于2019-06-19 11:08 被阅读0次

如何创建通知

随着Android系统不断升级,Notification的创建方式也随之变化,主要变化如下:

Android 3.0之前

Android 3.0 (API level 11)之前,使用new Notification()方式创建通知:

NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
      this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, content, contentIntent);

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);

Android 3.0 (API level 11)及更高版本

Android 3.0开始弃用new Notification()方式,改用Notification.Builder()来创建通知:

NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
      this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setContentIntent(contentIntent)
            .build();// getNotification()

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);

这里需要注意: "build()" 是Androdi 4.1(API level 16)加入的,用以替代
"getNotification()"。API level 16开始弃用"getNotification()"

通知基本用法

  • 一个通知必须包含以下三项属性:
    小图标,对应 setSmallIcon()
    通知标题,对应 setContentTitle()
    详细信息,对应 setContentText()
    其他属性均为可选项,更多属性方法请参考https://blog.csdn.net/yxncl/article/details/72801230
  • 创建通知
    1、实例化一个NotificationCompat.Builder对象
NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

NotificationCompat.Builder自动设置的默认值:
priority: PRIORITY_DEFAULT
when: System.currentTimeMillis()
audio stream: STREAM_DEFAULT

  • 定义并设置一个通知动作(Action)
Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
            this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
  • 生成Notification对象
Notificatioin notification = mBuilder.build();
  • 使用NotificationManager发送通知
// Sets an ID for the notification
int mNotificationId = 001;

// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, notification);
  • 更新通知
    更新通知很简单,只需再次发送相同ID的通知即可,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建。示例代码:
NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
NotificationCompat.Builder mNotifyBuilder = 
      new NotificationCompat.Builder(this)
          .setContentTitle("New Message")
          .setContentText("You've received new messages.")
          .setSmallIcon(R.drawable.ic_notify_status);

int numMessages = 0;
...
    mNotifyBuilder.setContentText("new content text")
            .setNumber(++numMessages);
    mNotifyMgr.notify(notifyID, mNotifyBuilder.build());
...
  • 取消通知
    取消通知有如下4种方式:

点击通知栏的清除按钮,会清除所有可清除的通知
设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
通过 NotificationManager 调用 cancel() 方法清除指定ID的通知
通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知

通知类型

通知有两种视图:普通视图和大视图。
普通视图:


image

大视图:
[图片上传失败...(image-f0e56-1560913700779)]

默认情况下为普通视图,可通过NotificationCompat.Builder.setStyle()设置大视图。

注: 大视图(Big Views)由Android 4.1(API level 16)开始引入,且仅支持Android 4.1及更高版本。

相关文章

  • Android 通知的简单使用

    如何创建通知 随着Android系统不断升级,Notification的创建方式也随之变化,主要变化如下: And...

  • Android Notificatin 通知

    通知的简单使用 1-通知渠道:android 8.0 必须设置通知渠道,否则通知不会显示。2-通知图标问题:and...

  • Android通知栏显示通知简单使用

    最近做直播,要求向关注者发通知,显示在通知栏, 记录下简单的使用。 并发现一个在魅族手机上奇葩的坑。。。 直接上代...

  • android学习之使用Notification

    通知栏是android中一个很常用的控件,其使用也非常简单

  • 《Android第一行代码》first reading 十

    Android多媒体运用 一 通知 使用Android通知功能步骤: 通过Context的getSystemSer...

  • Android通知快照

    简单整理一下android通知,快速使用,不深入讲解。需要完全掌握通知还请仔细学习官方文档。参考官方文档:http...

  • Android Notification通知

    Android官网通知通知是Android中经常使用的一个功能,本文记录以8.0版本为分割的通知使用方式,下图为通...

  • 简单使用通知

    之前用通知老是不知道该在哪个界面定义通知,哪个界面调用通知,哪里移除通知。即使我当前知道了,但是没过多久我就又忘记...

  • 通知简单使用

    1.创建通知,通过通知中心发送通知(比如你在a控制器想让b控制器做点什么的时候,并且控制器没什么关系的情况下) N...

  • 通知的简单使用

    模型数据

网友评论

      本文标题:Android 通知的简单使用

      本文链接:https://www.haomeiwen.com/subject/fwyhqctx.html