OneSignal-Flutter-SDK
OneSignal-Flutter-SDK copied to clipboard
setNotificationOpenedHandler is not called when I click the notification and app in killed status #599
What happened?
setNotificationOpenedHandler is not called when I click the notification and app in killed status
Steps to reproduce?
1. Open the application
2. Swipe out the application
3. Trigger a notification from the dashboard
4. Click on the notification
5. The handler is not called
What did you expect to happen?
setNotificationOpenedHandler should work irrespective to the app state.
OneSignal Flutter SDK version
OneSignal Flutter SDK 3.4.1
Which platform(s) are affected?
- [ ] iOS
- [X] Android
Relevant log output
No response
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
I thought I had the same problem but actually the handler is called (very early).
Here's a snippet of my code:
OneSignal.shared.setNotificationOpenedHandler((openedResult) {
// called whenever the user opens a OneSignal push notification
_lastOpenedOneSignalNotification = openedResult.notification;
if (openedResult.notification.additionalData != null) {
PiadaNotificationsHandler.defaultKey.currentState
?.handleNotificationPayload(openedResult.notification.additionalData!);
}
});
OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
// called whenever a notification arrives and the application is in foreground
event.complete(event.notification);
});
// OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
await OneSignal.shared.setAppId(const String.fromEnvironment("..."));
As you can see I save the _lastOpenedOneSignalNotification
to reuse later when another widget is finally loaded and ready to handle the action.
I tested this code on an iPhone XR and a Motorola E4
Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work
i got same problem. i can't find a way to access to additinalData after the app is opened
Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work
does notification work when the app is running?
Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work
does notification work when the app is running?
yes it works. for background try running flutter run --release. I opted to use firebase messaging though
Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work
does notification work when the app is running?
yes it works. for background try running flutter run --release. I opted to use firebase messaging though
use onesignal instead. it's based on firebase but with more features
I thought I had the same problem but actually the handler is called (very early).
Here's a snippet of my code:
OneSignal.shared.setNotificationOpenedHandler((openedResult) { // called whenever the user opens a OneSignal push notification _lastOpenedOneSignalNotification = openedResult.notification; if (openedResult.notification.additionalData != null) { PiadaNotificationsHandler.defaultKey.currentState ?.handleNotificationPayload(openedResult.notification.additionalData!); } }); OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) { // called whenever a notification arrives and the application is in foreground event.complete(event.notification); }); // OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none); await OneSignal.shared.setAppId(const String.fromEnvironment("..."));
As you can see I save the
_lastOpenedOneSignalNotification
to reuse later when another widget is finally loaded and ready to handle the action.I tested this code on an iPhone XR and a Motorola E4
Hi, @muccy. Were you able to access notification data even when app is killed? I think I didn't get the point.
In our application we have configured setNotificationOpenedHandler
, that works only when the app is already running.
The only part we didn't implemented yet is setNotificationWillShowInForegroundHandler
, but I don't think it's responsible for catching the notification data when the app is killed.
Hey @ebragaparah, I confirm you the "notification opened handler" is called also when the app is opened (if the app is opened through a push notification activation).
I can only add that my app setup is a but awkward because it also includes Firebase FCM, I don't know if it matters (I had to follow this tip on iOS: https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/348#issuecomment-824163607).
I also remember the handler was called very very early, so make sure you assign it ASAP
Thanks, @muccy.
yes, sure! We are already using the setNotificationOpenedHandler
, but as I said, it only works when the app is already running or when it's not killed. So, do you have to use FCM in order to have the notification correctly parsed when the app is not running?
and yes, OneSignal is being setup very very early.
I don't know if it's a coincidence, but I thought it may be the only difference in our setups
right. but, are you able to parse the content of a received push notification when the app is not on background nor foreground? I mean when the state is killed or not started, something like this
Yeah. During my tests I killed my app on purpose in order to inspect the incoming payload. To do so I compiled the app in release mode
Hey, @muccy! Thanks for all the support, dude! I checked up my setup and realized that I was not providing correctly all the dependencies that the view need.
The OneSignal hook is working properly even when the app is killed, and now I'm able to get the data from the push notification and handle it.
Another interesting thing is that I could visualize the issue running on release mode, in a real device.
Great to hear so, dude :)