-
Notifications
You must be signed in to change notification settings - Fork 9
/
CustomerIOFirebaseMessagingService.kt
84 lines (75 loc) · 3.1 KB
/
CustomerIOFirebaseMessagingService.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package io.customer.messagingpush
import android.content.Context
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.customer.base.comunication.Action
import io.customer.sdk.CustomerIO
import io.customer.sdk.extensions.getErrorResult
class CustomerIOFirebaseMessagingService : FirebaseMessagingService() {
companion object {
private const val TAG = "FirebaseMessaging:"
/**
* Handles receiving an incoming push notification.
*
* Call this from a custom [FirebaseMessagingService] to pass push messages to
* CustomerIo SDK for tracking and rendering
* @param remoteMessage Remote message received from Firebase in
* [FirebaseMessagingService.onMessageReceived]
* @param handleNotificationTrigger indicating if the local notification should be triggered
* @param errorCallback callback containing any error occurred
* @return Boolean indicating whether this will be handled by CustomerIo
*/
@JvmOverloads
fun onMessageReceived(
context: Context,
remoteMessage: RemoteMessage,
handleNotificationTrigger: Boolean = true,
errorCallback: Action.Callback<Unit> = Action.Callback { }
): Boolean {
return try {
handleMessageReceived(context, remoteMessage, handleNotificationTrigger)
} catch (e: Exception) {
errorCallback.onResult(e.getErrorResult())
false
}
}
/**
* Handles new or refreshed token
* Call this from [FirebaseMessagingService] to register the new device token
*
* @param token new or refreshed token
* @param errorCallback callback containing any error occurred
*/
@JvmOverloads
fun onNewToken(
token: String,
errorCallback: Action.Callback<Unit> = Action.Callback { }
) {
handleNewToken(token, errorCallback)
}
private fun handleNewToken(token: String, errorCallback: Action.Callback<Unit>) {
try {
CustomerIO.instance().registerDeviceToken(deviceToken = token)
.enqueue(errorCallback)
} catch (exception: IllegalStateException) {
Log.e(TAG, "Error while handling token: ${exception.message}")
errorCallback.onResult(exception.getErrorResult())
}
}
private fun handleMessageReceived(
context: Context,
remoteMessage: RemoteMessage,
handleNotificationTrigger: Boolean = true
): Boolean {
val handler = CustomerIOPushNotificationHandler(remoteMessage = remoteMessage)
return handler.handleMessage(context, handleNotificationTrigger)
}
}
override fun onNewToken(token: String) {
handleNewToken(token) { }
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
handleMessageReceived(this, remoteMessage)
}
}