forked from KohlsAdrian/zendesk2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyFirebaseMessagingService.kt
70 lines (58 loc) · 2.81 KB
/
MyFirebaseMessagingService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package br.com.adriankohls.zendesk2.fcm
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.media.RingtoneManager
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import br.com.adriankohls.zendesk2.R
import zendesk.chat.Chat
import zendesk.chat.PushData
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
Chat.INSTANCE.providers()?.pushNotificationsProvider()?.registerPushToken(token)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val pushProvider = Chat.INSTANCE.providers()?.pushNotificationsProvider()
if (pushProvider != null) {
val pushData = pushProvider.processPushNotification(remoteMessage.data)
pushData?.let {
when (it.type) {
PushData.Type.MESSAGE -> {
val builder = NotificationCompat.Builder(this, createNotificationChannel())
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContentTitle(it.author)
.setContentText(it.message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setVibrate(longArrayOf(0, 500, 100, 500))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
with(NotificationManagerCompat.from(this)) {
// notificationId is a unique int for each notification that you must define
notify(101, builder.build())
}
}
else -> {
}
}
}
}
}
private fun createNotificationChannel(): String {
val supportChannel = "support_channel"
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(supportChannel, "chat", importance)
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
return supportChannel
}
}