Android N Preview 开发介绍(2)

直接回复.png

上一次分享了Multi-Window的开发,今天继续介绍Android N Preview新特性-Notifications(通知)扩展功能,使用户快速响应通知而不需要进入你的App,可以设置通知组,相关通知折叠显示。

直接回复

背景

用户可以在通知界面里直接快速回复

开发

创建内联回复Notification
  1. 创建RemoteInput.Builder实例添加到notification action
private static final String KEY_TEXT_REPLY = "key_text_reply";
String replyLabel = getResources().getString(R.string.reply_label);
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
        .setLabel(replyLabel)
        .build();
  1. 使用addRemoteInput()添加到RemoteInput
Notification.Action action =
        new Notification.Action.Builder(R.drawable.ic_reply_icon,
                getString(R.string.label), replyPendingIntent)
                .addRemoteInput(remoteInput)
                .build();
  1. 将该Action添加到Notification里,并且发出
Notification notification =
        new Notification.Builder(mContext)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle(getString(R.string.title))
                .setContentText(getString(R.string.content))
                .addAction(action))
                .build();
NotificationManager notificationManager =
        NotificationManager.from(mContext);
notificationManager.notify(notificationId, notification);
从内联回复中接受用户输入
  1. 调用getResultsFromIntent()从action intent获取用户输入参数
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
            return remoteInput.getCharSequence(KEY_TEXT_REPLY);
            }

捆绑Notifications

背景

捆绑notifications和Android Wear里的Notification堆栈类似,如果你的app创建notifications来接受信息,当超过一个信息被接收到,捆绑这些notifications作为一个单独的组。

开发

  1. 为每一个你想捆绑在一起的notifications,调用setGroup()来设置指定的Group Key,然后调用notify()来发送到app。
final static String GROUP_KEY_EMAILS = "group_key_emails";
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender1)
         .setContentText(subject1)
         .setSmallIcon(R.drawable.new_mail)
         .setGroup(GROUP_KEY_EMAILS)
         .build();
// Issue the notification
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);
  1. 当你创建另一个notification时,设置同一个Group Key。
Notification notif2 = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender2)
         .setContentText(subject2)
         .setSmallIcon(R.drawable.new_mail)
         .setGroup(GROUP_KEY_EMAILS)
         .build();
notificationManager.notify(notificationId2, notif2);

总结

Notifications新的特性开发还是很简单的,但确实可以很好地运用在之后的App中,Android N Preview开发就简单介绍到这里,期待Android之后新的特性。

参考

Notifications
Stacking Notifications

欢迎关注我的微博

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文出处: http://www.androidchina.net/6174.html Notification在...
    木木00阅读 12,399评论 3 32
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,637评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • 关于明星的生活,人们一向知之甚少,而他们的感情生活,则更加的扑朔迷离。在去年,风行曝出黄轩在韩国与宋昰昀牵手拥吻,...
    搜影说阅读 459评论 0 0
  • 一杯水不多但却需要一桶水的积淀,我寻找着一滴滴分散的水,但它们不能汇聚,不会相溶,我在等待那神奇的一刻,它们聚集在...
    书海心岛阅读 311评论 0 0