Android 之 Android N 上的 notification 归类分组功能

3,851 阅读6分钟

Android之Android N 上的notification归类功能

文章链接:blog.csdn.net/qq_16628781…

知识点:

  1. Android通知的优缺点及改善方法;
  2. Android N中使用通知归类功能;
  3. 新名词记录{StatusBarNotification:每一个通知的状态类,包含notification原始的tag,id等信息,由notify(String, int, Notification)发出的通知,可以使用getTag(),getId(),getNotification()等拿到对应的信息;ViewAnimator:setDisplayedChild(1)显示第1个view,直接控件有几个就对应几个子view;}

概述

现在10个APP有9个都会用到消息推送,无论是最基本的版本更新、单点登录还是比较友好的消息,都需要进行消息推送。即使用户没有打开APP,也能将用户需要知晓的信息呈现给用户。消息推送是在开发中一个很重要的功能。

但是有一点很不好的就是,一个APP可能短时间之内会推送很多条消息过来,比如新闻类的APP,对一些突发的热点事件需要的比较高同步性,就有可能一下子推送N多条信息给用户(即使没打开APP之前不推送给你,但是很多推送默认都会给一个消息2小时的保留时间,2小时之内打开了APP,还是会推送到客户端),结果是消息都堆在一起了,可谓大大降低了用户的体验。

其中一个治标的办法:对不想要接收到推送的APP进行关闭推送消息给你,那么系统就会帮你过滤掉所有此APP发出的通知消息(记得是M开始就可以了)。很显然,这样是治标不治本的,用户还是有需要在通知栏中知道一些APP的推送消息的,凡事不能一刀切了。

既然不能全部关掉,那么我进行管理,让它看起来不要这么乱,体验不就改善了吗!

在Android最新的“N(牛轧糖)”版本中,关于通知栏有一个比较重要的改变:允许统同一应用程序将所有的通知“集成”在一个归类群组(cluster)里面,方便管理。只要你设置了同样的一个id,那么就可以将消息归类了。

这是一个比较容易理解的功能,就是一个应用发的消息,都是放在同一个集合里面去,然后统一管理或者取消。代码也比较容易实现,思路是:发送通知时,给我们的通知设置一个groupKey,并发送;因为发送的是一个普通的通知,所以我们还需要去检查是否有同样groupKey的通知,如果有,就再发送一个整理-归类的通知,让系统帮我们归类具有相同groupKey的通知成一个二级菜单列表。


先上图:
这里写图片描述
下面我们直接看代码,因为代码里我已经做了很详细的解释了。请认真去看:

public class ActiveNotificationsFragment extends Fragment {

    /**
     * 删除通知的请求码
     */
    private static final int REQUEST_CODE = 2323;

    private static final String TAG = "ActiveNotificationsFragment";

    /**
     * 归类的key
     */
    private static final String NOTIFICATION_GROUP =
            "com.example.android.activenotifications.notification_type";

    /**
     * 通知组别的id
     */
    private static final int NOTIFICATION_GROUP_SUMMARY_ID = 1;

    /**
     * 通知管理类
     */
    private NotificationManager mNotificationManager;

    private TextView mNumberOfNotifications;

    /**
     * 这是每一个通知的唯一id,如果不唯一,前一个通知会被后一个通知覆盖
     */
    private static int sNotificationId = NOTIFICATION_GROUP_SUMMARY_ID + 1;

