通知栏Notification的整理

一、介绍       

  通知栏适用于交互事件的通知,是位于顶层可以展开的通知列表。

二、功能作用

  1.显示接收到短消息,及时消息等信息(如QQ、微信、新浪、短信)
       2.显示客户端的推送消息(如有新版本发布,广告。推荐新闻等)
  3.显示正在进行的事物(例如:后台运行的程序)(如音乐播放器、版本更新时候的下载进度等)

三、Notification

  1.属性:设置提醒标识符Flags
     功能:提醒标志符,向通知添加声音、闪光灯和震动效果等设置达到通知提醒效果,可以组合多个属性。
     例子:有两种设置方法:
    (1)实例化通知栏之后通过给他添加flags属性赋值。

Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;

    (2)通过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags。

PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), flags);

     提醒标识符成员:

Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中)
Notification.FLAG_INSISTENT //让声音、震动无限循环
Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL //用户点击通知后自动消失
Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除,不清除该通知(QQ的通知无法清除,使用这个)
Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务

  2.属性:Action[] actions
     功能:与NotificationCompat的addAction(int icon, CharSequence title, PendingIntent intent)方法功能相同。
     注意:要求API为19或者以上才能使用。
     例子:

Notification notification = mBuilder.build();
Notification.Action action = new Notification.Action(R.drawable.icon,"test action",actionPendingIntent);
Notification.Action[] actions = new Notification.Action[1];
actions[0] = action;
notification.actions = actions;

四、NotificationManager

       状态栏通知的管理类,负责发通知、清除通知等。

  1.方法:notify(int id, Notification notification)、notify (String tag, int id, Notification notification)
     功能:发送通知。
     例子:notify(1, mBuilder.build())。
  2.方法:cancel (int id)、cancel (String tag, int id)、cancelAll ()
     功能:取消通知。
     例子:cancel(1)。

五、NotificationCompat

  1.方法:setContentTitle(CharSequence title)
     功能:设置通知栏标题。
     例子:setContentTitle("测试标题")。
  2.方法:setContentText(CharSequence text)
     功能:设置通知栏显示内容。
     例子:setContentText("测试内容")。
  3.方法:setContentIntent(PendingIntent intent)
     功能:设置通知栏点击意图。
     例子:setContentIntent(PendingIntent.getActivity(this, 1, new Intent(), flags))。
  4.方法:setTicker(CharSequence tickerText)、setTicker(CharSequence tickerText, RemoteViews views)
     功能:设置通知在第一次到达时在状态栏中显示的文本。
   注意:5.0及之后没有效果。
     例子:setTicker("测试通知来啦")。
  5.方法:setWhen(long when)
   功能:通知产生的时间,会在通知栏信息里显示,一般是系统获取到的时间。
   例子:setWhen(System.currentTimeMillis())。
  6.方法:setPriority(int pri)
   功能:设置通知优先级。
   例子:setPriority(Notification.PRIORITY_DEFAULT)。
   参数属性:

Notification.PRIORITY_DEFAULT //默认优先级,用于没有特殊优先级分类的通知
Notification.PRIORITY_HIGH //高优先级,用于重要的通信内容,例如短消息或者聊天
Notification.PRIORITY_LOW //低优先级,可以通知用户但又不是很紧急的事件
Notification.PRIORITY_MAX //重要而紧急的通知,通知用户这个事件是时间上紧迫的或者需要立即处理的
Notification.PRIORITY_MIN //用于后台消息(例如天气或者位置信息)。最低优先级通知将只在状态栏显示图标,只有用户下拉通知抽屉才能看到内容。

  7.方法:setNumber(int number)
   功能:设置通知集合的数量。
   例子:setNumber(10)。
  8.方法:setAutoCancel(boolean autoCancel)
   功能:true,当用户点击面板就可以让通知自动取消。
   例子:setAutoCancel(true)。
  9.方法:setOngoing(boolean ongoing)
   功能:true,设置它为一个正在进行的通知,通常表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)。
   例子:setOngoing(false)。
  10.方法:setDefaults(int defaults)
    功能:向通知添加声音、闪灯和震动效果,最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合Notification.DEFAULT_ALL、Notification.DEFAULT_SOUND添加声音。
    例子:setDefaults(Notification.DEFAULT_VIBRATE)。
    参数属性:

