Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalStateException in onMessageReceived for only data payload notifications #133

Open
franciscohanna92 opened this issue Feb 25, 2022 · 2 comments

Comments

@franciscohanna92
Copy link

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?

@benw-pusher
Copy link

As you say, this error makes sense as the app is completely closed and so no PushNotifications instance currently exists. As you are not able to initialise the instance then you may need to store the new interest values and then update the interests the next time the mainActivity starts.

As an aside, it looks as though you may be managing device <> interest associations on your server sideL detecting when the interest needs to change and then sending a background notification to get the device to carry out the notification. You may consider using Authenticated Users which would allow you to manage user <> topic associations at your server (without the need for these background notifications) and then when you come to publish a notification you can lookup which users should be targeted.

@franciscohanna92
Copy link
Author

franciscohanna92 commented Mar 7, 2022

Thanks for your answer! That's exactly what I'm doing.

Will the device receive push notifications even when the app is fully closed using the Authenticated Users feature? If so, I'll redesign my solution and implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants