Fixer les notifications pour Android O et Heads up

This commit is contained in:
odrling 2018-03-11 17:12:49 +01:00
parent d93c453ab5
commit cac383095c

View file

@ -1,6 +1,7 @@
package xyz.johnny.norntalk.messages
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
@ -9,7 +10,6 @@ import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.preference.PreferenceManager
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat
import android.support.v4.app.RemoteInput
import android.util.SparseArray
@ -37,6 +37,7 @@ class NornNotification private constructor(val conversationId: Int, val context:
*/
private val notifications = SparseArray<NornNotification>()
@TargetApi(Build.VERSION_CODES.O)
/**
* Méthode retournant un builder pour différentes versions d'Android
*
@ -45,11 +46,14 @@ class NornNotification private constructor(val conversationId: Int, val context:
fun getBuilder(context: Context): NotificationCompat.Builder {
val channelId = "messages"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getNotificationChannel(
channelId,
"Messages",
"Messages",
NotificationManager.IMPORTANCE_HIGH)
val name = "messages"
val description = "Messages"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(channelId, name, importance)
channel.description = description
// Register the channel with the system
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
return NotificationCompat.Builder(context, channelId)
}
@ -91,21 +95,6 @@ class NornNotification private constructor(val conversationId: Int, val context:
return this.toast!!
}
/**
* Crée une chaine de notification pour Android O et supérieur
*
* @param id Identifiant de la chaine de notification
* @param name Nom de la chaine
* @param description Description de la chaine
* @param importance Importance de la chaine
*/
@RequiresApi(Build.VERSION_CODES.O)
fun getNotificationChannel(id: String, name: String, description: String, importance: Int): NotificationChannel {
val channel = NotificationChannel(id, name, importance)
channel.description = description
return channel
}
}
/**
@ -143,6 +132,7 @@ class NornNotification private constructor(val conversationId: Int, val context:
this.builder.setSmallIcon(R.drawable.ic_message)
this.builder.setVibrate(longArrayOf(300, 300, 300, 300, 300))
this.builder.setLights(Color.WHITE, 1000, 500)
this.builder.priority = NotificationCompat.PRIORITY_HIGH
// assigner le style à la notification
this.builder.setStyle(this.messagingStyle)