Notification.DEFAULT_VISIBLE //添加默认震动提醒 需要VIBRATE permission
Notification.DEFAULT_SOUND //添加默认声音提醒
Notification.DEFAULT_LIGHTS //添加默认三色灯提醒
Notification.DEFAULT_ALL //添加默认以上三种全部提醒

  11.方法:setSmallIcon(int icon)
       功能:设置通知的小Icon。
     例子:setSmallIcon(R.mipmap.ic_launcher)。
  12.方法:setCategory(String category)
     功能:设置通知类别。
     例子:setCategory(Notification.CATEGORY_MESSAGE)。
     参数属性:

Notification.CATEGORY_CALL //呼入(语音或视频)或类似的同步通信请求。
Notification.CATEGORY_MESSAGE //直接消息(短信、即时消息等)。
Notification.CATEGORY_EMAIL //异步批量消息(电子邮件)。
Notification.CATEGORY_EVENT //日历事件。
Notification.CATEGORY_PROMO //促销或广告。
Notification.CATEGORY_ALARM //闹钟或定时器。
Notification.CATEGORY_PROGRESS //长时间后台操作的进展。
Notification.CATEGORY_SOCIAL //社交网络或共享更新。
Notification.CATEGORY_ERROR //后台操作或者身份验证状态出错。
Notification.CATEGORY_TRANSPORT //回放媒体传输控制。
Notification.CATEGORY_SYSTEM //系统或者设备状态更新,预留给系统使用。
Notification.CATEGORY_SERVICE //运行后台服务的指示。
Notification.CATEGORY_RECOMMENDATION //针对某一事物的具体及时的建议。
Notification.CATEGORY_STATUS //关于设备或者上下文状态的正在进行的信息。
Notification.CATEGORY_REMINDER //用户预定提醒。

  13.方法:setColor(@ColorInt int argb)
       功能:设置通知栏颜色。
       例子:setColor(0x667788)。
  14.方法:setContentInfo(CharSequence info)
       功能:在通知的右侧设置大文本。
       例子:setContentInfo("大文本")。
  15.方法:setLocalOnly(boolean b)
       功能:设置此通知是否仅与当前设备相关,如果设置为true,通知就不能桥接到其他设备上进行远程显示。 
       例子:setLocalOnly(true)。
  16.方法:setVibrate(long[] pattern)
       功能:设置使用震动模式。
       例子:setVibrate(new long[] {0,300,500,700})。延迟0秒,然后震动300ms,再延迟500ms,接着震动700ms。
  17.方法:setUsesChronometer(boolean b)
     功能:设置是否显示时间计时,电话通知就会使用到。
     例子:setUsesChronometer(true)。
  18.方法:setRemoteInputHistory(CharSequence[] text)
     功能:设置远程输入历史。
     例子:setRemoteInputHistory(new CharSequence[] {"1","2","3"})。
  19.方法:setOnlyAlertOnce(boolean onlyAlertOnce)
     功能:设置仅提醒一次。
     例子:setOnlyAlertOnce(true)。
  20.方法:setLights(@ColorInt int argb, int onMs, int offMs)
     功能:android支持三色灯提醒,这个方法就是设置不同场景下的不同颜色的灯。
     注意:(1)只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持三色灯提醒。
        (2)这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。
      例子:setLights(Oxff0000ff, 300, 0)

              其中argb表示灯光颜色,onMs表示亮持续时间,offMs表示暗的时间。
              还有一种写法是:

Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = 0xff0000ff;
notify.ledOnMS = 300;
notify.ledOffMs = 300;

  21.方法:setSound(Uri sound)、setSound(Uri sound, int streamType)、setSound(Uri sound, AudioAttributes audioAttributes)
     功能:设置默认或者自定义的铃声来提醒。
     例子:setDefaults(Notification.DEFAULT_SOUND);//获取默认铃声
          setSound(Uti.parse("file:///sdcard/xx/xx.mp3"));//获取自定义铃声
        setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URL, "5"));//获取Android多媒体库内的铃声
      其中streamType指的铃声的使用流。
     参数属性:

