Android Kotlin 通知サンプル

  • Android 8.0 以降で通知をまとめる(以前ではまとめない)
  • 通知クリックでアクティビティを起動する

Android 8.0

通知がまとまっている

android_notification_group01

通知を展開

android_notification_group02

Android5.0

通知はまとまらない

android_notification_group03

サンプル

  1. private var mNotificationNumber = 0
  2. /**
  3. * 通知
  4. * @see http://android.techblog.jp/archives/7976103.html
  5. * @see https://developer.android.com/guide/topics/ui/notifiers/notifications?hl=ja
  6. * @see https://developer.android.com/training/notify-user/build-notification
  7. * @see https://developer.android.com/training/notify-user/channels
  8. * @see https://developer.android.com/training/notify-user/group?hl=ja#create_a_group_and_add_a_notification_to_it
  9. * @see https://qiita.com/mstssk/items/14e1b94be6c52af3a0a6
  10. * @see https://qiita.com/naoi/items/367fc23e55292c50d459
  11. * @see https://www.gaprot.jp/pickup/old-tips/android-o/notification-channel
  12. */
  13. private fun notification() {
  14. val channelId = "channelId"
  15. val channelName = "GcpApiTrial"
  16. val groupKey = "groupKey"
  17. val notifyId = ++mNotificationNumber;
  18.  
  19. val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  20.  
  21. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  22. // 通知チャネルの登録
  23. // targetSdkVersion(26 Oreo) 以降の場合、通知チャネルを使用しないと通知が表示されない
  24. // 複数のチャンネルを作り、個々の通知を任意のチャンネルに割り振ることによって、
  25. // 重要度や通知音などの属性を一括で指定することができる
  26. val summaryId = 0
  27. if (manager.getNotificationChannel(channelId) == null) {
  28. val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
  29. channel.apply {
  30. description = "GcpApiTrial Notification"
  31. }
  32. manager.createNotificationChannel(channel)
  33. }
  34. val summary = NotificationCompat.Builder(this, channelId).run {
  35. setContentTitle("Summary Content Title")
  36. setContentText("New Message.")
  37. setSmallIcon(R.drawable.ic_menu_camera)
  38. setStyle(NotificationCompat.InboxStyle().addLine("New Message Add line"))
  39. setNumber(mNotificationNumber)
  40. setGroup(groupKey)
  41. setGroupSummary(true)
  42. build()
  43. }
  44. manager.apply {
  45. notify(summaryId, summary)
  46. }
  47. }
  48.  
  49. // 通知クリックでアクティビティを起動
  50. val intent = Intent(this, MainActivity::class.java)
  51. val stackBuilder = TaskStackBuilder.create(this)
  52. stackBuilder.addParentStack(MainActivity::class.java)
  53. stackBuilder.addNextIntent(intent)
  54. val pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT)
  55.  
  56. // 通知の作成
  57. val notification = NotificationCompat.Builder(this, channelId).run {
  58. setContentTitle("New Message")
  59. setContentText("You've received new message. No.$mNotificationNumber")
  60. setSmallIcon(R.drawable.ic_menu_camera)
  61. setNumber(mNotificationNumber)
  62. setContentIntent(pendingIntent)
  63. setAutoCancel(true) // 通知クリックでクリア
  64. setGroup(groupKey)
  65. build()
  66. }
  67. manager.apply {
  68. notify(notifyId, notification)
  69. }
  70. }

参考

http://android.techblog.jp/archives/7976103.html
https://developer.android.com/guide/topics/ui/notifiers/notifications?hl=ja
https://developer.android.com/training/notify-user/build-notification
https://developer.android.com/training/notify-user/channels
https://developer.android.com/training/notify-user/group?hl=ja#create_a_group_and_add_a_notification_to_it
https://qiita.com/mstssk/items/14e1b94be6c52af3a0a6
https://qiita.com/naoi/items/367fc23e55292c50d459
https://www.gaprot.jp/pickup/old-tips/android-o/notification-channel

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です