11package com.glodanif.bluetoothchat.view
22
3- import android.app.Notification
4- import android.app.NotificationManager
5- import android.app.PendingIntent
6- import android.app.Service
3+ import android.app.*
74import android.content.Context
85import android.content.Intent
96import android.graphics.Bitmap
107import android.graphics.BitmapFactory
118import android.graphics.Color
129import android.os.Build
10+ import android.support.v4.app.NotificationCompat
1311import com.glodanif.bluetoothchat.R
1412import com.glodanif.bluetoothchat.activity.ChatActivity
1513import com.glodanif.bluetoothchat.activity.ConversationsActivity
@@ -18,6 +16,10 @@ import com.glodanif.bluetoothchat.util.NotificationSettings
1816
1917class NotificationViewImpl (private val context : Context ) : NotificationView {
2018
19+ private val CHANNEL_FOREGROUND = " channel.foreground"
20+ private val CHANNEL_REQUEST = " channel.request"
21+ private val CHANNEL_MESSAGE = " channel.message"
22+
2123 private val notificationManager =
2224 context.getSystemService(Service .NOTIFICATION_SERVICE ) as NotificationManager
2325 private val resources = context.resources
@@ -32,9 +34,13 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
3234 stopIntent.action = BluetoothConnectionService .ACTION_STOP
3335 val stopPendingIntent = PendingIntent .getService(context, 0 , stopIntent, 0 )
3436
35- val icon = BitmapFactory .decodeResource(resources, R .mipmap.ic_launcher)
37+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
38+ val channel = NotificationChannel (CHANNEL_FOREGROUND , context.getString(R .string.notification__channel_background), NotificationManager .IMPORTANCE_LOW )
39+ notificationManager.createNotificationChannel(channel)
40+ }
3641
37- val builder = Notification .Builder (context)
42+ val icon = BitmapFactory .decodeResource(resources, R .mipmap.ic_launcher)
43+ val builder = NotificationCompat .Builder (context, CHANNEL_FOREGROUND )
3844 .setContentTitle(context.getString(R .string.app_name))
3945 .setContentText(message)
4046 .setSmallIcon(R .drawable.ic_notification)
@@ -45,7 +51,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
4551 .addAction(0 , context.getString(R .string.notification__stop), stopPendingIntent)
4652
4753 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
48- builder.setColor( resources.getColor(R .color.colorPrimary) )
54+ builder.color = resources.getColor(R .color.colorPrimary)
4955 }
5056
5157 return builder.build()
@@ -61,7 +67,12 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
6167 val icon = BitmapFactory .decodeResource(resources, R .mipmap.ic_launcher)
6268 val name = if (displayName.isNullOrEmpty()) deviceName else " $displayName ($deviceName )"
6369
64- val builder = Notification .Builder (context)
70+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
71+ val channel = NotificationChannel (CHANNEL_MESSAGE , context.getString(R .string.notification__channel_message), NotificationManager .IMPORTANCE_MAX )
72+ notificationManager.createNotificationChannel(channel)
73+ }
74+
75+ val builder = NotificationCompat .Builder (context, CHANNEL_MESSAGE )
6576 .setContentTitle(name)
6677 .setContentText(message)
6778 .setLights(Color .BLUE , 3000 , 3000 )
@@ -72,7 +83,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
7283 .setPriority(Notification .PRIORITY_MAX )
7384
7485 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
75- builder.setColor( resources.getColor(R .color.colorPrimary) )
86+ builder.color = resources.getColor(R .color.colorPrimary)
7687 }
7788
7889 val notification = builder.build()
@@ -94,9 +105,14 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
94105 notificationIntent.flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK
95106 val pendingIntent = PendingIntent .getActivity(context, 0 , notificationIntent, 0 )
96107
108+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
109+ val channel = NotificationChannel (CHANNEL_REQUEST , context.getString(R .string.notification__channel_request), NotificationManager .IMPORTANCE_MAX )
110+ notificationManager.createNotificationChannel(channel)
111+ }
112+
97113 val icon = BitmapFactory .decodeResource(resources, R .mipmap.ic_launcher)
98114
99- val builder = Notification .Builder (context)
115+ val builder = NotificationCompat .Builder (context, CHANNEL_REQUEST )
100116 .setContentTitle(context.getString(R .string.notification__connection_request))
101117 .setContentText(context.getString(R .string.notification__connection_request_body, deviceName))
102118 .setLights(Color .BLUE , 3000 , 3000 )
@@ -107,7 +123,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
107123 .setPriority(Notification .PRIORITY_MAX )
108124
109125 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
110- builder.setColor( resources.getColor(R .color.colorPrimary) )
126+ builder.color = resources.getColor(R .color.colorPrimary)
111127 }
112128
113129 val notification = builder.build()
@@ -125,7 +141,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
125141
126142 override fun dismissMessageNotification () {
127143 notificationManager.cancel(
128- NotificationView .NOTIFICATION_TAG_MESSAGE ,NotificationView .NOTIFICATION_ID_MESSAGE )
144+ NotificationView .NOTIFICATION_TAG_MESSAGE , NotificationView .NOTIFICATION_ID_MESSAGE )
129145 }
130146
131147 override fun dismissConnectionNotification () {
0 commit comments