AudioManager.STREAM_MUSIC //用于音乐播放的音频流
AudioManager.STREAM_RING //用于电话铃声的音频流
AudioManager.STREAM_VOICE_CALL //用于电话呼叫的音频流
AudioManager.STREAM_SYSTEM //用于系统声音的音频流
AudioManager.STREAM_ALARM //用于警报的音频流
AudioManager.STREAM_NOTIFICATION //用于通知的音频流
AudioManager.STREAM_BLUETOOTH_SCO //蓝牙已经连接的电话呼叫的音频流
AudioManager.STREAM_SYSTEM_ENFORCED //在特定国家强制系统声音的音频流(比如日本的照相机)
AudioManager.STREAM_DTMF //用于DTMF(双音多频电话)音调的音频流
AudioManager.STREAM_TTS //TTS(文本到语音)的音频流

  22.方法:setProgress(int max, int progress, boolean indeterminate)
     功能:设置带进度条的通知,可以在下载中使用。
     注意:此方法在4.0及以后版本才有用,如果为早期版本,需要自定义通知布局,其中包含ProgressBar视图。
     例子:setProgress(100, 66, true)。
        其中max表示精度条最大数值,progress表示当前进度,indeterminate表示进度是否不确定,true为不确定,false为确定。
        如果为确定的进度条,调用setProgress(max, progress, false)来设置通知,在更新进度的时候在此发起通知更新progress,并且在下载完成后要移除进度条,通过调用setProgress(0,0,false)即可。
        如果为不确定(持续活动)的进度条,这是在处理进度无法准确获知时显示活动正在持续,所以调用setProgress(0,0,true),操作结束时,调用setProgress(0,0,false)并更新通知以移除指示条。
  23.方法:build()
     功能:整合所有已设置的选项,返回一个新的Notification对象。
     例子:build()。
  24.方法:setStyle(Style style)
     功能:在构建应用时添加一个丰富的通知样式。
     例子:setStyle(inboxStyle)
     参数属性:

BigPictureStyle //用于生成包含大图像附件的大格式通知的辅助类
BigTextStyle //用于生成包含大量文本的大格式通知的辅助类
MessagingStyle //用于生成大格式通知的辅助类,其中包含任意数量的不同类型的多个来回消息
InboxStyle //用于生成大格式通知的辅助类,其中包含一个(最多5个)字符串列表

  25.方法:addAction(int icon, CharSequence title, PendingIntent intent)、addAction(Notification.Action action)
     功能:向通知添加操作,操作通常与通知的content相连被系统作为按钮来显示。在系统的content下方显示图片与title,点击这个图片或者title就会触发设置的intent。
     注意:Android 4.1版本及其以上才能使用,之下版本不能使用。
     例子:addAction(R.drawable.icon,"test action",actionPendingIntent)
  26.方法:setContent(RemoteViews views)
     功能:支持使用自定义视图替代标准的视图。
     例子:setContent(mRemoteViews)。
  27.方法:setLargeIcon(Bitmap icon)
     功能:设置下拉列表中的图标(大图标)
     例子:setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon))。
  28.方法:setPublicVersion(Notification n)
     功能:设置安全锁屏下的通知
     例子:setPublicVersion(publicBuilder.build())。
  29.方法:setDeleteIntent(PendingIntent intent)
     功能:设置通知删除时的意图
     例子:setDeleteIntent(deletePendingIntent)。
  30.方法:setExtras(Bundle extras)
     功能:为通知设置数据。
     例子:setExtras(bundle),不太清楚主要用于做什么。
  31.方法:setSubText(CharSequence text)
     功能:在通知模板上设置第三行文本。
     注意:如果使用了setProgress(),就不要使用setSubText()了,它们占据同一个位置。
        如果不提供大格式通知,则此方法没有效果,第三行文本只出现在展开视图中。
     例子:setSubText("sub text")。
  32.方法:setFullScreenIntent(PendingIntent intent, boolean highPriority)
     功能:只适用于具有极高优先级的通知,比如电话或者闹铃,如果设备被用用于其他东西,请给用户一个选项关闭它并使用一个正常的通知,因为这个可能会有破坏性。
     例子:setFullScreenIntent(fullScreenIntent, true)。
  33.方法:setGroup(String groupKey)
     功能:设置该通知组的密钥,即确认为哪一组。
     例子:setGroup(“test_notification”)。
  34.方法:setGroupSummary(boolean isGroupSummary)
     功能:设置是否为一组通知的第一个显示。
     例子:setGroupSummary(true)。
  35.方法:setSortKey(String sortKey)
     功能:设置针对一个包内的通知进行排序的键值,
     例子:setSortKey("sort")