    /**
     * 删除按钮触发的意图
     */
    private PendingIntent mDeletePendingIntent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_notification_builder, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        updateNumberOfNotifications();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mNotificationManager = (NotificationManager) getActivity().getSystemService(
                Context.NOTIFICATION_SERVICE);
        mNumberOfNotifications = (TextView) view.findViewById(R.id.number_of_notifications);

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.add_notification: {
                        addNotificationAndUpdateSummaries();
                        break;
                    }
                }
            }
        };
        view.findViewById(R.id.add_notification).setOnClickListener(onClickListener);

        //创建一个当用户在通知栏面板点击"删除"按钮后触发的PendingIntent意图
        Intent deleteIntent = new Intent(ActiveNotificationsActivity.ACTION_NOTIFICATION_DELETE);
        mDeletePendingIntent = PendingIntent.getBroadcast(getActivity(),
                REQUEST_CODE, deleteIntent, 0);
    }

    /**
     * 发送通知到系统。如果设备支持消息的归类功能,则进行消息归类。
     */
    private void addNotificationAndUpdateSummaries() {
        // 创建通知,并发送到状态栏
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity())
                .setSmallIcon(R.mipmap.ic_notification)
                .setContentTitle("消息的标题")
                .setContentText("消息的内容")
                .setAutoCancel(true) //自动取消
                .setDeleteIntent(mDeletePendingIntent) //设置删除的意图
                .setGroup(NOTIFICATION_GROUP); //设置类组key,说明此条通知归属于哪一个归类

        final Notification notification = builder.build();

        //这里需要为每一个通知创建一个新的id,如果id相同并且上一条通知还没有被用户取消,就会覆盖上一条通知
        mNotificationManager.notify(getNewNotificationId(), notification);

        //如果有必要,增加/更新/移除通知的归类
        updateNotificationSummary();

        //show有多少条通知在状态栏上面
        updateNumberOfNotifications();
    }

    /**
     * 如果有必要,增加/更新/移除通知的归类
     */
    protected void updateNotificationSummary() {
        int numberOfNotifications = getNumberOfNotifications();
        if (numberOfNotifications > 1) { //如果数量>=2,说明有了同样组key的通知,需要归类起来
            //将通知添加/更新归类到同一组下面
            String notificationContent = getString(R.string.sample_notification_summary_content,
                    numberOfNotifications);
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity())
                    .setSmallIcon(R.mipmap.ic_notification)
                    //添加富样式到通知的显示样式中,如果当前系统版本不支持,那么将不起作用,依旧用原来的通知样式
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .setSummaryText(notificationContent))
                    .setGroup(NOTIFICATION_GROUP) //设置类组key,说明此条通知归属于哪一个归类
                    .setGroupSummary(true); //这句话必须和上面那句一起调用,否则不起作用
            final Notification notification = builder.build();
            //发送通知到状态栏
            //测试发现,发送归类状态栏也是算一条通知的。所以返回值得时候,需要-1
            mNotificationManager.notify(NOTIFICATION_GROUP_SUMMARY_ID, notification);
        } else {
            //移除归类
            mNotificationManager.cancel(NOTIFICATION_GROUP_SUMMARY_ID);
        }
    }

    /**
     * 显示有多少条通知在状态栏上面
     */
    protected void updateNumberOfNotifications() {
        final int numberOfNotifications = getNumberOfNotifications();
        mNumberOfNotifications.setText("状态栏上面有多少条通知:%1$d" + numberOfNotifications);
    }

    /**
     * 返回一个通知的唯一id
     */
    public int getNewNotificationId() {
        int notificationId = sNotificationId++;
        if (notificationId == NOTIFICATION_GROUP_SUMMARY_ID) {
            notificationId = sNotificationId++;
        }
        return notificationId;
    }

    /**
     * 获取当前状态栏具有统一id的通知的数量
     *
     * @return 数量
     */
    private int getNumberOfNotifications() {
        //查询当前展示的所有通知的状态列表
        final StatusBarNotification[] activeNotifications = mNotificationManager
                .getActiveNotifications();

        //获取当前通知栏里头,NOTIFICATION_GROUP_SUMMARY_ID归类id的组别
        //因为发送分组的通知也算一条通知,所以需要-1
        for (StatusBarNotification notification : activeNotifications) {
            if (notification.getId() == NOTIFICATION_GROUP_SUMMARY_ID) {
                //-1是因为
                return activeNotifications.length - 1;
            }
        }
        return activeNotifications.length;
    }

}

在上面的代码中,我们在通知中设置了监听用户点击清除通知按钮的操作。我们就可以在用户通知了通知栏之后,做一些必要的响应,比如这里我做了一个更新UI的操作。代码如下:

private ActiveNotificationsFragment mFragment;

    protected static final String ACTION_NOTIFICATION_DELETE
            = "com.example.android.activenotifications.delete";

    private BroadcastReceiver mDeleteReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mFragment == null) {
                findFragment();
            }
            mFragment.updateNumberOfNotifications();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        findFragment();
        mFragment.updateNumberOfNotifications();
    }

    private void findFragment() {
        mFragment = (ActiveNotificationsFragment) getSupportFragmentManager()
                .findFragmentById(R.id.sample_content_fragment);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mDeleteReceiver, new IntentFilter(ACTION_NOTIFICATION_DELETE));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mDeleteReceiver);
    }

总结

在牛轧糖的Android版本中,有了这个不错的功能,但是在国内,要用上Android N恐怕还需要一段时间。听说Android O已经又要准备推出预览版了,爱好学习的你们,准备好迎接变革了么。共勉!

如有任何问题,请及时与我联系。谢谢!