1

I just want to know if it's really possible to save data when onMessageReceived is triggered. I want to show a notification badge inside my application to show how much notifications he received when the app was closed. The code I made is not working, I am receiving the notifications when my app is closed but I am unable to save an integer in sharedPreferences. Is it maybe because of the context? I'm not really sure, every answer is appreciated thanks!

class FirebaseMessagingServiceClass : FirebaseMessagingService(){
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        if (remoteMessage.notification != null){
            val title = remoteMessage.notification?.title
            val body = remoteMessage.notification?.body

            val prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext)
            val notificationBadgeCount = prefs.getInt("notification_badge_count", 0)

            val prefsEditor = prefs?.edit()
            prefsEditor?.putInt("notification_badge_count", notificationBadgeCount + 1)
            prefsEditor?.apply()

            NotificationHelper.displayNotification(applicationContext, "Status Channel", 1010, title!!, body!!)
        }
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

0 Answers0