五、Notification的普通使用

  1.获取状态通知栏管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  2.实例化通知栏的Builder构造器

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

  3.对Builder进行配置

mBuilder.setContentTitle("测试标题")//设置通知栏标题
.setContentText("测试内容")//设置通知栏显示内容
.setTicker("测试通知来啦")//设置通知在第一次到达时在状态栏中显示的文本
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知栏信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT)//设置通知优先级
.setNumber(10)//设置通知集合的数量
.setAutoCancel(true)//当用户点击面板就可以让通知自动取消
.setOngoing(false)//true,设置它为一个正在进行的通知,通常表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)。
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合Notification.DEFAULT_ALL、 Notification.DEFAULT_SOUND添加声音
.setSmallIcon(R.mipmap.ic_launcher)//设置通知小Icon
.setCategory(Notification.CATEGORY_MESSAGE)//设置通知类别
.setColor(0x0000ff)//设置通知栏颜色
.setContentInfo("大文本")//在通知的右侧设置大文本
.setLocalOnly(true)//设置此通知是否仅与当前设备相关。如果设置为true,通知就不能桥接到其他设备上进行远程显示。
.setVibrate(new long[]{0, 300, 500, 700})//设置使用振动模式
.setUsesChronometer(true)//设置是否显示时间计时,电话通知就会使用到
.setRemoteInputHistory(new CharSequence[]{"1", "2", "3"})//设置远程输入历史
.setOnlyAlertOnce(true);//设置仅提醒一次。

  4.设置通知栏点击事件

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent)//设置通知栏点击意图

  5.发通知

mNotifacationManager.notify(1, mBuilder.build());

六、自定义Notification样式

  1.创建通知布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/custom_song_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/sing_icon" />

    <LinearLayout
        android:id="@+id/ll_custom_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dip"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/btn_custom_prev"
            style="@style/btn_custom_style"
            android:src="@drawable/btn_prev" />

        <ImageButton
            android:id="@+id/btn_custom_play"
            style="@style/btn_custom_style"
            android:contentDescription="播放"
            android:src="@drawable/btn_play" />

        <ImageButton
            android:id="@+id/btn_custom_next"
            style="@style/btn_custom_style"
            android:contentDescription="下一首"
            android:src="@drawable/btn_next" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toLeftOf="@id/ll_custom_button"
        android:layout_toRightOf="@id/custom_song_icon">

        <TextView
            android:id="@+id/tv_custom_song_singer"
            style="@style/NotificationTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="title"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_custom_song_name"
            style="@style/NotificationContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="content"
            android:textSize="12sp" />
    </RelativeLayout>

  2.设置接受自定义界面按钮点击事件的广播

   public class ButtonBroadcastReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "onreceiver");
            String action = intent.getAction();
            if (action.equals(ACTION_BUTTON)) {
                int buttonId = intent.getIntExtra("ButtonId", 0);
                switch (buttonId) {
                    case 1:
                        Log.d(TAG, "上一首");
                        Toast.makeText(MainActivity.this, "上一首", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        String play_status = "";
                        isPlay = !isPlay;
                        if (isPlay) {
                            play_status = "开始播放";
                        } else {
                            play_status = "已暂停";
                        }
                        Log.d(TAG, "play_status = " + play_status);
                        showButonNotify();
                        Log.d(TAG, "showButonNotify");
                        Log.d(TAG, play_status);
                        Toast.makeText(MainActivity.this.getApplicationContext(), play_status, Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Log.d(TAG, "下一首");
                        Toast.makeText(MainActivity.this.getApplicationContext(), "下一首", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

  3.注册广播

public ButtonBroadcastReceiver mButtonReceicer;
mButtonReceicer = new ButtonBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_BUTTON);
registerReceiver(mButtonReceicer, intentFilter);

  4.解绑广播

    @Override
    protected void onDestroy() {
        if (mButtonReceicer != null) {
            unregisterReceiver(mButtonReceicer);
        }
        mNotifacationManager.cancel(2);//在应用退出的时候,关闭通知。
        super.onDestroy();
    }

  5.设置通知布局

RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button);
        mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon);
        //API 3.0以上的时候显示按钮,否则消失
        mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周杰伦");
        mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七里香");
        //如果版本号低于3.0,那么不显示按钮
        if (Build.VERSION.SDK_INT <= 9) {
            mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);
        } else {
            mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);
        }

        if (isPlay) {
            mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);
        } else {
            mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);
        }

        //点击的事件处理
        Intent buttonIntent = new Intent(ACTION_BUTTON);
        //上一首按钮
        buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);
        //这里加了广播,所及INTENT的必须用getBroadcast方法
        PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);
        //播放/暂停 按钮
        buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
        PendingIntent intent_play = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_play);
        //下一首按钮
        buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);
        PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);

  6.获取状态通知栏管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  7.实例化通知栏的Builder构造器

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

  8.对Builder进行配置

        mBuilder.setContent(mRemoteViews)
                .setWhen(System.currentTimeMillis())
                .setTicker("正在播放")
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setOngoing(true)
                .setSmallIcon(R.drawable.sing_icon);

  9.设置通知栏点击事件

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);

  10.发通知

Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_ONGOING_EVENT;
mNotifacationManager.notify(2, notify);

