Skip to content

Instantly share code, notes, and snippets.

@PetkevichPavel
Created August 24, 2019 08:20
Show Gist options
  • Save PetkevichPavel/6c8ff28ccba12bdb61eaac7c3c8eb07c to your computer and use it in GitHub Desktop.
Save PetkevichPavel/6c8ff28ccba12bdb61eaac7c3c8eb07c to your computer and use it in GitHub Desktop.
AN_Cloud Function: Firebase Messaging Service for processing the notifications.
class FCMService : FirebaseMessagingService() {
companion object {
private const val TAG = "FCMService"
private const val RECEIVER_TAG = "Receiver:"
const val IN_AP_EX_STR_TITLE = "in_ap_ex_str_title"
const val IN_AP_EX_STR_BODY = "in_ap_ex_str_body"
}
@SuppressLint("TimberArgCount")
override fun onNewToken(token: String?) {
super.onNewToken(token)
Timber.tag(TAG).i("token refreshed: ", token)
}
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
remoteMessage?.let { msg ->
Timber.tag(TAG).i("Config state update: ${msg.from}")
msg.from?.contains(PUSH_RC)?.takeIf { it }?.let {
sendRcMessage(this)
} ?: sendInAppNotification(msg, this)
}
}
private fun sendInAppNotification(remoteMessage: RemoteMessage,
context: Context) {
val fcmIntent = Intent(INTENT_FCM)
remoteMessage.notification?.let { not ->
not.title?.let { title ->
not.body?.let { body ->
fcmIntent.putExtra(IN_AP_EX_STR_TITLE, title)
fcmIntent.putExtra(IN_AP_EX_STR_BODY, body)
Timber.tag(RECEIVER_TAG).i("Broadcasting message was send.")
LocalBroadcastManager.getInstance(context).sendBroadcast(fcmIntent)
}
}
}
}
private fun sendRcMessage(context: Context) {
Timber.tag(RECEIVER_TAG).i("Broadcasting message was send.")
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(INTENT_FCM))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment