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,396評論 3 32
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,554評論 0 17
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,992評論 19 139
  • 關于明星的生活,人們一向知之甚少,而他們的感情生活,則更加的撲朔迷離。在去年,風行曝出黃軒在韓國與宋昰昀牽手擁吻,...
    搜影說閱讀 456評論 0 0
  • 一杯水不多但卻需要一桶水的積淀,我尋找著一滴滴分散的水,但它們不能匯聚,不會相溶,我在等待那神奇的一刻,它們聚集在...
    書海心島閱讀 311評論 0 0