七、设置通知样式

  1.创建BigPictureStyle样式

NotificationCompat.BigPictureStyle inboxStyle = new NotificationCompat.BigPictureStyle();
inboxStyle.setBigContentTitle("大视图内容");
inboxStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
inboxStyle.bigLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round));

  2.获取状态通知栏管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  3.实例化通知栏的Builder构造器

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

  4.对Builder进行配置

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        
        mBuilder.setContentIntent(pendingIntent)
                .setWhen(System.currentTimeMillis())
                .setTicker("大图测试")
                .setStyle(inboxStyle)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setNumber(10)
                .setContentTitle("大图风格")
                .setSmallIcon(R.drawable.icon)
                .setContentText("大图");

  6.发通知

mNotifacationManager.notify(3, mBuilder.build());

八、设置分组通知  

  1.获取状态通知栏管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  2.实例化多个通知栏的Builder构造器

NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this);
NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this);
NotificationCompat.Builder mBuilder3 = new NotificationCompat.Builder(this);
NotificationCompat.Builder mBuilder4 = new NotificationCompat.Builder(this);

  3.对多个Builder进行配置

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

mBuilder1.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("first ticker")
         .setContentTitle("first content title")
         .setContentText("first content text")
         .setContentInfo("first content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("a")
         .setGroupSummary(true)//作为这组信息的第一个显示
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

mBuilder2.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("second ticker")
         .setContentTitle("second content title")
         .setContentText("second content text")
         .setContentInfo("second content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("a")
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

mBuilder3.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("third ticker")
         .setContentTitle("third content title")
         .setContentText("third content text")
         .setContentInfo("third content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("b")
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

mBuilder4.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("forth ticker")
         .setContentTitle("forth content title")
         .setContentText("forth content text")
         .setContentInfo("forth content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("b")
         .setGroupSummary(true)
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

  4.发通知

mNotifacationManager.notify(4, mBuilder1.build());
mNotifacationManager.notify(5, mBuilder2.build());
mNotifacationManager.notify(6, mBuilder3.build());
mNotifacationManager.notify(7, mBuilder4.build());

 

代码地址:https://github.com/ZhangMiao147/NotificationDemo

参考文章
http://blog.csdn.net/aqi00/article/details/50540837
http://blog.csdn.net/vipzjyno1/article/details/25248021/
http://iluhcm.com/2017/03/12/experience-of-adapting-to-android-notifications/index.html#RemoteViews适配

posted on 2017-06-26 15:54  zhangmiao14  阅读(11414)  评论(0编辑  收藏  举报