Skip to content

IllegalStateException in onMessageReceived for only data payload notifications #133

@franciscohanna92

Description

@franciscohanna92

I have integrated Pusher Beams into an Android app following the docs. I have setup a service to process data notifications (only payload data).

When my app is fully closed (not in the background), the service throws the following exception:

022-02-25 12:14:09.509 16880-26719/com.staffcom.watch E/AndroidRuntime: FATAL EXCEPTION: Firebase-Messaging-Intent-Handle
    Process: com.staffcom.watch, PID: 16880
    java.lang.IllegalStateException: PushNotifications.start must have been called before
        at com.pusher.pushnotifications.PushNotifications.getDeviceInterests(PushNotifications.java:159)
        at com.staffcom.watch.pusher.beams.NotificationsMessagingService.onMessageReceived(NotificationsMessagingService.kt:28)
        at com.pusher.pushnotifications.fcm.MessagingService$underlyingService$1.invoke(MessagingService.kt:90)
        at com.pusher.pushnotifications.fcm.MessagingService$underlyingService$1.invoke(MessagingService.kt:15)
        at com.pusher.pushnotifications.fcm.MessagingService$WrappedFirebaseMessagingService.onMessageReceived(MessagingService.kt:57)
        at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(com.google.firebase:firebase-messaging@@20.2.3:85)
        at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(com.google.firebase:firebase-messaging@@20.2.3:55)
        at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(com.google.firebase:firebase-messaging@@20.2.3:34)
        at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(com.google.firebase:firebase-messaging@@20.2.3:23)
        at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$EnhancedIntentService(com.google.firebase:firebase-messaging@@20.2.3:43)
        at com.google.firebase.messaging.EnhancedIntentService$$Lambda$0.run(Unknown Source:6)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:6)
        at java.lang.Thread.run(Thread.java:923)

This is my service implementation:

class NotificationsMessagingService : MessagingService() {
    private val json = Json { ignoreUnknownKeys = true }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        val messagePayload = remoteMessage.data["payload"]

        if (messagePayload != null) {
            val event = json.decodeFromString<Event>(messagePayload)
            val interests = PushNotifications.getDeviceInterests()
            when (event.eventName) {
                "added" -> {
                    val eventWithPayload = json.decodeFromString<EventWithPayload<UserTeamEventPayload>>(messagePayload)
                    interests.add("team-${eventWithPayload.data.id}")
                }
                "removed" -> {
                    val eventWithPayload = json.decodeFromString<EventWithPayload<UserTeamEventPayload>>(messagePayload)
                    interests.remove("team-${eventWithPayload.data.id}")
                }
            }
            PushNotifications.setDeviceInterests(interests.toSet())
            Log.i("NotificationsService", event.eventName)
        }
    }
}

The error makes sense, since PushNotifications.start is only called when my MainActivity is constructed and in the service I'm trying to update the device interests (wich need Pusher to be initialized). How can I initialize it for this service? I don't have access to the application context to do it. Could I check if the PushNotifications instance has been instantiated before handling the notifications in the service?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions