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

[Bug]: INotificationLifecycleListener can't prevent display of notification from INotificationServiceExtension #2057

Open
1 task done
AlanMTuring opened this issue Apr 15, 2024 · 3 comments

Comments

@AlanMTuring
Copy link

AlanMTuring commented Apr 15, 2024

How can we help?

How do I properly NOT display a notification on a specific screen? event.preventDefault() is not working.

Our app is implementing an INotificationServiceExtension and in the onNotificationReceived function we decrpyt the message, update the notification, and call event.display().

On a the screen in our app where the notification would normally take the user, I'm using the INotificationWillDisplayEvent in the INotificationLifecycleListener.onWillDisplay to call preventDefault so that the notification would not display, but the notification always displays. I'm assuming this is because we're calling .display() from the service extension, but I don't know how to get around that.

Am I doing something wrong in the implementation? or is this a bug and should I create a bug issue?

The code is using version 5.1.8 of the SDK.

Here is the code from the Service Extension. Confirmed that this is successfully decrypting the message and showing the proper notification.

override fun onNotificationReceived(event: INotificationReceivedEvent) {
    try {
        val updatedNotification = getDecryptedNotification(event.notification)
        updatedNotification.display()
    } catch (ex: Exception) {
        event.notification.display()
    }
}

Here is the code from the screen that should be hiding the notification, but confirmed is not working.

OneSignal.Notifications.addForegroundLifecycleListener(object : INotificationLifecycleListener {
    override fun onWillDisplay(event: INotificationWillDisplayEvent) {
        event.preventDefault()
    }
})

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jkasten2
Copy link
Member

@AlanMTuring could you let us know where you are calling OneSignal.initWithContext and what type of Context you are passing it?

  • This detail is important as it can effect if the SDK know if it is in the foreground or not, which in turn effects if the ForegroundLifecycleListener fire or not.
  • It should be in your Application.onCreate().

If the above is correct, can you confirm onWillDisplay is firing by adding some logging? Also including the OneSignal VERBOSE logging will be helpful.

@AlanMTuring
Copy link
Author

We pass the applicationContext into OneSignal.initWithContext and I've confirmed that it is happening in our Application.onCreate().

I added verbose logging via OneSignal and also added two small logs of my own. I launched our app, navigated to the screen that has the INotificationLifecycleListener, and took the steps to receive a notification. I've attached all the logs that happened, but the ones to note are here:

The lifecycle listener is getting added:

2024-04-18 10:20:50.257 22861-22861 OneSignal               com.{{app package}}    D  [main] NotificationsManager.addForegroundLifecycleListener(listener: com.{{app package}}.ui.conversations.details.ConversationDetailViewModel$notificationLifecycleListener$1@d396ce2)

and this confirms that the onWillDisplay code gets reached and that event.preventDefault() is getting called, both from my logs and the OneSignal logs.

2024-04-18 10:20:57.948 22861-22861 KalinDebug              com.{{app package}}    D  inside OneSignal INotificationLifecycleListener onWillDisplay
2024-04-18 10:20:57.948 22861-22861 KalinDebug              com.{{app package}}    D  calling OneSignal event.preventDefault()
2024-04-18 10:20:57.948 22861-22861 OneSignal               com.{{app package}}    D  [main] NotificationWillDisplayEvent.preventDefault()

and the notification is still coming through. Happy to share any other info!

OneSignalLogs.txt

@AlanMTuring
Copy link
Author

Any updates here? Does this look like a OneSignal issue or an issue on our end, with setup or something else?

@AlanMTuring AlanMTuring changed the title INotificationLifecycleListener can't prevent display of notification from INotificationServiceExtension [Bug] INotificationLifecycleListener can't prevent display of notification from INotificationServiceExtension May 20, 2024
@AlanMTuring AlanMTuring changed the title [Bug] INotificationLifecycleListener can't prevent display of notification from INotificationServiceExtension [Bug]: INotificationLifecycleListener can't prevent display of notification from INotificationServiceExtension May 20, 2024
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